-
Notifications
You must be signed in to change notification settings - Fork 239
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
rebuild! on a scoped collection empties the entire table #275
Comments
Ugh, that's rough, and I'm glad you thought to rebuild the full table. (I'd also suggest taking backups before launching a prod console as a standard operating procedure, speaking from prior scarring production emergencies). I hate to say it, but the ActiveRecord API is so expansive that documenting every way not to use every function is simply not tenable. I'd be happy to accept a PR that can detect when Thanks for taking the time to report the issue. |
What’s the point of the table truncation in the |
@kazalt perhaps I'm misunderstanding you, but the tests don't truncate the hierarchies table to get around FK constraints. In other words, there aren't FK constraints to the The class-level |
@mceachen the for the foreign keys, i'm talking about those : I can't delete an instance without deleting their hierarchies, so the truncation seems useless. I talk about that truncation : |
@kazalt yes, if your FK constraints are all valid, the truncation shouldn't be necessary, but realize when I wrote this gem, rails 2 didn't even support foreign keys. Perhaps the truncate could be an option?
If you have time, please read, comment, and vote on #277 ! |
I have the same problem with STI. Given the example from the README, if I do
and call For the moment, I worked around it by overloading the
|
@gr8bit thanks! I think that would be great to add to the docs. If you have time, I'd be happy to take a pr! |
PR #287 |
Perfect, thanks! |
It would appear that executing
rebuild!
on a scoped collection empties the whole table before rebuilding the few selected elements.In our case, the model is called BusinessUnit.
When you run
BusinessUnit.where(id: [1, 2]).each { |bu| bu.rebuild! }
, the following SQL queries are executed (amongst the INSERT queries):When you run
BusinessUnit.where(id: [1, 2]).rebuild!
, the following SQL queries are executed (again, amongst the INSERT queries):You will notice the extraneous
DELETE FROM "business_unit_hierarchies"
, which completely empties the table before rebuilding only the two selected BusinessUnits.We unfortunately experienced this problem in production, but were quickly able to recover by doing a full
BusinessUnit.rebuild!
. However, I think it would be nice to fix this, to avoid the same situation from happening to another unfortunate soul.The text was updated successfully, but these errors were encountered: