-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5607d6b
commit 7ed6e1c
Showing
8 changed files
with
328 additions
and
29 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
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
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 | ||
|
||
module ActualDbSchema | ||
# Handles multi-tenancy support by switching schemas for supported databases | ||
module MultiTenant | ||
include ActualDbSchema::OutputFormatter | ||
|
||
class << self | ||
def with_schema(schema_name) | ||
context = switch_schema(schema_name) | ||
yield | ||
ensure | ||
restore_context(context) | ||
end | ||
|
||
private | ||
|
||
def adapter_name | ||
ActiveRecord::Base.connection.adapter_name | ||
end | ||
|
||
def switch_schema(schema_name) | ||
case adapter_name | ||
when /postgresql/i | ||
switch_postgresql_schema(schema_name) | ||
when /mysql/i | ||
switch_mysql_schema(schema_name) | ||
else | ||
message = "[ActualDbSchema] Multi-tenancy not supported for adapter: #{adapter_name}. " \ | ||
"Proceeding without schema switching." | ||
puts colorize(message, :gray) | ||
end | ||
end | ||
|
||
def switch_postgresql_schema(schema_name) | ||
old_search_path = ActiveRecord::Base.connection.schema_search_path | ||
ActiveRecord::Base.connection.schema_search_path = schema_name | ||
{ type: :postgresql, old_context: old_search_path } | ||
end | ||
|
||
def switch_mysql_schema(schema_name) | ||
old_db = ActiveRecord::Base.connection.current_database | ||
ActiveRecord::Base.connection.execute("USE #{ActiveRecord::Base.connection.quote_table_name(schema_name)}") | ||
{ type: :mysql, old_context: old_db } | ||
end | ||
|
||
def restore_context(context) | ||
return unless context | ||
|
||
case context[:type] | ||
when :postgresql | ||
ActiveRecord::Base.connection.schema_search_path = context[:old_context] if context[:old_context] | ||
when :mysql | ||
return unless context[:old_context] | ||
|
||
ActiveRecord::Base.connection.execute( | ||
"USE #{ActiveRecord::Base.connection.quote_table_name(context[:old_context])}" | ||
) | ||
end | ||
end | ||
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
Oops, something went wrong.