Skip to content

Commit

Permalink
Support active record schema migration changes introduced with Rails …
Browse files Browse the repository at this point in the history
…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 <sergey.glukhov@gmail.com>
  • Loading branch information
hernanvicente and serggl authored May 20, 2024
1 parent 3dd3d60 commit 727f933
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 25 additions & 6 deletions lib/rails_data_migrations/log_entry.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 18 additions & 2 deletions lib/rails_data_migrations/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_data_migrations/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RailsDataMigrations
VERSION = '1.2.0'
VERSION = '1.3.0'
end

0 comments on commit 727f933

Please sign in to comment.