diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 329c810f..d145bbc9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3] + ruby-version: [3.1, 3.2, 3.3] steps: - name: Cancel previous runs diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..be94e6f5 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.2.2 diff --git a/Appraisals b/Appraisals index be56edeb..fd602a01 100644 --- a/Appraisals +++ b/Appraisals @@ -2,53 +2,9 @@ ruby_version = Gem::Version.new(RUBY_VERSION) -if ruby_version < Gem::Version.new('2.7.0') - ['4.0.0', '4.1.0', '4.2.0'].each do |rails_version| - appraise "rails-#{rails_version}" do - gem 'rails', "~> #{rails_version}" - gem 'bigdecimal', '1.3.5' - gem 'sqlite3', '~> 1.3.6' - end - end -end - -if ruby_version < Gem::Version.new('3.0.0') - appraise 'rails-5.0' do - gem 'rails', '~> 5.0.0' - gem 'sqlite3', '~> 1.3.6' - end - - appraise 'rails-5.1' do - gem 'rails', '~> 5.1.0' - gem 'sqlite3', '~> 1.3.6' - end - - appraise 'rails-5.2' do - gem 'rails', '~> 5.2.0' - gem 'sqlite3', '~> 1.3.6' - end -end - -if ruby_version >= Gem::Version.new('2.5.0') - appraise 'rails-6.0' do - gem 'rails', '~> 6.0.0' - gem 'sqlite3', '~> 1.4.0' - end - - appraise 'rails-6.1' do - gem 'rails', '~> 6.1.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' +if ruby_version >= Gem::Version.new('3.1.0') + appraise 'rails-7.2' do + gem 'rails', '~> 7.2.0' gem 'sqlite3', '~> 1.4.0' end end diff --git a/README.md b/README.md index 86e70a04..93712c92 100644 --- a/README.md +++ b/README.md @@ -43,17 +43,16 @@ You can change the migration folder by setting the DATA_MIGRATIONS_PATH environm ```ruby DATA_MIGRATIONS_PATH=new/path ``` -You can also change the table and index names by setting the corresponding environment variables: +You can also change the table name by setting the corresponding environment variables: ```ruby DATA_MIGRATIONS_TABLE_NAME=new_table_name -DATA_MIGRATIONS_INDEX_NAME=new_index_name ``` This can enable more complex use cases, such as data migrations intended for different contexts, or running multiple sets of data migrations in a specific order. It may also avoid conflict with existing path or table names ## Rails Support -Rails 4.0 and higher +Rails 7.2 and higher ## Installation diff --git a/lib/rails_data_migrations/log_entry.rb b/lib/rails_data_migrations/log_entry.rb index 0b6a4030..a7b07cf5 100644 --- a/lib/rails_data_migrations/log_entry.rb +++ b/lib/rails_data_migrations/log_entry.rb @@ -1,36 +1,21 @@ # frozen_string_literal: true module RailsDataMigrations - module SharedMethods - def table_name - tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations') - "#{ActiveRecord::Base.table_name_prefix}#{tbl_name}#{ActiveRecord::Base.table_name_suffix}" + class LogEntry < ::ActiveRecord::Base + def self.table_name + ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations') end - def index_name - "#{table_name_prefix}#{ENV.fetch('DATA_MIGRATIONS_INDEX_NAME', 'unique_data_migrations')}#{table_name_suffix}" + def self.create_table + schema_migration_instance.create_table 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 - tbl_name = ENV.fetch('DATA_MIGRATIONS_TABLE_NAME', 'data_migrations') - "#{::ActiveRecord::Base.table_name_prefix}#{tbl_name}#{::ActiveRecord::Base.table_name_suffix}" - end - ::ActiveRecord::Base.connection.schema_migration.create_table - end - end + def self.schema_migration_instance + connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool + schema_migration = ActiveRecord::SchemaMigration.new(connection_pool) + schema_migration.define_singleton_method(:table_name) { LogEntry.table_name } + schema_migration.instance_variable_set(:@arel_table, Arel::Table.new(LogEntry.table_name)) + schema_migration end end -end \ No newline at end of file +end diff --git a/lib/rails_data_migrations/migrator.rb b/lib/rails_data_migrations/migrator.rb index 51b71049..48d96c6c 100644 --- a/lib/rails_data_migrations/migrator.rb +++ b/lib/rails_data_migrations/migrator.rb @@ -2,6 +2,8 @@ module RailsDataMigrations class Migrator < ::ActiveRecord::Migrator + self.migrations_paths = [ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations')] + MIGRATOR_SALT = 2053462855 def record_version_state_after_migrating(version) @@ -15,13 +17,8 @@ def record_version_state_after_migrating(version) end class << self - def migrations_table_exists?(connection = ActiveRecord::Base.connection) - table_check_method = connection.respond_to?(:data_source_exists?) ? :data_source_exists? : :table_exists? - connection.send(table_check_method, schema_migrations_table_name) - end - - def get_all_versions(connection = ActiveRecord::Base.connection) - if migrations_table_exists?(connection) + def get_all_versions # rubocop:disable Naming/AccessorMethodName + if LogEntry.table_exists? LogEntry.all.map { |x| x.version.to_i }.sort else [] @@ -36,61 +33,25 @@ def schema_migrations_table_name LogEntry.table_name end - def migrations_path - ENV.fetch('DATA_MIGRATIONS_PATH', 'db/data_migrations') - end - - def rails_6_0? - Rails::VERSION::MAJOR >= 6 - end - - 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_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 - else - migrations(migrations_path) - end + ::ActiveRecord::MigrationContext.new(migrations_path).migrations end def list_pending_migrations - if rails_5_2? - already_migrated = get_all_versions - list_migrations.reject { |m| already_migrated.include?(m.version) } - else - open(migrations_path).pending_migrations # rubocop:disable Security/Open - end - end - - def run_migration(direction, migrations_path, version) - 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 - else - run(direction, migrations_path, version) - end + already_migrated = get_all_versions + list_migrations.reject { |m| already_migrated.include?(m.version) } + end + + def run_migration(direction, version) + connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool + internal_metadata = ActiveRecord::InternalMetadata.new(connection_pool) + new( + direction, + list_migrations, + LogEntry.schema_migration_instance, + internal_metadata, + version + ).run end end end diff --git a/lib/rails_data_migrations/version.rb b/lib/rails_data_migrations/version.rb index ea18f867..236b0b84 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.3.0.1' + VERSION = '1.3.0.2' end diff --git a/lib/tasks/data_migrations.rake b/lib/tasks/data_migrations.rake index 0f4286e9..c6a04dc2 100644 --- a/lib/tasks/data_migrations.rake +++ b/lib/tasks/data_migrations.rake @@ -6,11 +6,7 @@ namespace :data do def apply_single_migration(direction, version) raise 'VERSION is required' unless version - RailsDataMigrations::Migrator.run_migration( - direction, - RailsDataMigrations::Migrator.migrations_path, - version.to_i - ) + RailsDataMigrations::Migrator.run_migration(direction, version.to_i) end task init_migration: :environment do diff --git a/rails-data-migrations.gemspec b/rails-data-migrations.gemspec index 1d65ad62..540adc77 100644 --- a/rails-data-migrations.gemspec +++ b/rails-data-migrations.gemspec @@ -19,9 +19,9 @@ Gem::Specification.new do |spec| end spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.required_ruby_version = '>= 2.3' + spec.required_ruby_version = '>= 3.1.0' - spec.add_runtime_dependency 'rails', '>= 4.0.0' + spec.add_runtime_dependency 'rails', '>= 7.2.0' spec.add_development_dependency 'appraisal', '~> 2.1' spec.add_development_dependency 'rake', '>= 12.3.3' diff --git a/spec/data_migrations_spec.rb b/spec/data_migrations_spec.rb index 62fa716f..3fc045a3 100644 --- a/spec/data_migrations_spec.rb +++ b/spec/data_migrations_spec.rb @@ -4,7 +4,6 @@ describe RailsDataMigrations do it 'checks for migration log table existence' do - expect(RailsDataMigrations::Migrator.migrations_table_exists?(ActiveRecord::Base.connection)).to be_truthy expect(RailsDataMigrations::Migrator.get_all_versions).to be_blank end