-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from rails-on-services/development
Add trilogy adapter support
- Loading branch information
Showing
6 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
ruby_version: | ||
- 3.0 | ||
- "3.0" | ||
- 3.1 | ||
- 3.2 | ||
- 3.3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |