Module: Edgestitch::Tasks

Extended by:
Rake::DSL
Defined in:
lib/edgestitch/tasks.rb

Overview

Define rake tasks to deal with edgestitch structurefiles:

db:stitch: creates a structure.sql out of loaded engines and application
           structure-self.sql
db:stitch:<engine_name>: creates a structure-self.sql containing only the
                         engine's models and migrations

In order to integrate well with rails, it also enhances tasks like db:structure:load, db:prepare and others.

Class Method Summary collapse

Class Method Details

.define_engine(engine, namespace: "db:stitch", name: engine.engine_name, enhance_rails: true) ⇒ Rake::Task

Defines a task to generate the structure-self.sql file from an engine. This task is responsible for gathering all models owned by the engine and all migrations defined within the engine and generate a structure-self.sql.

Rails enhancements will happen before each of these tasks:

  • db:structure:dump

  • db:schema:dump

  • app:db:structure:dump

  • app:db:schema:dump

Parameters:

  • engine (Class<Rails::Engine>)

    target class of the task

  • namespace (String) (defaults to: "db:stitch")

    the namespace where the target will be generated [default: db:stitch]

  • name (String) (defaults to: engine.engine_name)

    the name of the task within the given namespace [default: engine.engine_name]

  • enhance_rails (Boolean) (defaults to: true)

    whether edgestitch should enhance the above tasks or not [default: true]

Returns:

  • (Rake::Task)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/edgestitch/tasks.rb', line 63

def define_engine(engine, namespace: "db:stitch", name: engine.engine_name, enhance_rails: true) # rubocop:disable Metrics/MethodLength
  if enhance_rails
    enhance("#{namespace}:#{name}", "db:structure:dump", "app:db:structure:dump", "db:schema:dump",
            "app:db:schema:dump")
  end

  desc "Create structure-self.sql for an engine"
  task "#{namespace}:#{name}" => [:environment] do |_, _args|
    Rails.application.eager_load!
    engine&.eager_load!
    ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
      ::Edgestitch::Exporter.export(engine, ::Edgestitch::Mysql::Dump.new(db_config))
    end
  end
end

.define_stitch(namespace = "db:stitch", enhance_rails: true) ⇒ Rake::Task

Defines a task to stitch a structure.sql from loaded Engines. This task is responsible for gathering all loaded engines and the current application’s structrure-self.sql and stitching them together into a structure.sql.

Rails enhancements will happen before each of these tasks:

  • db:prepare

  • db:structure:load

  • db:schema:load

Parameters:

  • namespace (String) (defaults to: "db:stitch")

    namespace where the task will run [default: db:stitch]

  • enhance_rails (Boolean) (defaults to: true)

    whether edgestitch should enhance the above tasks or not [default: true]

Returns:

  • (Rake::Task)


36
37
38
39
40
41
42
43
44
45
# File 'lib/edgestitch/tasks.rb', line 36

def define_stitch(namespace = "db:stitch", enhance_rails: true)
  enhance(namespace, "db:prepare", "db:structure:load", "db:schema:load") if enhance_rails

  desc "Create structure.sql for an app based on all loaded engines' structure-self.sql"
  task namespace => [:environment] do |_task, _args|
    ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
      ::Edgestitch::Stitcher.to_file(filename(db_config), *::Rails::Engine.subclasses, Rails.application)
    end
  end
end