From 494491ef80da7529a52496520e875481b996d30a Mon Sep 17 00:00:00 2001
From: z <zumkehr@puzzle.ch>
Date: Tue, 3 Dec 2024 16:38:20 +0100
Subject: [PATCH] Remove support for old rails versions (#2)

---
 .circleci/config.yml                          |  7 +-
 .github/workflows/main.yml                    | 10 +--
 Appraisals                                    | 24 +++---
 gemfiles/rails_6_1.gemfile                    | 17 -----
 gemfiles/rails_8_0.gemfile                    | 17 +++++
 lib/apartment.rb                              | 22 ++----
 puzzle-apartment.gemspec                      |  8 +-
 spec/adapters/sqlite3_adapter_spec.rb         |  3 +-
 .../apartment_rake_integration_spec.rb        | 71 ++++++------------
 spec/unit/migrator_spec.rb                    | 74 +++++--------------
 10 files changed, 89 insertions(+), 164 deletions(-)
 delete mode 100644 gemfiles/rails_6_1.gemfile
 create mode 100644 gemfiles/rails_8_0.gemfile

diff --git a/.circleci/config.yml b/.circleci/config.yml
index 151b98ff..0cf647ea 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -62,11 +62,14 @@ workflows:
       - build:
           matrix:
             parameters:
-              ruby_version: ["ruby:3.0.6", "ruby:3.1.4", "ruby:3.2.2"]
+              ruby_version: ["ruby:3.1.4", "ruby:3.2.2", "ruby:3.3.6"]
               gemfile:
                 [
-                  "gemfiles/rails_6_1.gemfile",
                   "gemfiles/rails_7_0.gemfile",
                   "gemfiles/rails_7_1.gemfile",
                   "gemfiles/rails_7_2.gemfile",
+                  "gemfiles/rails_8_0.gemfile",
                 ]
+            exclude:
+              - ruby_version: "ruby:3.1.4"
+                gemfile: "gemfiles/rails_8_0.gemfile"
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index feabdc00..153d953a 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -20,18 +20,14 @@ jobs:
           - 3.1
           - 3.2
           - 3.3
-          - jruby
         rails_version:
-          - 6_1
           - 7_0
           - 7_1
           - 7_2
-          # - master # versions failing
+          - 8_0
         exclude:
-          - ruby_version: jruby
-            rails_version: 7_1
-          - ruby_version: jruby
-            rails_version: 7_2
+          - rails_version: 8_0
+            ruby_version: 3.1
     env:
       BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails_version }}.gemfile
       CI: true
diff --git a/Appraisals b/Appraisals
index b3fc60c9..7e8d12ca 100644
--- a/Appraisals
+++ b/Appraisals
@@ -1,17 +1,5 @@
 # frozen_string_literal: true
 
-appraise 'rails-6-1' do
-  gem 'rails', '~> 6.1.0'
-  platforms :ruby do
-    gem 'sqlite3', '~> 1.4'
-  end
-  platforms :jruby do
-    gem 'activerecord-jdbc-adapter', '~> 61.0'
-    gem 'activerecord-jdbcpostgresql-adapter', '~> 61.0'
-    gem 'activerecord-jdbcmysql-adapter', '~> 61.0'
-  end
-end
-
 appraise 'rails-7-0' do
   gem 'rails', '~> 7.0.0'
   platforms :ruby do
@@ -48,6 +36,18 @@ appraise 'rails-7-2' do
   end
 end
 
+appraise 'rails-8-0' do
+  gem 'rails', '~> 8.0.0'
+  platforms :ruby do
+    gem 'sqlite3', '~> 2.0'
+  end
+  platforms :jruby do
+    gem 'activerecord-jdbc-adapter', '~> 70.0'
+    gem 'activerecord-jdbcpostgresql-adapter', '~> 70.0'
+    gem 'activerecord-jdbcmysql-adapter', '~> 70.0'
+  end
+end
+
 # Install Rails from the main branch are failing
 # appraise 'rails-master' do
 #   gem 'rails', git: 'https://github.com/rails/rails.git'
