From 727f933eec032a8f8e53364c5134d16d4bef1d75 Mon Sep 17 00:00:00 2001 From: Hernan Maguina Date: Mon, 20 May 2024 13:11:05 +0200 Subject: [PATCH] Support active record schema migration changes introduced with Rails 7.1 (#17) * update appraisals to handle rails 7 and 7.1 versions * adapt to support active record schema migration 7.1 * bump version for rails 7.1 support * Update version.rb * run rails 7 tests with ruby 2.7+ --------- Co-authored-by: Sergey A. Glukhov --- .rubocop.yml | 6 +++++ Appraisals | 12 ++++++++++ lib/rails_data_migrations/log_entry.rb | 31 +++++++++++++++++++++----- lib/rails_data_migrations/migrator.rb | 20 +++++++++++++++-- lib/rails_data_migrations/version.rb | 2 +- 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 408b2d84..cd54887d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -10,12 +10,18 @@ Gemspec/RequiredRubyVersion: Layout/TrailingEmptyLines: Enabled: false +Metrics/AbcSize: + Enabled: false + Metrics/BlockLength: Enabled: false Metrics/LineLength: Max: 120 +Metrics/MethodLength: + Enabled: false + Naming/FileName: Exclude: - lib/rails-data-migrations.rb diff --git a/Appraisals b/Appraisals index f619e28b..be56edeb 100644 --- a/Appraisals +++ b/Appraisals @@ -40,3 +40,15 @@ if ruby_version >= Gem::Version.new('2.5.0') gem 'sqlite3', '~> 1.4.0' end end + +if ruby_version >= Gem::Version.new('2.7.0') + appraise 'rails-7.0' do + gem 'rails', '~> 7.0.0' + gem 'sqlite3', '~> 1.4.0' + end + + appraise 'rails-7.1' do + gem 'rails', '~> 7.1.0' + gem 'sqlite3', '~> 1.4.0' + end +end diff --git a/lib/rails_data_migrations/log_entry.rb b/lib/rails_data_migrations/log_entry.rb index 0fa1b0b1..df849607 100644 --- a/lib/rails_data_migrations/log_entry.rb +++ b/lib/rails_data_migrations/log_entry.rb @@ -1,14 +1,33 @@ # frozen_string_literal: true module RailsDataMigrations - class LogEntry < ::ActiveRecord::SchemaMigration - class << self - def table_name - "#{ActiveRecord::Base.table_name_prefix}data_migrations#{ActiveRecord::Base.table_name_suffix}" + module SharedMethods + def table_name + "#{ActiveRecord::Base.table_name_prefix}data_migrations#{ActiveRecord::Base.table_name_suffix}" + end + + def index_name + "#{table_name_prefix}unique_data_migrations#{table_name_suffix}" + end + end + + if Gem::Version.new('7.1.0') >= Gem::Version.new(::ActiveRecord.version) + class LogEntry < ::ActiveRecord::SchemaMigration + class << self + include SharedMethods end + end + else + class LogEntry < ::ActiveRecord::Base + class << self + include SharedMethods + def create_table + ::ActiveRecord::SchemaMigration.define_method(:table_name) do + "#{::ActiveRecord::Base.table_name_prefix}data_migrations#{::ActiveRecord::Base.table_name_suffix}" + end - def index_name - "#{table_name_prefix}unique_data_migrations#{table_name_suffix}" + ::ActiveRecord::Base.connection.schema_migration.create_table + end end end end diff --git a/lib/rails_data_migrations/migrator.rb b/lib/rails_data_migrations/migrator.rb index 1234fcf1..ef8e1a64 100644 --- a/lib/rails_data_migrations/migrator.rb +++ b/lib/rails_data_migrations/migrator.rb @@ -48,8 +48,16 @@ def rails_5_2? Rails::VERSION::MAJOR > 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2) end + def rails_7_1? + Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1 + end + def list_migrations - if rails_6_0? + if rails_7_1? + ::ActiveRecord::MigrationContext.new( + migrations_path, ::ActiveRecord::Base.connection.schema_migration + ).migrations + elsif rails_6_0? ::ActiveRecord::MigrationContext.new(migrations_path, ::ActiveRecord::SchemaMigration).migrations elsif rails_5_2? ::ActiveRecord::MigrationContext.new(migrations_path).migrations @@ -68,7 +76,15 @@ def list_pending_migrations end def run_migration(direction, migrations_path, version) - if rails_6_0? + if rails_7_1? + new( + direction, + list_migrations, + ::ActiveRecord::Base.connection.schema_migration, + ::ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection), + version + ).run + elsif rails_6_0? new(direction, list_migrations, ::ActiveRecord::SchemaMigration, version).run elsif rails_5_2? new(direction, list_migrations, version).run diff --git a/lib/rails_data_migrations/version.rb b/lib/rails_data_migrations/version.rb index 9cf916d0..5e3834ec 100644 --- a/lib/rails_data_migrations/version.rb +++ b/lib/rails_data_migrations/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RailsDataMigrations - VERSION = '1.2.0' + VERSION = '1.3.0' end