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 migrations #15250

14 changes: 14 additions & 0 deletions db/migrate/20170530102506_add_deleted_to_containers_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddDeletedToContainersTables < ActiveRecord::Migration[5.0]
def change
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"
add_index :container_images, :deleted_on,
:name => "index_container_images_on_deleted_on"
add_index :container_projects, :deleted_on,
:name => "index_container_projects_on_deleted_on"
add_index :containers, :deleted_on,
:name => "index_containers_on_deleted_on"
end
end
67 changes: 67 additions & 0 deletions db/migrate/20170530102536_use_deleted_in_containers_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class UseDeletedInContainersTables < ActiveRecord::Migration[5.0]
class ContainerDefinition < ActiveRecord::Base; end

class ContainerGroup < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class ContainerImage < ActiveRecord::Base; end

class ContainerProject < ActiveRecord::Base; end

class Container < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def disconnect_to_soft_delete(model)
model.where.not(:deleted_on => nil).update_all("ems_id = old_ems_id")
end

def soft_delete_to_disconnect(model)
model.where.not(:deleted_on => nil).update_all(:ems_id => nil)
end

def up
say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerDefinition") do
disconnect_to_soft_delete(ContainerDefinition)
end

say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerGroup") do
disconnect_to_soft_delete(ContainerGroup)
end

say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerImages") do
disconnect_to_soft_delete(ContainerImage)
end

say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerProject") do
disconnect_to_soft_delete(ContainerProject)
end

say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for Container") do
disconnect_to_soft_delete(Container)
end
end

def down
say_with_time("Change ':deleted_on not nil' :ems_id to nil for ContainerDefinition") do
soft_delete_to_disconnect(ContainerDefinition)
end

say_with_time("Change ':deleted_on not nil' :ems_id to nil for ContainerGroup") do
soft_delete_to_disconnect(ContainerGroup)
end

say_with_time("Change ':deleted_on not nil' :ems_id to nil for ContainerImages") do
soft_delete_to_disconnect(ContainerImage)
end

say_with_time("Change ':deleted_on not nil' :ems_id to nil for ContainerProject") do
soft_delete_to_disconnect(ContainerProject)
end

say_with_time("Change ':deleted_on not nil' :ems_id to nil for Container") do
soft_delete_to_disconnect(Container)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require_migration

describe UseDeletedInContainersTables do
let(:container_definitions_stub) { migration_stub(:ContainerDefinition) }
let(:container_groups_stub) { migration_stub(:ContainerGroup) }
let(:container_images_stub) { migration_stub(:ContainerImage) }
let(:container_projects_stub) { migration_stub(:ContainerProject) }
let(:containers_stub) { migration_stub(:Container) }

def create_before_migration_stub_data_for(model)
model.create!(:ems_id => 10, :old_ems_id => nil)
model.create!(:ems_id => 10, :old_ems_id => 10)
model.create!(:ems_id => nil, :old_ems_id => 10, :deleted_on => Time.now.utc)
model.create!(:ems_id => nil, :old_ems_id => 20, :deleted_on => Time.now.utc)
model.create!(:ems_id => nil, :old_ems_id => nil, :deleted_on => Time.now.utc)
end

def create_after_migration_stub_data_for(model)
model.create!(:ems_id => 10, :old_ems_id => nil)
model.create!(:ems_id => 10, :old_ems_id => 10)
model.create!(:ems_id => 10, :old_ems_id => 10, :deleted_on => Time.now.utc)
model.create!(:ems_id => 20, :old_ems_id => 20, :deleted_on => Time.now.utc)
model.create!(:ems_id => nil, :old_ems_id => nil, :deleted_on => Time.now.utc)
end

def assert_before_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 3
expect(model.where.not(:ems_id => nil).count).to eq 2
end

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
expect(model.where.not(:ems_id => nil).count).to eq 4
end

def assert_up_migration_for(model)
create_before_migration_stub_data_for(model)

assert_before_migration_data_of(model)
migrate
assert_after_migration_data_of(model)
end

def assert_down_migration_for(model)
create_after_migration_stub_data_for(model)

assert_after_migration_data_of(model)
migrate
assert_before_migration_data_of(model)
end

migration_context :up do
it "Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerDefinition" do
assert_up_migration_for(container_definitions_stub)
end

it "Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerGroup" do
assert_up_migration_for(container_groups_stub)
end

it "Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerImages" do
assert_up_migration_for(container_images_stub)
end

it "Change ':deleted_on not nil' :ems_id to :old_ems_id for ContainerProjects" do
assert_up_migration_for(container_projects_stub)
end

it "Change ':deleted_on not nil' :ems_id to :old_ems_id for Containers" do
assert_up_migration_for(containers_stub)
end
end

migration_context :down do
it "Change ':deleted_on not nil' :ems_id to nil for ContainerDefinition" do
assert_down_migration_for(container_definitions_stub)
end

it "Change ':deleted_on not nil' :ems_id to nil for ContainerGroup" do
assert_down_migration_for(container_groups_stub)
end

it "Change ':deleted_on not nil' :ems_id to nil for ContainerImages" do
assert_down_migration_for(container_images_stub)
end

it "Change ':deleted_on not nil' :ems_id to nil for ContainerProjects" do
assert_down_migration_for(container_projects_stub)
end

it "Change ':deleted_on not nil' :ems_id to nil for Containers" do
assert_down_migration_for(containers_stub)
end
end
end