diff --git a/gemfiles/rails_6_1.gemfile b/gemfiles/rails_6_1.gemfile
deleted file mode 100644
index ef48f142..00000000
--- a/gemfiles/rails_6_1.gemfile
+++ /dev/null
@@ -1,17 +0,0 @@
-# This file was generated by Appraisal
-
-source "http://rubygems.org"
-
-gem "rails", "~> 6.1.0"
-
-platforms :ruby do
-  gem "sqlite3", "~> 1.4"
-end
-
-platforms :jruby do
-  gem "activerecord-jdbc-adapter", "~> 61.0"
-  gem "activerecord-jdbcpostgresql-adapter", "~> 61.0"
-  gem "activerecord-jdbcmysql-adapter", "~> 61.0"
-end
-
-gemspec path: "../"
diff --git a/gemfiles/rails_8_0.gemfile b/gemfiles/rails_8_0.gemfile
new file mode 100644
index 00000000..6704eb53
--- /dev/null
+++ b/gemfiles/rails_8_0.gemfile
@@ -0,0 +1,17 @@
+# This file was generated by Appraisal
+
+source "http://rubygems.org"
+
+gem "rails", "~> 8.0.0"
+
+platforms :ruby do
+  gem "sqlite3", "~> 2.0"
+end
+
+platforms :jruby do
+  gem 'activerecord-jdbc-adapter', '~> 70.0'
+  gem "activerecord-jdbcpostgresql-adapter", "~> 70.0"
+  gem "activerecord-jdbcmysql-adapter", "~> 70.0"
+end
+
+gemspec path: "../"
diff --git a/lib/apartment.rb b/lib/apartment.rb
index 9d79a81e..1a452bfb 100644
--- a/lib/apartment.rb
+++ b/lib/apartment.rb
@@ -7,15 +7,9 @@
 require 'apartment/tenant'
 
 require_relative 'apartment/log_subscriber'
-
-if ActiveRecord.version.release >= Gem::Version.new('6.0')
-  require_relative 'apartment/active_record/connection_handling'
-end
-
-if ActiveRecord.version.release >= Gem::Version.new('6.1')
-  require_relative 'apartment/active_record/schema_migration'
-  require_relative 'apartment/active_record/internal_metadata'
-end
+require_relative 'apartment/active_record/connection_handling'
+require_relative 'apartment/active_record/schema_migration'
+require_relative 'apartment/active_record/internal_metadata'
 
 if ActiveRecord.version.release >= Gem::Version.new('7.1')
   require_relative 'apartment/active_record/postgres/schema_dumper'
@@ -37,14 +31,10 @@ class << self
     attr_accessor(*ACCESSOR_METHODS)
     attr_writer(*WRITER_METHODS)
 
-    if ActiveRecord.version.release >= Gem::Version.new('6.1')
-      def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
+    def_delegators :connection_class, :connection, :connection_db_config, :establish_connection
 
-      def connection_config
-        connection_db_config.configuration_hash
-      end
-    else
-      def_delegators :connection_class, :connection, :connection_config, :establish_connection
+    def connection_config
+      connection_db_config.configuration_hash
     end
 
     # configure apartment with available options
diff --git a/puzzle-apartment.gemspec b/puzzle-apartment.gemspec
index 5c53bdda..988dab91 100644
--- a/puzzle-apartment.gemspec
+++ b/puzzle-apartment.gemspec
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
 
   s.required_ruby_version = '>= 3.1', '<= 3.4'
 
-  s.add_dependency 'activerecord', '>= 6.1.0', '<= 8.1'
+  s.add_dependency 'activerecord', '>= 7.0.0', '<= 8.1'
   s.add_dependency 'parallel', '< 2.0'
   s.add_dependency 'public_suffix', '>= 2.0.5', '<= 6.0.1'
   s.add_dependency 'rack', '>= 1.3.6', '< 4.0'
@@ -57,8 +57,8 @@ Gem::Specification.new do |s|
     s.add_development_dependency 'jdbc-mysql'
     s.add_development_dependency 'jdbc-postgres'
   else
-    s.add_development_dependency 'mysql2',  '~> 0.5'
-    s.add_development_dependency 'pg',      '~> 1.2'
-    s.add_development_dependency 'sqlite3', '~> 1.3.6'
+    s.add_development_dependency 'mysql2'
+    s.add_development_dependency 'pg'
+    s.add_development_dependency 'sqlite3'
   end
 end
diff --git a/spec/adapters/sqlite3_adapter_spec.rb b/spec/adapters/sqlite3_adapter_spec.rb
index 339cb38c..ee666c2a 100644
--- a/spec/adapters/sqlite3_adapter_spec.rb
+++ b/spec/adapters/sqlite3_adapter_spec.rb
@@ -24,7 +24,8 @@ def tenant_names
       it_behaves_like 'a connection based apartment adapter'
 
       after(:all) do
-        File.delete(Apartment::Test.config['connections']['sqlite']['database'])
+        db_file = Apartment::Test.config['connections']['sqlite']['database']
+        File.delete(db_file) if File.exist?(db_file)
       end
     end
 
diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb
index e9acfb10..d2aec82c 100644
--- a/spec/integration/apartment_rake_integration_spec.rb
+++ b/spec/integration/apartment_rake_integration_spec.rb
@@ -47,60 +47,31 @@
       Company.delete_all
     end
 
