Skip to content
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

Conversation

Ladas
Copy link
Contributor

@Ladas Ladas commented Jun 23, 2017

Soft delete instead of disconnect for containers models

@Ladas
Copy link
Contributor Author

Ladas commented Jun 23, 2017

@miq-bot assign @Fryguy

cc @zeari @cben @agrare @simon3z

class AddDeletedToContainersTables < ActiveRecord::Migration[5.0]
def change
add_index :container_definitions, :deleted_on,
:name => "index_container_definitions_on_deleted_on"
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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"
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

@Ladas Ladas Jul 3, 2017

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Fryguy
Copy link
Member

Fryguy commented Jun 23, 2017

Code-wise, looks good to me. I want a 👍 from @simon3z before merging, though.

@Fryguy
Copy link
Member

Fryguy commented Jun 23, 2017

@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.

@simon3z
Copy link
Contributor

simon3z commented Jun 23, 2017

Code-wise, looks good to me. I want a from @simon3z before merging, though.

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
Copy link
Contributor

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.

@Ladas
Copy link
Contributor Author

Ladas commented Jul 3, 2017

@Fryguy @simon3z ok, will squash it as this seems the final version. Will default and dry it up. :-)

@Fryguy Fryguy mentioned this pull request Jul 3, 2017
Ladas added 3 commits July 4, 2017 09:30
Add indexes to :deleted_on cols for Container tables
Use soft delete instead of disconnect
Test data migration of disconnect to soft delete
@Ladas Ladas force-pushed the soft_delete_instead_of_disconnect_for_containers_models branch from 2136910 to 0f8511b Compare July 4, 2017 07:32
@Ladas
Copy link
Contributor Author

Ladas commented Jul 4, 2017

@simon3z @Fryguy @cben done, done and done. :-D Added also the ContainerNode model.

Fix rubocop issues
Copy link
Contributor

@cben cben left a 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")
Copy link
Contributor

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.

Copy link
Contributor

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?).

Copy link
Contributor Author

@Ladas Ladas Jul 12, 2017

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

Copy link
Contributor Author

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?

Copy link
Contributor Author

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
@miq-bot
Copy link
Member

miq-bot commented Jul 12, 2017

Checked commits Ladas/manageiq-schema@dda9800~...539e757 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
3 files checked, 6 offenses detected

db/migrate/20170704102536_use_deleted_on_in_containers_tables.rb

spec/migrations/20170704102536_use_deleted_on_in_containers_tables_spec.rb

@cben
Copy link
Contributor

cben commented Jul 12, 2017

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. 👍

@Ladas
Copy link
Contributor Author

Ladas commented Jul 13, 2017

@Fryguy @simon3z this should be ready

@Fryguy Fryguy merged commit 9ac5bfb into ManageIQ:master Jul 17, 2017
@Fryguy Fryguy added this to the Sprint 65 Ending Jul 24, 2017 milestone Jul 17, 2017
cben added a commit to cben/manageiq-providers-kubernetes that referenced this pull request Jul 4, 2018
ems_id always set since we switched to soft delete.  Backfilled for
old records too by ManageIQ/manageiq-schema#18.
cben added a commit to cben/manageiq that referenced this pull request Jul 11, 2018
ems_id always set since we switched to soft delete.  Backfilled for
old records too by ManageIQ/manageiq-schema#18.
cben added a commit to cben/manageiq that referenced this pull request Jul 11, 2018
ems_id always set since we switched to soft delete.  Backfilled for
old records too by ManageIQ/manageiq-schema#18.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants