Skip to content

Commit 636cddf

Browse files
[ETX-221] Support Rails 7.2 - Part 2
[ETX-221] Support Rails 7.2 - Part 2
2 parents ead2b3a + 3d10cd9 commit 636cddf

File tree

10 files changed

+42
-145
lines changed

10 files changed

+42
-145
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3]
23+
ruby-version: [3.1, 3.2, 3.3]
2424

2525
steps:
2626
- name: Cancel previous runs

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.2.2

Appraisals

+3-47
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,9 @@
22

33
ruby_version = Gem::Version.new(RUBY_VERSION)
44

5-
if ruby_version < Gem::Version.new('2.7.0')
6-
['4.0.0', '4.1.0', '4.2.0'].each do |rails_version|
7-
appraise "rails-#{rails_version}" do
8-
gem 'rails', "~> #{rails_version}"
9-
gem 'bigdecimal', '1.3.5'
10-
gem 'sqlite3', '~> 1.3.6'
11-
end
12-
end
13-
end
14-
15-
if ruby_version < Gem::Version.new('3.0.0')
16-
appraise 'rails-5.0' do
17-
gem 'rails', '~> 5.0.0'
18-
gem 'sqlite3', '~> 1.3.6'
19-
end
20-
21-
appraise 'rails-5.1' do
22-
gem 'rails', '~> 5.1.0'
23-
gem 'sqlite3', '~> 1.3.6'
24-
end
25-
26-
appraise 'rails-5.2' do
27-
gem 'rails', '~> 5.2.0'
28-
gem 'sqlite3', '~> 1.3.6'
29-
end
30-
end
31-
32-
if ruby_version >= Gem::Version.new('2.5.0')
33-
appraise 'rails-6.0' do
34-
gem 'rails', '~> 6.0.0'
35-
gem 'sqlite3', '~> 1.4.0'
36-
end
37-
38-
appraise 'rails-6.1' do
39-
gem 'rails', '~> 6.1.0'
40-
gem 'sqlite3', '~> 1.4.0'
41-
end
42-
end
43-
44-
if ruby_version >= Gem::Version.new('2.7.0')
45-
appraise 'rails-7.0' do
46-
gem 'rails', '~> 7.0.0'
47-
gem 'sqlite3', '~> 1.4.0'
48-
end
49-
50-
appraise 'rails-7.1' do
51-
gem 'rails', '~> 7.1.0'
5+
if ruby_version >= Gem::Version.new('3.1.0')
6+
appraise 'rails-7.2' do
7+
gem 'rails', '~> 7.2.0'
528
gem 'sqlite3', '~> 1.4.0'
539
end
5410
end

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ You can change the migration folder by setting the DATA_MIGRATIONS_PATH environm
4343
```ruby
4444
DATA_MIGRATIONS_PATH=new/path
4545
```
46-
You can also change the table and index names by setting the corresponding environment variables:
46+
You can also change the table name by setting the corresponding environment variables:
4747
```ruby
4848
DATA_MIGRATIONS_TABLE_NAME=new_table_name
49-
DATA_MIGRATIONS_INDEX_NAME=new_index_name
5049
```
5150
This can enable more complex use cases, such as data migrations intended for different contexts,
5251
or running multiple sets of data migrations in a specific order. It may also avoid conflict with existing path or table names
5352

5453
## Rails Support
5554

56-
Rails 4.0 and higher
55+
Rails 7.2 and higher
5756

5857
## Installation
5958

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
11
# frozen_string_literal: true
22

33
module RailsDataMigrations
4-
module SharedMethods
5-
def table_name
6-
tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
7-
"#{ActiveRecord::Base.table_name_prefix}#{tbl_name}#{ActiveRecord::Base.table_name_suffix}"
4+
class LogEntry < ::ActiveRecord::Base
5+
def self.table_name
6+
ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
87
end
98

10-
def index_name
11-
"#{table_name_prefix}#{ENV.fetch('DATA_MIGRATIONS_INDEX_NAME', 'unique_data_migrations')}#{table_name_suffix}"
9+
def self.create_table
10+
schema_migration_instance.create_table
1211
end
13-
end
14-
15-
if Gem::Version.new('7.1.0') >= Gem::Version.new(::ActiveRecord.version)
16-
class LogEntry < ::ActiveRecord::SchemaMigration
17-
class << self
18-
include SharedMethods
19-
end
20-
end
21-
else
22-
class LogEntry < ::ActiveRecord::Base
23-
class << self
24-
include SharedMethods
25-
def create_table
26-
::ActiveRecord::SchemaMigration.define_method(:table_name) do
27-
tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations')
28-
"#{::ActiveRecord::Base.table_name_prefix}#{tbl_name}#{::ActiveRecord::Base.table_name_suffix}"
29-
end
3012

31-
::ActiveRecord::Base.connection.schema_migration.create_table
32-
end
33-
end
13+
def self.schema_migration_instance
14+
connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
15+
schema_migration = ActiveRecord::SchemaMigration.new(connection_pool)
16+
schema_migration.define_singleton_method(:table_name) { LogEntry.table_name }
17+
schema_migration.instance_variable_set(:@arel_table, Arel::Table.new(LogEntry.table_name))
18+
schema_migration
3419
end
3520
end
36-
end
21+
end