-    context 'with ActiveRecord below 5.2.0' do
-      before do
-        allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] }
-        allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
-      end
-
-      describe '#migrate' do
-        it 'should migrate all databases' do
-          expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times
-
-          @rake['apartment:migrate'].invoke
-        end
-      end
-
-      describe '#rollback' do
-        it 'should rollback all dbs' do
-          expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times
-
-          @rake['apartment:rollback'].invoke
-        end
+    let(:migration_context_double) { double(:migration_context) }
+
+    describe '#migrate' do
+      it 'should migrate all databases' do
+        if ActiveRecord.version >= Gem::Version.new('7.2.0')
+          allow(ActiveRecord::Base.connection_pool)
+        else
+          allow(ActiveRecord::Base.connection)
+        end.to receive(:migration_context) { migration_context_double }
+        expect(migration_context_double).to receive(:migrate).exactly(company_count).times
+
+        @rake['apartment:migrate'].invoke
       end
     end
 
-    context 'with ActiveRecord above or equal to 5.2.0' do
-      let(:migration_context_double) { double(:migration_context) }
-
-      before do
-        allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
-      end
-
-      describe '#migrate' do
-        it 'should migrate all databases' do
-          if ActiveRecord.version >= Gem::Version.new('7.2.0')
-            allow(ActiveRecord::Base.connection_pool)
-          else
-            allow(ActiveRecord::Base.connection)
-          end.to receive(:migration_context) { migration_context_double }
-          expect(migration_context_double).to receive(:migrate).exactly(company_count).times
-
-          @rake['apartment:migrate'].invoke
-        end
-      end
+    describe '#rollback' do
+      it 'should rollback all dbs' do
+        if ActiveRecord.version >= Gem::Version.new('7.2.0')
+          allow(ActiveRecord::Base.connection_pool)
+        else
+          allow(ActiveRecord::Base.connection)
+        end.to receive(:migration_context) { migration_context_double }
+        expect(migration_context_double).to receive(:rollback).exactly(company_count).times
 
-      describe '#rollback' do
-        it 'should rollback all dbs' do
-          if ActiveRecord.version >= Gem::Version.new('7.2.0')
-            allow(ActiveRecord::Base.connection_pool)
-          else
-            allow(ActiveRecord::Base.connection)
-          end.to receive(:migration_context) { migration_context_double }
-          expect(migration_context_double).to receive(:rollback).exactly(company_count).times
-
-          @rake['apartment:rollback'].invoke
-        end
+        @rake['apartment:rollback'].invoke
       end
     end
 
diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb
index 8cbcb9e2..ceb10444 100644
--- a/spec/unit/migrator_spec.rb
+++ b/spec/unit/migrator_spec.rb
@@ -9,70 +9,34 @@
   # Don't need a real switch here, just testing behaviour
   before { allow(Apartment::Tenant.adapter).to receive(:connect_to_new) }
 
-  context 'with ActiveRecord below 5.2.0', skip: ActiveRecord.version >= Gem::Version.new('5.2.0') do
-    before do
-      allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] }
-      allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
-    end
-
-    describe '::migrate' do
-      it 'switches and migrates' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect(ActiveRecord::Migrator).to receive(:migrate)
-
-        Apartment::Migrator.migrate(tenant)
-      end
-    end
-
-    describe '::run' do
-      it 'switches and runs' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect(ActiveRecord::Migrator).to receive(:run).with(:up, anything, 1234)
-
-        Apartment::Migrator.run(:up, tenant, 1234)
-      end
-    end
-
-    describe '::rollback' do
-      it 'switches and rolls back' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect(ActiveRecord::Migrator).to receive(:rollback).with(anything, 2)
-
-        Apartment::Migrator.rollback(tenant, 2)
-      end
-    end
+  before do
+    allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
   end
 
-  context 'with ActiveRecord above or equal to 5.2.0', skip: ActiveRecord.version < Gem::Version.new('5.2.0') do
-    before do
-      allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
-    end
+  describe '::migrate' do
+    it 'switches and migrates' do
+      expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
+      expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:migrate)
 
-    describe '::migrate' do
-      it 'switches and migrates' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:migrate)
-
-        Apartment::Migrator.migrate(tenant)
-      end
+      Apartment::Migrator.migrate(tenant)
     end
+  end
 
-    describe '::run' do
-      it 'switches and runs' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:run).with(:up, 1234)
+  describe '::run' do
+    it 'switches and runs' do
+      expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
+      expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:run).with(:up, 1234)
 
-        Apartment::Migrator.run(:up, tenant, 1234)
-      end
+      Apartment::Migrator.run(:up, tenant, 1234)
     end
+  end
 
-    describe '::rollback' do
-      it 'switches and rolls back' do
-        expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
-        expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:rollback).with(2)
+  describe '::rollback' do
+    it 'switches and rolls back' do
+      expect(Apartment::Tenant).to receive(:switch).with(tenant).and_call_original
+      expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:rollback).with(2)
 
-        Apartment::Migrator.rollback(tenant, 2)
-      end
+      Apartment::Migrator.rollback(tenant, 2)
     end
   end
 end