Class: Angarium::Generators::MigrationsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/angarium/migrations/migrations_generator.rb

Overview

Installs (and, after a gem upgrade, refreshes) Angarium's engine migrations. Angarium keeps its migrations in db/angarium_migrate rather than the conventional db/migrate, so Rails never auto-appends them onto the host's primary connection nor generates a generic install:migrations task: this generator is the single install path. It is multi-database aware: it reads config.database (set at install time) so a host that forgets the flag on a later run still gets new migrations in the right place. Uses Rails' native ActiveRecord::Migration.copy, so re-runs are idempotent (already-installed migrations are skipped).

Instance Method Summary collapse

Instance Method Details

#install_migrationsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/generators/angarium/migrations/migrations_generator.rb', line 22

def install_migrations
  database = options[:database].presence || Angarium.config.migrations_database
  # The primary connection always migrates from db/migrate; only a separate
  # database gets its own db/<name>_migrate path.
  dir = (database.nil? || database.to_s == "primary") ? "db/migrate" : "db/#{database}_migrate"
  copied = ActiveRecord::Migration.copy(
    File.join(destination_root, dir),
    {"angarium" => Angarium::Engine.root.join("db/angarium_migrate").to_s}
  )
  if copied.any?
    say_status :installed, "#{copied.size} Angarium migration(s) into #{dir}", :green
  else
    say_status :identical, "Angarium migrations already present in #{dir}", :blue
  end
end