Skip to content

Commit 828f1ab

Browse files
committed
BACKUP - currently getting rails 8.1 to switch intelligently between trilogy, right now it will always use trilogy if it is present in the gemfile
1 parent fcabff4 commit 828f1ab

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,13 @@ All configuration options are configurable from the `Departure.configure` block
154154
| Option | Default | What it Controls |
155155
|-----------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
156156
| disable_rails_advisory_lock_patch | false | When truthy, disables a patch in at least rails 7.1 and 7.2 where rails throws ConcurrentMigrationErrors due to the inability to release the advisory lock in migrations |
157-
| db_adapter_name | nil | 'mysql2' or 'percona' - gives users an ability to provide the specific db adapter to override the db_adapter we use in departure see Db Adapter Support Below for details |
158157

159-
### Db Adapter Support
158+
### Trilogy Adapter Support
160159

161160
Starting in Rails 8.1 we add support for the use of the trilogy database adapter gem. Logic for selecting an adapter follows this logic
162161

163-
1. Departure.configuration.db_adapter_name is set to 'mysql2' or 'trilogy' use that value
164-
2. If the database configuration specifies 'mysql2 or 'trilogy' use that adapter
165-
3. Default to mysql2
162+
1. If the database configuration specifies 'trilogy' use the trilogy adapter
163+
2. Default to mysql2
166164

167165
### Disable on per-migration basis
168166

lib/active_record/connection_adapters/rails_8_1_mysql2_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def visit_DropForeignKey(name) # rubocop:disable Naming/MethodName
4040
ADAPTER_NAME = 'Percona'.freeze
4141

4242
def self.new_client(config)
43+
binding.pry
4344
original_client = super
4445

4546
Departure::DbClient.new(config, original_client)

lib/departure/rails_adapter.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ def for_current(**args)
2424

2525
def for(ar_version, db_connection_adapter: nil)
2626
if ar_version::MAJOR == 8 && ar_version::MINOR.positive?
27-
case specifies_adapter(db_connection_adapter)
28-
when 'mysql2'
29-
V8_1_Adapter
30-
when 'trilogy'
27+
require 'pry'
28+
binding.pry
29+
if db_connection_adapter == 'trilogy'
3130
V8_1_TrilogyAdapter
3231
else
33-
V8_1_Adapter
32+
V8_1_Mysql2Adapter
3433
end
3534
elsif ar_version::MAJOR == 8
3635
V8_0_Adapter
@@ -42,10 +41,6 @@ def for(ar_version, db_connection_adapter: nil)
4241
raise "Unsupported Rails version: #{ar_version}"
4342
end
4443
end
45-
46-
def specifies_adapter(db_connection_adapter)
47-
Departure.configuration.db_adapter_name.presence || db_connection_adapter
48-
end
4944
end
5045

5146
class BaseAdapter
@@ -168,7 +163,7 @@ def sql_column
168163
end
169164
end
170165

171-
class V8_1_Adapter < BaseAdapter # rubocop:disable Naming/ClassAndModuleCamelCase
166+
class V8_1_Mysql2Adapter < BaseAdapter # rubocop:disable Naming/ClassAndModuleCamelCase
172167
class << self
173168
def register_integrations
174169
require 'active_record/connection_adapters/rails_8_1_mysql2_adapter'
@@ -210,12 +205,20 @@ def sql_column
210205
end
211206
end
212207

213-
class V8_1_TrilogyAdapter < V8_1_Adapter # rubocop:disable Naming/ClassAndModuleCamelCase
208+
class V8_1_TrilogyAdapter < V8_1_Mysql2Adapter # rubocop:disable Naming/ClassAndModuleCamelCase
214209
class << self
215210
def register_integrations
216211
require 'active_record/connection_adapters/rails_8_1_trilogy_adapter'
217212
require 'departure/rails_patches/active_record_migrator_with_advisory_lock_patch'
218213

214+
ActiveSupport.on_load(:active_record) do
215+
ActiveRecord::Migration.class_eval do
216+
include Departure::Migration
217+
end
218+
219+
ActiveRecord::Migrator.prepend Departure::RailsPatches::ActiveRecordMigratorWithAdvisoryLockPatch
220+
end
221+
219222
ActiveRecord::ConnectionAdapters.register 'percona',
220223
'ActiveRecord::ConnectionAdapters::Rails81TrilogyAdapter',
221224
'active_record/connection_adapters/rails_8_1_trilogy_adapter'

spec/integration/rails_adapter_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ def instance_for(version, db_connection_adapter = 'mysql2')
2323
it 'when the config specifies an adapter of trilogy' do
2424
expect(instance_for('8.1.0', 'trilogy')).to be(Departure::RailsAdapter::V8_1_TrilogyAdapter)
2525
end
26-
27-
it 'when the config specifies an adapter of trilogy regardless of the db-connection_adapter' do
28-
expect(Departure.configuration).to receive(:db_adapter_name) { 'trilogy' }
29-
expect(instance_for('8.1.0', 'mysql2')).to be(Departure::RailsAdapter::V8_1_TrilogyAdapter)
30-
end
3126
end
3227

3328
describe 'returns mysql2 adapter' do
@@ -40,9 +35,8 @@ def instance_for(version, db_connection_adapter = 'mysql2')
4035
expect(instance_for('8.1.0', 'mysql2')).to be(Departure::RailsAdapter::V8_1_Adapter)
4136
end
4237

43-
it 'when the config specifies an adapter of mysql2 regardless of the db-connection_adapter' do
44-
expect(Departure.configuration).to receive(:db_adapter_name) { 'mysql2' }
45-
expect(instance_for('8.1.0', 'trilogy')).to be(Departure::RailsAdapter::V8_1_Adapter)
38+
it 'when the config specifies anything else' do
39+
expect(instance_for('8.1.0', 'percona')).to be(Departure::RailsAdapter::V8_1_Adapter)
4640
end
4741
end
4842
end

0 commit comments

Comments
 (0)