Class: RubyCms::MigrationInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cms/migration_installer.rb

Overview

Copies the migrations a module set needs into the app's db/migrate.

The gem ships migrations with fixed historical timestamps. Some depend on tables created by install-time generators (ahoy, action_text) whose freshly generated migrations have LATER timestamps — so copying the gem files verbatim would run them before those tables exist. To guarantee correct order, copied migrations are RE-STAMPED with versions strictly greater than every migration already in the app, preserving the gem's own relative order. Migrations already present (matched by basename, ignoring the timestamp) are skipped.

Instance Method Summary collapse

Constructor Details

#initialize(templates_root:, app_root:) ⇒ MigrationInstaller

Returns a new instance of MigrationInstaller.



17
18
19
20
# File 'lib/ruby_cms/migration_installer.rb', line 17

def initialize(templates_root:, app_root:)
  @src_dir = Pathname.new(templates_root).join("db/migrate")
  @dest_dir = Pathname.new(app_root).join("db/migrate")
end

Instance Method Details

#install(modules) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_cms/migration_installer.rb', line 22

def install(modules)
  return [] unless @src_dir.directory?

  wanted = modules.flat_map(&:migrations).uniq
  FileUtils.mkdir_p(@dest_dir)
  existing = @dest_dir.children.map {|p| basename_without_timestamp(p.basename.to_s) }

  version = max_existing_version
  copied = []
  sources(wanted, existing).each do |src|
    version += 1
    base = basename_without_timestamp(src.basename.to_s)
    dest = @dest_dir.join("#{version}_#{base}.rb")
    FileUtils.cp(src, dest)
    copied << dest.basename.to_s
  end
  copied
end