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

Allow loading or dumping a specific tenant in a multitenant setup using Apartment gem #131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/yaml_db/serialization_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def reenable_logger

class Load
def self.load(io, truncate = true)
if defined?(Apartment::Tenant) && ENV['TENANT']
Apartment::Tenant.switch! ENV['TENANT']
end
ActiveRecord::Base.connection.transaction do
load_documents(io, truncate)
end
Expand Down Expand Up @@ -165,6 +168,9 @@ def self.after_table(io, table)
end

def self.tables
if defined?(Apartment::Tenant) && ENV['TENANT']
Apartment::Tenant.switch! ENV['TENANT']
end
ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }.sort
end

Expand Down
2 changes: 1 addition & 1 deletion lib/yaml_db/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module YamlDb
VERSION = "0.6.0"
VERSION = "0.6.1"
end
21 changes: 21 additions & 0 deletions spec/yaml_db/serialization_helper_dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ module SerializationHelper
@io = StringIO.new
end

context "with Apartment multi-tenancy" do
before(:each) do
class_double('Apartment').as_stubbed_const
@tenant_klass = class_double('Apartment::Tenant').as_stubbed_const
@old_env = ENV['TENANT']
end
after(:each) do
ENV['TENANT'] = @old_env
end
it "explicitly sets tenant before extracting tables if ENV['TENANT'] set" do
tenant_name = 'tenant-33'
ENV['TENANT'] = tenant_name
expect(@tenant_klass).to receive(:switch!).with(tenant_name)
Dump.tables
end
it "does not explicitly set tenant if ENV['TENANT'] not set" do
ENV.delete('TENANT')
expect(@tenant_klass).not_to receive(:switch!)
Dump.tables
end
end
it "returns a list of column names" do
expect(Dump.table_column_names('mytable')).to eq([ 'a', 'b' ])
end
Expand Down
22 changes: 22 additions & 0 deletions spec/yaml_db/serialization_helper_load_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ module SerializationHelper
@io = StringIO.new
end

context "with Apartment multi-tenancy" do
before(:each) do
allow(Load).to receive(:load_documents)
class_double('Apartment').as_stubbed_const
@tenant_klass = class_double('Apartment::Tenant').as_stubbed_const
@old_env = ENV['TENANT']
end
after(:each) do
ENV['TENANT'] = @old_env
end
it "switches to tenant if ENV[TENANT] set" do
tenant_name = 'tenant-23'
ENV['TENANT'] = tenant_name
expect(@tenant_klass).to receive(:switch!).with(tenant_name)
Load.load(@io)
end
it "does not explicitly switch tenants if ENV[TENANT] is not set" do
ENV.delete('TENANT')
expect(@tenant_klass).not_to receive(:switch!)
end
end

it "truncates the table" do
allow(ActiveRecord::Base.connection).to receive(:execute).with("TRUNCATE mytable").and_return(true)
expect(ActiveRecord::Base.connection).not_to receive(:execute).with("DELETE FROM mytable")
Expand Down