Skip to content

Commit 19770a1

Browse files
Ryan Brunnermarksiemers
authored andcommitted
Allow a list of schemas when switching using schemas
1 parent ac0fbb2 commit 19770a1

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ would not allow a full new database to be created.
8484

8585
One can optionally use the full database creation instead if they want, though this is not recommended
8686

87+
When using schemas, you can also pass in a list of schemas if desired. Any tables defined in a schema earlier in the chain will be referenced first, so this is only useful if you have a schema with only some of the tables defined:
88+
89+
```ruby
90+
Apartment::Tenant.switch(['tenant_1', 'tenant_2']) do
91+
# ...
92+
end
93+
```
94+
8795
### Switching Tenants
8896

8997
To switch tenants using Apartment, use the following command:

lib/apartment/adapters/jdbc_postgresql_adapter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ class JDBCPostgresqlSchemaAdapter < PostgresqlSchemaAdapter
3838
#
3939
def connect_to_new(tenant = nil)
4040
return reset if tenant.nil?
41+
raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless schema_exists?(tenant)
4142

42-
tenant = tenant.to_s
43-
raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
43+
@current = tenant.is_a?(Array) ? tenant.map(&:to_s) : tenant.to_s
44+
Apartment.connection.schema_search_path = full_search_path
4445

4546
@current = tenant
4647
Apartment.connection.schema_search_path = full_search_path

lib/apartment/adapters/postgresql_adapter.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ def drop_command(conn, tenant)
7272
#
7373
def connect_to_new(tenant = nil)
7474
return reset if tenant.nil?
75+
raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless schema_exists?(tenant)
7576

76-
tenant = tenant.to_s
77-
raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
78-
79-
@current = tenant
77+
@current = tenant.is_a?(Array) ? tenant.map(&:to_s) : tenant.to_s
8078
Apartment.connection.schema_search_path = full_search_path
8179

8280
# When the PostgreSQL version is < 9.3,
@@ -148,6 +146,9 @@ def reset_sequence_names
148146
# unreproduceable error we're checking before trying to remove it
149147
c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name)
150148
end
149+
150+
def schema_exists?(schemas)
151+
[*schemas].all? { |schema| Apartment.connection.schema_exists?(schema.to_s) }
151152
end
152153
end
153154

spec/examples/schema_adapter_examples.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@
127127
expect(connection.schema_search_path).to start_with %("#{public_schema}")
128128
expect(User.sequence_name).to eq "#{public_schema}.#{User.table_name}_id_seq"
129129
end
130+
131+
it "allows a list of schemas" do
132+
subject.switch([schema1, schema2]) do
133+
expect(connection.schema_search_path).to include %{"#{schema1}"}
134+
expect(connection.schema_search_path).to include %{"#{schema2}"}
135+
end
136+
end
130137
end
131138

132139
describe '#reset' do

0 commit comments

Comments
 (0)