-
Notifications
You must be signed in to change notification settings - Fork 125
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
Soft delete instead of disconnect for containers models #18
Soft delete instead of disconnect for containers models #18
Conversation
class AddDeletedToContainersTables < ActiveRecord::Migration[5.0] | ||
def change | ||
add_index :container_definitions, :deleted_on, | ||
:name => "index_container_definitions_on_deleted_on" |
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.
Can you remove the name and let Rails use the default name?
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.
might be that the default name will pass the length limit now, will do
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.
done
add_index :container_definitions, :deleted_on, | ||
:name => "index_container_definitions_on_deleted_on" | ||
add_index :container_groups, :deleted_on, | ||
:name => "container_groups_on_deleted_on" |
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.
Does this one not have the "index" prefix intentionally?
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.
fixed by autonaming
|
||
say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for Container") do | ||
disconnect_to_soft_delete(Container) | ||
end |
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.
Minor, but you can massively DRY this up with a loop on the models
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 know, I was too lazy. @lpichler has said the same thing :-D
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.
done
Code-wise, looks good to me. I want a 👍 from @simon3z before merging, though. |
@Ladas Would it make sense to squash commits here? For example, you have commits in the middle about partial indexes, but you don't actually have any partial indexes in the end. |
I was waiting for @cben to take a look at ManageIQ/manageiq#15250 but probably shouldn't block on that. +1 for letting Rails use the default name for the indexes. Overall LGTM 👍 |
def assert_after_migration_data_of(model) | ||
expect(model.where.not(:deleted_on => nil).count).to eq 3 | ||
expect(model.where(:deleted_on => nil).count).to eq 2 | ||
expect(model.where(:ems_id => nil).count).to eq 1 |
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.
consider also testing :ems_id => 20
and/or :ems_id => 10
counts.
Add indexes to :deleted_on cols for Container tables
Use soft delete instead of disconnect
Test data migration of disconnect to soft delete
2136910
to
0f8511b
Compare
Fix rubocop issues
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.
Neat 👍
end | ||
|
||
def disconnect_to_soft_delete(model) | ||
model.where.not(:deleted_on => nil).update_all("ems_id = old_ems_id") |
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.
@zeari @zakiva @simon3z
Is there any scenario where a record could have ems_id
set and old_ems_id == nil
, but deleted_on
set?
Then this would make a bad situation worse, setting ems_id = old_ems_id = nil
.
It "shouldn't happen", I just remember we saw ems_id == old_ems_id == nil
in a customer's DB, and suspected it might have resulted from double disconnect overwriting old_ems_id, so trying to think along similar lines...
Same question for soft_delete_to_disconnect
.
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.
@zeari @zakiva @simon3z
Is there any scenario where a record could have ems_id set and old_ems_id == nil, but deleted_on set?
Then this would make a bad situation worse, setting ems_id = old_ems_id = nil.
@cben technically there shouldn't be but I suppose it depends on whether it was possible that a crash left unfinished the updates (if multiple ones and not atomic). Also you're never 100% sure of not having had bugs.
If there's a better logic that we could use to avoid possible mistakes then let's try to improve this (maybe we should use model.where.not(:deleted_on => nil, :ems_id => nil).update_all("ems_id = old_ems_id")
to be on the safe side?).
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.
so doing this, before doing update_all("ems_id = old_ems_id")
model.where.not(:deleted_on => nil).where.not(:ems_id => nil).update_all(:deleted_on => nil)
so having :ems_id will mean the record is not disconnected
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 wonder if I should also clean up the case where :ems_id is nil but :deleted_on is also nil? Putting Time.now into deleted_on?
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.
For :deleted_on => nil, :ems_id => nil, I am filling deleted_on with a date
Correct naming of the migrations
Clean up deleted_on for non diconected records
Cover :deleted_on => nil, :ems_id => nil case by filling deleted_on
Checked commits Ladas/manageiq-schema@dda9800~...539e757 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 db/migrate/20170704102536_use_deleted_on_in_containers_tables.rb
spec/migrations/20170704102536_use_deleted_on_in_containers_tables_spec.rb
|
Sorry, I've been reviewing another migration today, and my brain is refusing to concentrate 🤕 But yeah, looks excellent (thanks for separate commits). In both directions, you make deleted_on consistent before you act based on it. 👍 |
ems_id always set since we switched to soft delete. Backfilled for old records too by ManageIQ/manageiq-schema#18.
ems_id always set since we switched to soft delete. Backfilled for old records too by ManageIQ/manageiq-schema#18.
ems_id always set since we switched to soft delete. Backfilled for old records too by ManageIQ/manageiq-schema#18.
Soft delete instead of disconnect for containers models