diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index de73c90..c31c416 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,6 @@ jobs: matrix: os: [ "ubuntu-20.04" ] ruby: - - '3.0' - '3.1' - '3.2' - '3.3' @@ -19,6 +18,7 @@ jobs: - gemfiles/rails_6.1.gemfile - gemfiles/rails_7.0.gemfile - gemfiles/rails_7.1.gemfile + - gemfiles/rails_7.2.gemfile runs-on: ubuntu-latest env: BUNDLE_GEMFILE: ${{ matrix.gemfile }} diff --git a/Appraisals b/Appraisals index 7eec0af..4fba715 100644 --- a/Appraisals +++ b/Appraisals @@ -9,3 +9,7 @@ end appraise 'rails-7.1' do gem 'rails', '7.1.0' end + +appraise 'rails-7.2' do + gem 'rails', '~> 7.2.0' +end diff --git a/README.md b/README.md index e6aca12..045ec92 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ table to track all migrations. ## Rails Support -Support Rails 6.1 through 7.1 +Support Rails 6.1 through 7.2 #### v1 @@ -167,6 +167,7 @@ bundle exec appraisal install bundle exec appraisal rails-6.1 rspec bundle exec appraisal rails-7.0 rspec bundle exec appraisal rails-7.1 rspec +bundle exec appraisal rails-7.2 rspec ``` ## Thanks diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile new file mode 100644 index 0000000..e107c0e --- /dev/null +++ b/gemfiles/rails_7.2.gemfile @@ -0,0 +1,7 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rails", "7.2.0" + +gemspec path: "../" diff --git a/gemfiles/rails_7.2.gemfile.lock b/gemfiles/rails_7.2.gemfile.lock new file mode 100644 index 0000000..35e2b11 --- /dev/null +++ b/gemfiles/rails_7.2.gemfile.lock @@ -0,0 +1,257 @@ +PATH + remote: .. + specs: + data_migrate (9.4.1) + activerecord (>= 6.1) + railties (>= 6.1) + +GEM + remote: http://rubygems.org/ + specs: + actioncable (7.2.0) + actionpack (= 7.2.0) + activesupport (= 7.2.0) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (7.2.0) + actionpack (= 7.2.0) + activejob (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) + mail (>= 2.8.0) + actionmailer (7.2.0) + actionpack (= 7.2.0) + actionview (= 7.2.0) + activejob (= 7.2.0) + activesupport (= 7.2.0) + mail (>= 2.8.0) + rails-dom-testing (~> 2.2) + actionpack (7.2.0) + actionview (= 7.2.0) + activesupport (= 7.2.0) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4, < 3.2) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + useragent (~> 0.16) + actiontext (7.2.0) + actionpack (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.2.0) + activesupport (= 7.2.0) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (7.2.0) + activesupport (= 7.2.0) + globalid (>= 0.3.6) + activemodel (7.2.0) + activesupport (= 7.2.0) + activerecord (7.2.0) + activemodel (= 7.2.0) + activesupport (= 7.2.0) + timeout (>= 0.4.0) + activestorage (7.2.0) + actionpack (= 7.2.0) + activejob (= 7.2.0) + activerecord (= 7.2.0) + activesupport (= 7.2.0) + marcel (~> 1.0) + activesupport (7.2.0) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.3.1) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + logger (>= 1.4.2) + minitest (>= 5.1) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.8) + builder (3.3.0) + childprocess (5.0.0) + coderay (1.1.3) + concurrent-ruby (1.3.4) + connection_pool (2.4.1) + crass (1.0.6) + date (3.3.4) + diff-lcs (1.5.1) + drb (2.2.1) + erubi (1.13.0) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.5) + concurrent-ruby (~> 1.0) + iniparse (1.5.0) + io-console (0.7.2) + irb (1.14.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + json (2.7.2) + language_server-protocol (3.17.0.3) + logger (1.6.0) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + method_source (1.1.0) + mini_mime (1.1.5) + minitest (5.25.0) + net-imap (0.4.14) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.0) + net-protocol + nio4r (2.7.3) + nokogiri (1.16.7-arm64-darwin) + racc (~> 1.4) + overcommit (0.63.0) + childprocess (>= 0.6.3, < 6) + iniparse (~> 1.4) + rexml (~> 3.2) + parallel (1.25.1) + parser (3.3.4.0) + ast (~> 2.4.1) + racc + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + psych (5.1.2) + stringio + racc (1.8.1) + rack (3.1.7) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails (7.2.0) + actioncable (= 7.2.0) + actionmailbox (= 7.2.0) + actionmailer (= 7.2.0) + actionpack (= 7.2.0) + actiontext (= 7.2.0) + actionview (= 7.2.0) + activejob (= 7.2.0) + activemodel (= 7.2.0) + activerecord (= 7.2.0) + activestorage (= 7.2.0) + activesupport (= 7.2.0) + bundler (>= 1.15.0) + railties (= 7.2.0) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (7.2.0) + actionpack (= 7.2.0) + activesupport (= 7.2.0) + irb (~> 1.13) + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rainbow (3.1.1) + rake (13.2.1) + rb-readline (0.5.5) + rdoc (6.7.0) + psych (>= 4.0.0) + regexp_parser (2.9.2) + reline (0.5.9) + io-console (~> 0.5) + rexml (3.3.4) + strscan + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + rubocop (1.64.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) + securerandom (0.3.1) + sqlite3 (1.7.3-arm64-darwin) + stringio (3.1.1) + strscan (3.1.0) + thor (1.3.1) + timecop (0.9.10) + timeout (0.4.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.5.0) + useragent (0.16.10) + webrick (1.8.1) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.6.17) + +PLATFORMS + arm64-darwin + +DEPENDENCIES + appraisal + data_migrate! + overcommit + pry + rails (= 7.2.0) + rake + rb-readline + rspec + rspec-core + rubocop + sqlite3 + timecop + +BUNDLED WITH + 2.5.9 diff --git a/lib/data_migrate/database_tasks.rb b/lib/data_migrate/database_tasks.rb index dee2c47..3b13fb5 100644 --- a/lib/data_migrate/database_tasks.rb +++ b/lib/data_migrate/database_tasks.rb @@ -10,21 +10,21 @@ module DatabaseTasks extend self # These method are only introduced in Rails 7.1 - unless respond_to?(:with_temporary_connection_for_each) - def with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc: + unless respond_to?(:with_temporary_pool_for_each) + def with_temporary_pool_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc: if name db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name) - with_temporary_connection(db_config, &block) + with_temporary_pool(db_config, &block) else ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config| - with_temporary_connection(db_config, &block) + with_temporary_pool(db_config, &block) end end end def with_temporary_connection(db_config) # :nodoc: with_temporary_pool(db_config) do |pool| - yield pool.connection + pool.with_connection end end @@ -33,7 +33,7 @@ def migration_class # :nodoc: end def migration_connection # :nodoc: - migration_class.connection + migration_class.lease_connection end private def with_temporary_pool(db_config) @@ -49,8 +49,8 @@ def migration_connection # :nodoc: def db_configs_with_versions db_configs_with_versions = Hash.new { |h, k| h[k] = [] } - with_temporary_connection_for_each do |conn| - db_config = conn.pool.db_config + with_temporary_pool_for_each do |pool| + db_config = pool.db_config if db_config.primary? versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] } target_version = ActiveRecord::Tasks::DatabaseTasks.target_version @@ -179,9 +179,7 @@ def self.migrate_with_data ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true - db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env) - - schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs) + schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs| diff --git a/lib/data_migrate/rails_helper.rb b/lib/data_migrate/rails_helper.rb index 9f94e73..d71ae44 100644 --- a/lib/data_migrate/rails_helper.rb +++ b/lib/data_migrate/rails_helper.rb @@ -1,6 +1,11 @@ module DataMigrate class RailsHelper class << self + def rails_version_equal_to_or_higher_than_7_2 + return @equal_to_or_higher_than_7_2 if defined?(@equal_to_or_higher_than_7_2) + + @equal_to_or_higher_than_7_2 = Gem::Dependency.new("railties", ">= 7.2.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true) + end def rails_version_equal_to_or_higher_than_7_1 return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1) @@ -14,7 +19,9 @@ def rails_version_equal_to_or_higher_than_7_0 end def internal_metadata - if rails_version_equal_to_or_higher_than_7_1 + if rails_version_equal_to_or_higher_than_7_2 + ActiveRecord::Base.connection_pool.internal_metadata + elsif rails_version_equal_to_or_higher_than_7_1 ActiveRecord::Base.connection.internal_metadata else ActiveRecord::InternalMetadata @@ -22,7 +29,9 @@ def internal_metadata end def schema_migration - if rails_version_equal_to_or_higher_than_7_1 + if rails_version_equal_to_or_higher_than_7_2 + ActiveRecord::Base.connection_pool.schema_migration + elsif rails_version_equal_to_or_higher_than_7_1 ActiveRecord::Base.connection.schema_migration else ActiveRecord::SchemaMigration @@ -54,7 +63,9 @@ def data_schema_delete_version(version) end def data_schema_migration - if rails_version_equal_to_or_higher_than_7_1 + if rails_version_equal_to_or_higher_than_7_2 + DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool) + elsif rails_version_equal_to_or_higher_than_7_1 DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection) else DataMigrate::DataSchemaMigration diff --git a/spec/data_migrate/migration_context_spec.rb b/spec/data_migrate/migration_context_spec.rb index c80155d..d995137 100644 --- a/spec/data_migrate/migration_context_spec.rb +++ b/spec/data_migrate/migration_context_spec.rb @@ -4,7 +4,7 @@ describe DataMigrate::DataMigrator do let(:context) { DataMigrate::MigrationContext.new("spec/db/data") } - let(:schema_context) { ActiveRecord::MigrationContext.new("spec/db/migrate", ActiveRecord::Base.connection.schema_migration) } + let(:schema_context) { ActiveRecord::MigrationContext.new("spec/db/migrate", ar_schema_migration) } let(:db_config) do { adapter: "sqlite3", @@ -105,4 +105,13 @@ expect(versions.count).to eq(0) end end + + # schema migration changed in Rails 7.2, from the connection to the pool object. + def ar_schema_migration + if ActiveRecord::Base.connection_pool.respond_to?(:schema_migration) + ActiveRecord::Base.connection_pool.schema_migration + else + ActiveRecord::Base.connection.schema_migration + end + end end