Skip to content

Commit

Permalink
Merge pull request #18 from Ladas/soft_delete_instead_of_disconnect_f…
Browse files Browse the repository at this point in the history
…or_containers_models

Soft delete instead of disconnect for containers models
  • Loading branch information
Fryguy authored Jul 17, 2017
2 parents 5d92818 + 539e757 commit 9ac5bfb
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddDeletedOnIndexesToContainersTables < ActiveRecord::Migration[5.0]
def change
add_index :container_definitions, :deleted_on
add_index :container_groups, :deleted_on
add_index :container_images, :deleted_on
add_index :container_projects, :deleted_on
add_index :container_nodes, :deleted_on
add_index :containers, :deleted_on
end
end
57 changes: 57 additions & 0 deletions db/migrate/20170704102536_use_deleted_on_in_containers_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class UseDeletedOnInContainersTables < 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 ContainerNode < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

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

def disconnect_to_soft_delete(model)
model.where(:deleted_on => nil, :ems_id => nil).update_all(:deleted_on => Time.now.utc)
model.where.not(:deleted_on => nil).where.not(:ems_id => nil).update_all(:deleted_on => nil)
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

MODEL_CLASSES = [
ContainerDefinition,
ContainerGroup,
ContainerImage,
ContainerProject,
ContainerNode,
Container,
].freeze

def up
MODEL_CLASSES.each do |model_class|
say_with_time("Change ':deleted_on not nil' :ems_id to :old_ems_id for #{model_class}") do
disconnect_to_soft_delete(model_class)
end
end
end

def down
MODEL_CLASSES.each do |model_class|
say_with_time("Change ':deleted_on not nil' :ems_id to nil for #{model_class}") do
soft_delete_to_disconnect(model_class)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require_migration

describe UseDeletedOnInContainersTables 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(:container_nodes_stub) { migration_stub(:ContainerNode) }
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 => 11, :old_ems_id => 11)
model.create!(:ems_id => nil, :old_ems_id => 12, :deleted_on => Time.now.utc)
model.create!(:ems_id => 15, :old_ems_id => 15, :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)
model.create!(:ems_id => nil, :old_ems_id => 25, :deleted_on => nil)
end

def create_after_migration_stub_data_for(model)
model.create!(:ems_id => 10, :old_ems_id => nil)
model.create!(:ems_id => 11, :old_ems_id => 11)
model.create!(:ems_id => 15, :old_ems_id => 15, :deleted_on => nil)
model.create!(:ems_id => 12, :old_ems_id => 12, :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)
model.create!(:ems_id => 25, :old_ems_id => 25, :deleted_on => Time.now.utc)
end

def assert_before_migration_data_of(model, context)
if context == :up
expect(model.where.not(:deleted_on => nil).count).to eq 4
expect(model.where(:deleted_on => nil).count).to eq 3
expect(model.where.not(:deleted_on => nil).collect(&:ems_id)).to(
match_array([15, nil, nil, nil])
)
expect(model.where(:deleted_on => nil).collect(&:ems_id)).to(
match_array([10, 11, nil])
)
else
expect(model.where.not(:deleted_on => nil).count).to eq 4
expect(model.where(:deleted_on => nil).count).to eq 3
expect(model.where.not(:deleted_on => nil).collect(&:ems_id)).to(
match_array([nil, nil, nil, nil])
)
expect(model.where(:deleted_on => nil).collect(&:ems_id)).to(
match_array([10, 11, 15])
)
end
expect(model.where(:ems_id => nil).count).to eq 4
expect(model.where(:ems_id => 10).count).to eq 1
expect(model.where(:ems_id => 11).count).to eq 1
expect(model.where(:ems_id => 12).count).to eq 0
expect(model.where(:ems_id => 15).count).to eq 1
expect(model.where(:ems_id => 20).count).to eq 0
expect(model.where(:ems_id => 25).count).to eq 0
expect(model.where.not(:ems_id => nil).count).to eq 3
end

def assert_after_migration_data_of(model)
expect(model.where.not(:deleted_on => nil).count).to eq 4
expect(model.where.not(:deleted_on => nil).collect(&:ems_id)).to(
match_array([12, 20, nil, 25])
)
expect(model.where(:deleted_on => nil).count).to eq 3
expect(model.where(:deleted_on => nil).collect(&:ems_id)).to(
match_array([10, 11, 15])
)
expect(model.where(:ems_id => nil).count).to eq 1
expect(model.where(:ems_id => 10).count).to eq 1
expect(model.where(:ems_id => 11).count).to eq 1
expect(model.where(:ems_id => 12).count).to eq 1
expect(model.where(:ems_id => 15).count).to eq 1
expect(model.where(:ems_id => 20).count).to eq 1
expect(model.where(:ems_id => 25).count).to eq 1
expect(model.where.not(:ems_id => nil).count).to eq 6
end

def assert_up_migration_for(model)
create_before_migration_stub_data_for(model)

assert_before_migration_data_of(model, :up)
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, :down)
end

ALL_STUBS = [
:container_definitions_stub,
:container_groups_stub,
:container_images_stub,
:container_projects_stub,
:container_nodes_stub,
:containers_stub
].freeze

migration_context :up do
ALL_STUBS.each do |stub|
context "with #{stub}" do
it "change ':deleted_on not nil' :ems_id to :old_ems_id" do
assert_up_migration_for(public_send(stub))
end
end
end
end

migration_context :down do
ALL_STUBS.each do |stub|
context "with #{stub}" do
it "change ':deleted_on not nil' :ems_id to nil" do
assert_down_migration_for(public_send(stub))
end
end
end
end
end

0 comments on commit 9ac5bfb

Please sign in to comment.