Skip to content

Commit 9c8308f

Browse files
[ETX-221] Support Rails 7.2
1 parent ead2b3a commit 9c8308f

File tree

6 files changed

+27
-137
lines changed

6 files changed

+27
-137
lines changed

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.2.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

+8-29
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
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 = ::ActiveRecord::Base.connection_pool.schema_migration
11+
schema_migration.define_singleton_method(:table_name) { ::RailsDataMigrations::LogEntry.table_name }
12+
schema_migration.create_table
1213
end
1314
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
30-
31-
::ActiveRecord::Base.connection.schema_migration.create_table
32-
end
33-
end
34-
end
35-
end
36-
end
15+
end

lib/rails_data_migrations/migrator.rb

+11-55
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
21+
if LogEntry.table_exists?
2522
LogEntry.all.map { |x| x.version.to_i }.sort
2623
else
2724
[]
@@ -36,61 +33,20 @@ 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
41+
already_migrated = get_all_versions
42+
list_migrations.reject { |m| already_migrated.include?(m.version) }
7643
end
7744

7845
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
46+
schema_migration = ::ActiveRecord::Base.connection_pool.schema_migration
47+
schema_migration.define_singleton_method(:table_name) { ::RailsDataMigrations::LogEntry.table_name }
48+
internal_metadata = ::ActiveRecord::Base.connection_pool.internal_metadata
49+
new(direction, list_migrations, schema_migration, internal_metadata, version).run
9450
end
9551
end
9652
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

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.2'
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'

0 commit comments

Comments
 (0)