From 7f290035432d078845159c47c25baf6706b4ee9b Mon Sep 17 00:00:00 2001 From: Vladislav Sokov <62177364+VladislavSokov@users.noreply.github.com> Date: Thu, 6 Jun 2024 19:46:07 +0300 Subject: [PATCH] Support latest edge rails (#59) * Support latest edge rails * fixup! Support latest edge rails * Support latest beta version rails * fixup! Support latest beta version rails --- .github/workflows/main.yml | 1 + .gitignore | 5 +++-- Appraisals | 6 ++++++ gemfiles/rails.edge.gemfile | 15 +++++++++++++++ lib/actual_db_schema/commands/base.rb | 17 +++++++++++++---- test/support/test_utils.rb | 19 ++++++++++++++----- 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 gemfiles/rails.edge.gemfile diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7900118..23dd412 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,6 +19,7 @@ jobs: - { ruby: '3.1', rails: '7.0' } - { ruby: '3.2', rails: '7.1' } - { ruby: '3.3', rails: '7.1' } + - { ruby: '3.3', rails: 'edge' } env: BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails.${{ matrix.rails }}.gemfile steps: diff --git a/.gitignore b/.gitignore index 320be80..ce1295a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,8 @@ /spec/reports/ /tmp/ /test/dummy_app/tmp/ -/test/dummy_app/db/migrate/*.rb -/test/dummy_app/db/schema.rb +/test/dummy_app/db/*/*.rb +/test/dummy_app/db/*.rb .ruby-version .ruby-gemset +/gemfiles/*.gemfile.lock diff --git a/Appraisals b/Appraisals index 53b1d0b..f813f34 100644 --- a/Appraisals +++ b/Appraisals @@ -6,3 +6,9 @@ gem "activesupport", "~> #{version}.0" end end + +appraise "rails.edge" do + gem "rails", ">= 7.2.0.beta" + gem "activerecord", ">= 7.2.0.beta" + gem "activesupport", ">= 7.2.0.beta" +end diff --git a/gemfiles/rails.edge.gemfile b/gemfiles/rails.edge.gemfile new file mode 100644 index 0000000..1be7aaa --- /dev/null +++ b/gemfiles/rails.edge.gemfile @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activerecord", ">= 7.2.0.beta" +gem "activesupport", ">= 7.2.0.beta" +gem "minitest", "~> 5.0" +gem "rake" +gem "rubocop", "~> 1.21" +gem "rails", ">= 7.2.0.beta" +gem "sqlite3" + +gemspec path: "../" diff --git a/lib/actual_db_schema/commands/base.rb b/lib/actual_db_schema/commands/base.rb index bd43026..055f35e 100644 --- a/lib/actual_db_schema/commands/base.rb +++ b/lib/actual_db_schema/commands/base.rb @@ -19,10 +19,19 @@ def call_impl end def context - @context ||= - ActiveRecord::Base.connection.migration_context.tap do |c| - c.extend(ActualDbSchema::Patches::MigrationContext) - end + @context ||= fetch_migration_context.tap do |c| + c.extend(ActualDbSchema::Patches::MigrationContext) + end + end + + def fetch_migration_context + ar_version = Gem::Version.new(ActiveRecord::VERSION::STRING) + if ar_version >= Gem::Version.new("7.2.0") || + (ar_version >= Gem::Version.new("7.1.0") && ar_version.prerelease?) + ActiveRecord::Base.connection_pool.migration_context + else + ActiveRecord::Base.connection.migration_context + end end end end diff --git a/test/support/test_utils.rb b/test/support/test_utils.rb index fd3fe1a..2586c3f 100644 --- a/test/support/test_utils.rb +++ b/test/support/test_utils.rb @@ -140,17 +140,26 @@ def migrated_files(db_config = nil) def cleanup_call(prefix_name = nil) delete_migrations_files(prefix_name) - if ActiveRecord::SchemaMigration.respond_to?(:create_table) - ActiveRecord::SchemaMigration.create_table - else - ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table - end + create_schema_migration_table run_sql("delete from schema_migrations") remove_app_dir(MIGRATED_PATHS.fetch(prefix_name&.to_sym, migrated_paths.first)) define_migrations(prefix_name) Rails.application.load_tasks end + def create_schema_migration_table + if ActiveRecord::SchemaMigration.respond_to?(:create_table) + ActiveRecord::SchemaMigration.create_table + else + ar_version = Gem::Version.new(ActiveRecord::VERSION::STRING) + if ar_version >= Gem::Version.new("7.2.0") || (ar_version >= Gem::Version.new("7.1.0") && ar_version.prerelease?) + ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection_pool).create_table + else + ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table + end + end + end + def delete_migrations_files_for(path) Dir.glob(app_file("#{path}/**/*.rb")).each do |file| remove_app_dir(file)