class LazyMigrate::MigratorAdapter
Public Class Methods
new()
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 14 def initialize; end
Public Instance Methods
bring_to_top(migration:, ask_for_rerun:)
click to toggle source
# 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)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 119 def down(version); end
dump_schema()
click to toggle source
# 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_migrations()
click to toggle source
# 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_paths()
click to toggle source
# 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)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 125 def migrate(version); end
previous_value(arr, value)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 106 def previous_value(arr, value) arr.sort.select { |v| v < value }.last end
redo(version)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 122 def redo(version); end
remove_version_from_table(version)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 128 def rollback(version); end
up(version)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 116 def up(version); end
Protected Instance Methods
find_filename_for_migration(migration)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 139 def find_filename_for_migration(migration); end
find_migration_tuples()
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 133 def find_migration_tuples; end
find_previous_version(version)
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 136 def find_previous_version(version); end
last_version()
click to toggle source
# File lib/lazy_migrate/migrator_adapter.rb, line 142 def last_version; end