lib/rails_data_migrations/migrator.rb

+19-58
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module RailsDataMigrations
44
class Migrator < ::ActiveRecord::Migrator
5+
self.migrations_paths = [ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations')]
6+
57
MIGRATOR_SALT = 2053462855
68

79
def record_version_state_after_migrating(version)
@@ -15,13 +17,8 @@ def record_version_state_after_migrating(version)
1517
end
1618

1719
class << self
18-
def migrations_table_exists?(connection = ActiveRecord::Base.connection)
19-
table_check_method = connection.respond_to?(:data_source_exists?) ? :data_source_exists? : :table_exists?
20-
connection.send(table_check_method, schema_migrations_table_name)
21-
end
22-
23-
def get_all_versions(connection = ActiveRecord::Base.connection)
24-
if migrations_table_exists?(connection)
20+
def get_all_versions # rubocop:disable Naming/AccessorMethodName
21+
if LogEntry.table_exists?
2522
LogEntry.all.map { |x| x.version.to_i }.sort
2623
else
2724
[]
@@ -36,61 +33,25 @@ def schema_migrations_table_name
3633
LogEntry.table_name
3734
end
3835

39-
def migrations_path
40-
ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations')
41-
end
42-
43-
def rails_6_0?
44-
Rails::VERSION::MAJOR >= 6
45-
end
46-
47-
def rails_5_2?
48-
Rails::VERSION::MAJOR > 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 2)
49-
end
50-
51-
def rails_7_1?
52-
Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1
53-
end
54-
5536
def list_migrations
56-
if rails_7_1?
57-
::ActiveRecord::MigrationContext.new(
58-
migrations_path, ::ActiveRecord::Base.connection.schema_migration
59-
).migrations
60-
elsif rails_6_0?
61-
::ActiveRecord::MigrationContext.new(migrations_path, ::ActiveRecord::SchemaMigration).migrations
62-
elsif rails_5_2?
63-
::ActiveRecord::MigrationContext.new(migrations_path).migrations
64-
else
65-
migrations(migrations_path)
66-
end
37+
::ActiveRecord::MigrationContext.new(migrations_path).migrations
6738
end
6839

6940
def list_pending_migrations
70-
if rails_5_2?
71-
already_migrated = get_all_versions
72-
list_migrations.reject { |m| already_migrated.include?(m.version) }
73-
else
74-
open(migrations_path).pending_migrations # rubocop:disable Security/Open
75-
end
76-
end
77-
78-
def run_migration(direction, migrations_path, version)
79-
if rails_7_1?
80-
new(
81-
direction,
82-
list_migrations,
83-
::ActiveRecord::Base.connection.schema_migration,
84-
::ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection),
85-
version
86-
).run
87-
elsif rails_6_0?
88-
new(direction, list_migrations, ::ActiveRecord::SchemaMigration, version).run
89-
elsif rails_5_2?
90-
new(direction, list_migrations, version).run
91-
else
92-
run(direction, migrations_path, version)
93-
end
41+
already_migrated = get_all_versions
42+
list_migrations.reject { |m| already_migrated.include?(m.version) }
43+
end
44+
45+
def run_migration(direction, version)
46+
connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
47+
internal_metadata = ActiveRecord::InternalMetadata.new(connection_pool)
48+
new(
49+
direction,
50+
list_migrations,
51+
LogEntry.schema_migration_instance,
52+
internal_metadata,
53+
version
54+
).run
9455
end
9556
end
9657
end

lib/rails_data_migrations/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RailsDataMigrations
4-
VERSION = '1.3.0.1'
4+
VERSION = '1.3.0.2'
55
end

lib/tasks/data_migrations.rake

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ namespace :data do
66
def apply_single_migration(direction, version)
77
raise 'VERSION is required' unless version
88

9-
RailsDataMigrations::Migrator.run_migration(
10-
direction,
11-
RailsDataMigrations::Migrator.migrations_path,
12-
version.to_i
13-
)
9+
RailsDataMigrations::Migrator.run_migration(direction, version.to_i)
1410
end
1511

1612
task init_migration: :environment do

rails-data-migrations.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Gem::Specification.new do |spec|
1919
end
2020
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2121
spec.require_paths = ['lib']
22-
spec.required_ruby_version = '>= 2.3'
22+
spec.required_ruby_version = '>= 3.1.0'
2323

24-
spec.add_runtime_dependency 'rails', '>= 4.0.0'
24+
spec.add_runtime_dependency 'rails', '>= 7.2.0'
2525

2626
spec.add_development_dependency 'appraisal', '~> 2.1'
2727
spec.add_development_dependency 'rake', '>= 12.3.3'

spec/data_migrations_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
describe RailsDataMigrations do
66
it 'checks for migration log table existence' do
7-
expect(RailsDataMigrations::Migrator.migrations_table_exists?(ActiveRecord::Base.connection)).to be_truthy
87
expect(RailsDataMigrations::Migrator.get_all_versions).to be_blank
98
end
109

0 commit comments

Comments
 (0)