lib/erd/migrator.rb in erd-0.4.0 vs lib/erd/migrator.rb in erd-0.5.0

- old
+ new

@@ -1,14 +1,17 @@ +# frozen_string_literal: true + require 'rails/generators' module Erd class MigrationError < StandardError; end class Migrator class << self def status - migrated_versions = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}").map {|v| '%.3d' % v} + migration_table_name = defined?(ActiveRecord::SchemaMigration) ? ActiveRecord::SchemaMigration.table_name : ActiveRecord::Migrator.schema_migrations_table_name + migrated_versions = ActiveRecord::Base.connection.select_values("SELECT version FROM #{migration_table_name}").map {|v| '%.3d' % v} migrations = [] ActiveRecord::Migrator.migrations_paths.each do |path| Dir.foreach(Rails.root.join(path)) do |file| if (version_and_name = /^(\d{3,})_(.+)\.rb$/.match(file)) status = migrated_versions.delete(version_and_name[1]) ? 'up' : 'down' @@ -26,11 +29,16 @@ # run_migrations up: '20120512020202', down: ... # run_migrations up: ['20120512020202', '20120609010203', ...] def run_migrations(migrations) migrations.each do |direction, version_or_filenames| Array.wrap(version_or_filenames).each do |version_or_filename| - /^(?<version>\d{3,})/ =~ File.basename(version_or_filename) - ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_path, version.to_i) + version = File.basename(version_or_filename)[/\d{3,}/] + + if defined? ActiveRecord::MigrationContext # >= 5.2 + ActiveRecord::Base.connection.migration_context.run(direction, version.to_i) + else + ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_paths, version.to_i) + end end if version_or_filenames end if ActiveRecord::Base.schema_format == :ruby File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", 'w') do |file| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)