From 8eac247ceab05ab8ebc6123b7e65c928f5f35c63 Mon Sep 17 00:00:00 2001 From: Leo Arnold Date: Tue, 19 Nov 2024 19:37:00 +0100 Subject: [PATCH] Add support for Rails v8 --- .github/workflows/ci.yml | 8 ++++---- spec/scenic/schema_dumper_spec.rb | 23 ++++++++++++++--------- spec/spec_helper.rb | 19 +++++++++++++++---- spec/support/database_schema_helpers.rb | 10 ++++++++++ 4 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 spec/support/database_schema_helpers.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ccbc7c1..ceac5bca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run standardrb uses: standardrb/standard-ruby-action@f533e61f461ccb766b2d9c235abf59be02aea793 @@ -30,8 +30,8 @@ jobs: strategy: fail-fast: false matrix: - ruby: ["3.1", "3.2"] - rails: ["6.1", "7.0"] + ruby: ["3.2", "3.3"] + rails: ["7.1", "7.2", "8.0"] continue-on-error: [false] runs-on: ubuntu-latest @@ -57,7 +57,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install Ruby ${{ matrix.ruby }} uses: ruby/setup-ruby@v1 diff --git a/spec/scenic/schema_dumper_spec.rb b/spec/scenic/schema_dumper_spec.rb index 02378110..03eb8bee 100644 --- a/spec/scenic/schema_dumper_spec.rb +++ b/spec/scenic/schema_dumper_spec.rb @@ -12,7 +12,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :searches, sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string @@ -31,7 +31,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :searches, sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string expect(output).to include "~ '\\\\d+'::text" @@ -47,7 +47,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :searches, materialized: true, sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string @@ -62,7 +62,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :"scenic.searches", sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string expect(output).to include 'create_view "scenic.searches",' @@ -77,7 +77,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.execute("CREATE OR REPLACE VIEW scenic.apples AS SELECT * FROM scenic.bananas;") stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) views = stream.string.lines.grep(/create_view/).map do |view_line| view_line.match('create_view "(?.*)"')[:name] end @@ -93,7 +93,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :a_searches_z, sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string @@ -106,7 +106,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view :searches, sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string @@ -121,7 +121,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.create_view '"search in a haystack"', sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string expect(output).to include 'create_view "\"search in a haystack\"",' @@ -145,7 +145,7 @@ class SearchInAHaystack < ActiveRecord::Base sql_definition: view_definition stream = StringIO.new - ActiveRecord::SchemaDumper.dump(Search.connection, stream) + dump_schema(stream) output = stream.string expect(output).to include 'create_view "scenic.\"search in a haystack\"",' @@ -153,6 +153,11 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.drop_view :'scenic."search in a haystack"' + case ActiveRecord.gem_version + when Gem::Requirement.new(">= 7.1") + Search.connection.drop_schema "scenic" + end + silence_stream($stdout) { eval(output) } # standard:disable Security/Eval expect(SearchInAHaystack.take.haystack).to eq "needle" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b3dfc3a9..c0ec85d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,18 +2,29 @@ require "database_cleaner" require File.expand_path("dummy/config/environment", __dir__) -require "support/rails_configuration_helpers" -require "support/generator_spec_setup" -require "support/view_definition_helpers" + +Dir.glob("#{__dir__}/support/**/*.rb").each { |f| require f } RSpec.configure do |config| config.order = "random" + config.include DatabaseSchemaHelpers config.include ViewDefinitionHelpers config.include RailsConfigurationHelpers DatabaseCleaner.strategy = :transaction config.around(:each, db: true) do |example| - ActiveRecord::SchemaMigration.create_table + case ActiveRecord.gem_version + when Gem::Requirement.new(">= 7.2") + ActiveRecord::SchemaMigration + .new(ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool) + .create_table + when Gem::Requirement.new("~> 7.1.0") + ActiveRecord::SchemaMigration + .new(ActiveRecord::Tasks::DatabaseTasks.migration_connection) + .create_table + when Gem::Requirement.new("< 7.1") + ActiveRecord::SchemaMigration.create_table + end DatabaseCleaner.start example.run diff --git a/spec/support/database_schema_helpers.rb b/spec/support/database_schema_helpers.rb new file mode 100644 index 00000000..6ac553bc --- /dev/null +++ b/spec/support/database_schema_helpers.rb @@ -0,0 +1,10 @@ +module DatabaseSchemaHelpers + def dump_schema(stream) + case ActiveRecord.gem_version + when Gem::Requirement.new(">= 7.2") + ActiveRecord::SchemaDumper.dump(Search.connection_pool, stream) + else + ActiveRecord::SchemaDumper.dump(Search.connection, stream) + end + end +end