Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trilogy adapter support #273

Merged
merged 13 commits into from
May 28, 2024
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
ruby_version:
- 3.0
- "3.0"
- 3.1
- 3.2
- 3.3
Expand Down
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ RSpec/ExampleWording:
RSpec/FilePath:
Exclude:
- 'spec/adapters/mysql2_adapter_spec.rb'
- 'spec/adapters/trilogy_adapter_spec.rb'
- 'spec/adapters/postgresql_adapter_spec.rb'
- 'spec/adapters/sqlite3_adapter_spec.rb'
- 'spec/tenant_spec.rb'
Expand Down Expand Up @@ -292,6 +293,7 @@ RSpec/MultipleExpectations:
RSpec/NamedSubject:
Exclude:
- 'spec/adapters/mysql2_adapter_spec.rb'
- 'spec/adapters/trilogy_adapter_spec.rb'
- 'spec/adapters/sqlite3_adapter_spec.rb'
- 'spec/support/contexts.rb'
- 'spec/support/requirements.rb'
Expand All @@ -315,6 +317,7 @@ RSpec/NoExpectationExample:
RSpec/SpecFilePathFormat:
Exclude:
- 'spec/adapters/mysql2_adapter_spec.rb'
- 'spec/adapters/trilogy_adapter_spec.rb'
- 'spec/adapters/postgresql_adapter_spec.rb'
- 'spec/adapters/sqlite3_adapter_spec.rb'
- 'spec/tenant_spec.rb'
Expand Down
29 changes: 29 additions & 0 deletions lib/apartment/adapters/trilogy_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'apartment/adapters/mysql2_adapter'

module Apartment
# Helper module to decide wether to use trilogy adapter or trilogy adapter with schemas
module Tenant
def self.trilogy_adapter(config)
if Apartment.use_schemas
Adapters::TrilogySchemaAdapter.new(config)
else
Adapters::TrilogyAdapter.new(config)
end
end
end

module Adapters
class TrilogyAdapter < Mysql2Adapter
protected

def rescue_from
Trilogy::Error
end
end

class TrilogySchemaAdapter < Mysql2SchemaAdapter
end
end
end
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Apartment
VERSION = '3.0.4'
VERSION = '3.1.0'
end
1 change: 1 addition & 0 deletions ros-apartment.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'mysql2', '~> 0.5'
s.add_development_dependency 'pg', '~> 1.5'
s.add_development_dependency 'sqlite3', '< 2.0'
s.add_development_dependency 'trilogy', '< 3.0'
end
end
63 changes: 63 additions & 0 deletions spec/adapters/trilogy_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'spec_helper'
require 'apartment/adapters/trilogy_adapter'

describe Apartment::Adapters::TrilogyAdapter, database: :mysql do
unless defined?(JRUBY_VERSION)

subject(:adapter) { Apartment::Tenant.adapter }

def tenant_names
ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').pluck(0)
end

let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }

it_behaves_like 'a generic apartment adapter callbacks'

context 'when using - the equivalent of - schemas' do
before { Apartment.use_schemas = true }

it_behaves_like 'a generic apartment adapter'

describe '#default_tenant' do
it 'is set to the original db from config' do
expect(subject.default_tenant).to eq(config[:database])
end
end

describe '#init' do
include Apartment::Spec::AdapterRequirements

before do
Apartment.configure do |config|
config.excluded_models = ['Company']
end
end

after do
# Apartment::Tenant.init creates per model connection.
# Remove the connection after testing not to unintentionally keep the connection across tests.
Apartment.excluded_models.each do |excluded_model|
excluded_model.constantize.remove_connection
end
end

it 'processes model exclusions' do
Apartment::Tenant.init

expect(Company.table_name).to eq("#{default_tenant}.companies")
end
end
end

context 'when using connections' do
before { Apartment.use_schemas = false }

it_behaves_like 'a generic apartment adapter'
it_behaves_like 'a generic apartment adapter able to handle custom configuration'
it_behaves_like 'a connection based apartment adapter'
end
end
end