-
Notifications
You must be signed in to change notification settings - Fork 154
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
Fix sequence name #187
Fix sequence name #187
Conversation
@fsateler if you have the time, do you mind taking a look at this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only dealbraker is that this would force the loading of pg adatper, which wouldn't exist on non-pg databases.
lib/apartment.rb
Outdated
@@ -8,6 +8,7 @@ | |||
|
|||
require_relative 'apartment/log_subscriber' | |||
|
|||
require_relative 'apartment/active_record/postgresql_adapter' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would force the loading of the pg adapter. Are other dbs no longer supported? This was my comment on #143 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I have not looked at how can we fix this and test it on a non postgresql db. I currently run only postgres so it's also hard for me to test these details on all dbs. I've been tempted on starting refactoring the gem to have one gem per adapter and a core, but that's discussion for another day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps we can go the "easy" route:
if defined?(PG)
require_relative 'apartment/active_record/postgresql_adapter'
end
Perhaps in an initializer so that this happens after all gems are loaded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i have a clearer working solution. We only require the adapter once we eval the config. I'm requiring this patch after requiring the postgres adapter
if Apartment.excluded_models.none? { |m| m.constantize.table_name == table } | ||
res.delete_prefix!(schema_prefix) | ||
else | ||
res.sub!(schema_prefix, "#{Apartment::Tenant.default_tenant}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes extra work when default_tenant == current
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, this is not strictly correct, because it should happen even if the sequence name is prefixed with some other scheam. No idea if that can happen though 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of that when i was writing this and then forgot to address it. tks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree w your second concern. addressed it as well
@marksiemers and @citrus I have been working on this with @fsateler and just remembered your request to be able to switch to multiple schemas:
The solution here assumes that the search path will resolve the table's sequence properly and therefore tries to remove the prefix. |
I'll merge this one first. We can look at your specific use case after |
**Implemented enhancements:** - Increase errors visibility by showing more information on the underlying error rather than a generic error 'Apartment::TenantNotFound' (#176) - Resolved #177 - Added rails 7 support (#178) **Fixed bugs:** - Fixing tenant_presence_check config in the README (#180) - Resolved #161 and #81 - Fixed sequence name (#187) **Closed issues:** - Resolved #151 - removed reloader and console overwrite of reload method (#174)
@rpbaltazar - Thanks for calling this out. I don't work at the company anymore where we used Apartment, so I don't have the system/codebase in place that has the real-world use case. I can offer some info from the postgres docs. Your question:
From docs:
Source: https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH I assume this applies to sequences as well. The first named schema in the search path is where any new object will be created. @ryanswood - Is this something you can check into or confirm? |
@rpbaltazar Thank you so much for remembering our request. I worked with @marksiemers and I am at still at the company where the multi-schema switch feature is still needed. To answer your question, the value of It appears as if the override of Quick background: We use the multi-schema switch feature for our data pipeline where we create a temporary schema that partially mirrors the original so that we can mass import data and hide it from UI. There are cases where the duplicate table in the temporary schema does not have its own sequence. |
Resolves #161
Resolves #81
Apartment::Tenant.adapter
to avoid creating two instances of the adapter that wouldn't be in sync