|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require 'apartment/adapters/abstract_adapter'
|
| 4 | +require 'apartment/active_record/postgresql_adapter' |
4 | 5 |
|
5 | 6 | module Apartment
|
6 | 7 | module Tenant
|
@@ -41,7 +42,6 @@ def default_tenant
|
41 | 42 | def reset
|
42 | 43 | @current = default_tenant
|
43 | 44 | Apartment.connection.schema_search_path = full_search_path
|
44 |
| - reset_sequence_names |
45 | 45 | end
|
46 | 46 |
|
47 | 47 | def init
|
@@ -81,9 +81,8 @@ def connect_to_new(tenant = nil)
|
81 | 81 | # there is a issue for prepared statement with changing search_path.
|
82 | 82 | # https://www.postgresql.org/docs/9.3/static/sql-prepare.html
|
83 | 83 | Apartment.connection.clear_cache! if postgresql_version < 90_300
|
84 |
| - reset_sequence_names |
85 |
| - rescue *rescuable_exceptions |
86 |
| - raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}" |
| 84 | + rescue *rescuable_exceptions => e |
| 85 | + raise_schema_connect_to_new(tenant, e) |
87 | 86 | end
|
88 | 87 |
|
89 | 88 | private
|
@@ -130,29 +129,18 @@ def postgresql_version
|
130 | 129 | Apartment.connection.send(:postgresql_version)
|
131 | 130 | end
|
132 | 131 |
|
133 |
| - def reset_sequence_names |
134 |
| - # sequence_name contains the schema, so it must be reset after switch |
135 |
| - # There is `reset_sequence_name`, but that method actually goes to the database |
136 |
| - # to find out the new name. Therefore, we do this hack to only unset the name, |
137 |
| - # and it will be dynamically found the next time it is needed |
138 |
| - descendants_to_unset = ActiveRecord::Base.descendants |
139 |
| - .select { |c| c.instance_variable_defined?(:@sequence_name) } |
140 |
| - .reject do |c| |
141 |
| - c.instance_variable_defined?(:@explicit_sequence_name) && |
142 |
| - c.instance_variable_get(:@explicit_sequence_name) |
143 |
| - end |
144 |
| - descendants_to_unset.each do |c| |
145 |
| - # NOTE: due to this https://github.com/rails-on-services/apartment/issues/81 |
146 |
| - # unreproduceable error we're checking before trying to remove it |
147 |
| - c.remove_instance_variable :@sequence_name if c.instance_variable_defined?(:@sequence_name) |
148 |
| - end |
149 |
| - end |
150 |
| - |
151 | 132 | def schema_exists?(schemas)
|
152 | 133 | return true unless Apartment.tenant_presence_check
|
153 | 134 |
|
154 | 135 | Array(schemas).all? { |schema| Apartment.connection.schema_exists?(schema.to_s) }
|
155 | 136 | end
|
| 137 | + |
| 138 | + def raise_schema_connect_to_new(tenant, exception) |
| 139 | + raise TenantNotFound, <<~EXCEPTION_MESSAGE |
| 140 | + Could not set search path to schemas, they may be invalid: "#{tenant}" #{full_search_path}. |
| 141 | + Original error: #{exception.class}: #{exception} |
| 142 | + EXCEPTION_MESSAGE |
| 143 | + end |
156 | 144 | end
|
157 | 145 |
|
158 | 146 | # Another Adapter for Postgresql when using schemas and SQL
|
|
0 commit comments