Class: LazyMigrate::MigratorAdapter

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/lazy_migrate/migrator_adapter.rb

Direct Known Subclasses

NewMigratorAdapter, OldMigratorAdapter

Instance Method Summary collapse

Constructor Details

#initializeMigratorAdapter

Returns a new instance of MigratorAdapter.



14
# File 'lib/lazy_migrate/migrator_adapter.rb', line 14

def initialize; end

Instance Method Details

#bring_to_top(migration:, ask_for_rerun:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lazy_migrate/migrator_adapter.rb', line 22

def bring_to_top(migration:, ask_for_rerun:)
  initial_version = migration.version
  initial_status = migration.status
  initial_filename = self.find_filename_for_migration(migration)

  if initial_filename.nil?
    raise("No file found for migration #{initial_version}")
  end

  re_run = initial_status == 'up' && ask_for_rerun.()

  if re_run
    self.down(initial_version)
  end

  last = self.last_version
  new_version = ActiveRecord::Migration.new.next_migration_number(last ? last + 1 : 0).to_i
  new_filename = replace_version_in_filename(initial_filename, new_version)
  File.rename(initial_filename, new_filename)

  if re_run
    self.up(new_version)
  elsif initial_status == 'up'
    ActiveRecord::SchemaMigration.create(version: new_version)
    ActiveRecord::SchemaMigration.find_by(version: initial_version)&.destroy!
  end
end

#down(version) ⇒ Object



119
# File 'lib/lazy_migrate/migrator_adapter.rb', line 119

def down(version); end

#dump_schemaObject



60
61
62
63
64
65
66
67
68
# File 'lib/lazy_migrate/migrator_adapter.rb', line 60

def dump_schema
  return if !ActiveRecord::Base.dump_schema_after_migration

  # ripped from https://github.com/rails/rails/blob/5-1-stable/activerecord/lib/active_record/railties/databases.rake
  filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
  File.open(filename, "w:utf-8") do |file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

#find_migrationsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lazy_migrate/migrator_adapter.rb', line 71

def find_migrations
  self.load_migration_paths
  current_version = self.last_version

  self.find_migration_tuples
    .reverse
    .map { |status, str_version, name|
      # This depends on how rails reports a file is missing.
      # This is no doubt subject to change so be wary.
      has_file = name != '********** NO FILE **********'
      version = str_version.to_i
      current = version == current_version

      LazyMigrate::Migration.new(
        status: status,
        version: version,
        name: name,
        has_file: has_file,
        current: current,
      )
    }
end

#load_migration_pathsObject



95
96
97
98
99
100
101
102
103
# File 'lib/lazy_migrate/migrator_adapter.rb', line 95

def load_migration_paths
  # silencing cos we might be re-initializing some constants and rails
  # doesn't like that
  Kernel.silence_warnings do
    ActiveRecord::Migrator.migrations_paths.each do |path|
      Dir[Rails.application.root.join("#{path}/**/*.rb")].each { |file| load file }
    end
  end
end

#migrate(version) ⇒ Object



125
# File 'lib/lazy_migrate/migrator_adapter.rb', line 125

def migrate(version); end

#previous_value(arr, value) ⇒ Object



106
107
108
# File 'lib/lazy_migrate/migrator_adapter.rb', line 106

def previous_value(arr, value)
  arr.sort.select { |v| v < value }.last
end

#redo(version) ⇒ Object



122
# File 'lib/lazy_migrate/migrator_adapter.rb', line 122

def redo(version); end

#remove_version_from_table(version) ⇒ Object



111
112
113
# File 'lib/lazy_migrate/migrator_adapter.rb', line 111

def remove_version_from_table(version)
  ActiveRecord::SchemaMigration.find_by!(version: version).destroy!
end

#replace_version_in_filename(filename, new_version) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/lazy_migrate/migrator_adapter.rb', line 51

def replace_version_in_filename(filename, new_version)
  basename = File.basename(filename)
  dir = File.dirname(filename)
  name_after_version = T.must(basename.split('_')[1..]).join('_')
  new_basename = "#{new_version}_#{name_after_version}"
  File.join(dir, new_basename)
end

#rollback(version) ⇒ Object



128
# File 'lib/lazy_migrate/migrator_adapter.rb', line 128

def rollback(version); end

#up(version) ⇒ Object



116
# File 'lib/lazy_migrate/migrator_adapter.rb', line 116

def up(version); end