-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrations for merging container_definition and container
- Loading branch information
Ari Zellner
committed
Jun 25, 2017
1 parent
e9d356d
commit 5c3d7f0
Showing
2 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
db/migrate/20170529142557_unify_container_definition_and_container.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
class UnifyContainerDefinitionAndContainer < ActiveRecord::Migration[5.0] | ||
class ContainerDefinition < ActiveRecord::Base | ||
has_many :container_port_configs | ||
has_many :container_env_vars | ||
has_one :security_context, :as => :resource | ||
has_one :container | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
class Container < ActiveRecord::Base | ||
belongs_to :container_definition | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
class SecurityContext < ActiveRecord::Base | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
class ContainerEnvVar < ActiveRecord::Base | ||
belongs_to :container_definition | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
class ContainerPortConfig < ActiveRecord::Base | ||
belongs_to :container_definition | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
def up | ||
# attributes | ||
add_column :containers, :image, :string | ||
add_column :containers, :image_pull_policy, :string | ||
add_column :containers, :memory, :string | ||
add_column :containers, :cpu_cores, :float | ||
add_column :containers, :container_group_id, :bigint | ||
add_column :containers, :privileged, :boolean | ||
add_column :containers, :run_as_user, :bigint | ||
add_column :containers, :run_as_non_root, :boolean | ||
add_column :containers, :capabilities_add, :string | ||
add_column :containers, :capabilities_drop, :string | ||
add_column :containers, :command, :text | ||
|
||
say_with_time("Copying over columns from container_definition to container") do | ||
Container.all.each do |container| | ||
container_definition = ContainerDefinition.find(container.container_definition_id) | ||
container.update!( | ||
:image => container_definition.image, | ||
:image_pull_policy => container_definition.image_pull_policy, | ||
:memory => container_definition.memory, | ||
:cpu_cores => container_definition.cpu_cores, | ||
:container_group_id => container_definition.container_group_id, | ||
:privileged => container_definition.privileged, | ||
:run_as_user => container_definition.run_as_user, | ||
:run_as_non_root => container_definition.run_as_non_root, | ||
:capabilities_add => container_definition.capabilities_add, | ||
:capabilities_drop => container_definition.capabilities_drop, | ||
:command => container_definition.command | ||
) | ||
|
||
say_with_time("changing belong_to relation of ContainerPortConfigs to container") do | ||
ContainerPortConfig.where(:container_definition_id => container_definition.id) | ||
.update_all(:container_definition_id => container.id) | ||
end | ||
say_with_time("changing belong_to relation of ContainerEnvVar to container") do | ||
ContainerEnvVar.where(:container_definition_id => container_definition.id) | ||
.update_all(:container_definition_id => container.id) | ||
end | ||
say_with_time("changing belong_to relation of SecurityContext from container_definition to container") do | ||
SecurityContext.where(:resource_type => "ContainerDefinition", :resource_id => container_definition.id) | ||
.update_all(:resource_type => "Container", :resource_id => container.id) | ||
end | ||
end | ||
end | ||
|
||
# relationships | ||
rename_column :container_port_configs, :container_definition_id, :container_id | ||
rename_column :container_env_vars, :container_definition_id, :container_id | ||
|
||
remove_column :containers, :container_definition_id | ||
drop_table :container_definitions | ||
end | ||
|
||
def down | ||
create_table :container_definitions do |t| | ||
t.belongs_to :ems, :type => :bigint | ||
t.string :ems_ref | ||
t.bigint :old_ems_id | ||
t.timestamp :deleted_on | ||
t.string :name | ||
t.string :image | ||
t.string :image_pull_policy | ||
t.string :memory | ||
t.float :cpu_cores | ||
t.belongs_to :container_group, :type => :bigint | ||
t.boolean :privileged | ||
t.bigint :run_as_user | ||
t.boolean :run_as_non_root | ||
t.string :capabilities_add | ||
t.string :capabilities_drop | ||
t.text :command | ||
end | ||
|
||
add_column :containers, :container_definition_id, :bigint | ||
|
||
say_with_time("splitting columns from container into container_definition") do | ||
Container.all.each do |container| | ||
container_def = ContainerDefinition.create( | ||
:ems_id => container.ems_id, | ||
:ems_ref => container.ems_ref, | ||
:old_ems_id => container.old_ems_id, | ||
:deleted_on => container.deleted_on, | ||
:name => container.name, | ||
:image => container.image, | ||
:image_pull_policy => container.image_pull_policy, | ||
:memory => container.memory, | ||
:cpu_cores => container.cpu_cores, | ||
:container_group_id => container.container_group_id, | ||
:privileged => container.privileged, | ||
:run_as_user => container.run_as_user, | ||
:run_as_non_root => container.run_as_non_root, | ||
:capabilities_add => container.capabilities_add, | ||
:capabilities_drop => container.capabilities_drop, | ||
:command => container.command | ||
) | ||
container.update!(:container_definition_id => container_def.id) | ||
|
||
say_with_time("changing belong_to relation of ContainerPortConfigs to container definition") do | ||
ContainerPortConfig.where(:container_id => container.id) | ||
.update_all(:container_id => container_def.id) | ||
end | ||
say_with_time("changing belong_to relation of ContainerEnvVar to container definition") do | ||
ContainerEnvVar.where(:container_id => container.id) | ||
.update_all(:container_id => container_def.id) | ||
end | ||
say_with_time("changing belong_to relation of SecurityContext from container to container definition") do | ||
SecurityContext.where(:resource_type => "Container", :resource_id => container.id) | ||
.update_all(:resource_type => "ContainerDefinition", :resource_id => container_def.id) | ||
end | ||
end | ||
end | ||
|
||
# relationships | ||
rename_column :container_port_configs, :container_id, :container_definition_id | ||
rename_column :container_env_vars, :container_id, :container_definition_id | ||
|
||
# attributes | ||
remove_column :containers, :image | ||
remove_column :containers, :image_pull_policy | ||
remove_column :containers, :memory | ||
remove_column :containers, :cpu_cores | ||
remove_column :containers, :container_group_id | ||
remove_column :containers, :privileged | ||
remove_column :containers, :run_as_user | ||
remove_column :containers, :run_as_non_root | ||
remove_column :containers, :capabilities_add | ||
remove_column :containers, :capabilities_drop | ||
remove_column :containers, :command | ||
end | ||
end |
86 changes: 86 additions & 0 deletions
86
spec/migrations/20170529142557_unify_container_definition_and_container_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require_migration | ||
|
||
describe UnifyContainerDefinitionAndContainer do | ||
let(:container_stub) { migration_stub(:Container) } | ||
let(:container_definition_stub) { migration_stub(:ContainerDefinition) } | ||
|
||
let(:security_context_stub) { migration_stub(:SecurityContext) } | ||
let(:container_env_var_stub) { migration_stub(:ContainerEnvVar) } | ||
let(:container_port_config_stub) { migration_stub(:ContainerPortConfig) } | ||
|
||
let(:definition_hash) do | ||
{ | ||
:image => "my_image", | ||
:image_pull_policy => "image_pull_policy", | ||
:memory => "memory", | ||
:cpu_cores => 1.1, | ||
:container_group_id => 1, | ||
:privileged => true, | ||
:run_as_user => 2, | ||
:run_as_non_root => true, | ||
:capabilities_add => "capabilities_add", | ||
:capabilities_drop => "capabilities_drop", | ||
:command => "command" | ||
} | ||
end | ||
|
||
let(:container_hash) do | ||
{ | ||
:name => "mycontainer", | ||
:ems_ref => "123", | ||
:ems_id => 1, | ||
:old_ems_id => 1, | ||
:deleted_on => Time.now.utc | ||
} | ||
end | ||
|
||
migration_context :up do | ||
it "moves container definition columns to container" do | ||
container_def = container_definition_stub.create!(definition_hash) | ||
container = container_stub.create!(container_hash.merge(:container_definition => container_def)) | ||
container_env_var_stub.create!(:name => "REGISTRY_HTTP_ADDR", | ||
:value => ":5000", :field_path => nil, | ||
:container_definition_id => container_def.id) | ||
container_port_config_stub.create!(:ems_ref => "2da0c9e4", | ||
:port => 5000, | ||
:protocol => "TCP", | ||
:container_definition_id => container_def.id) | ||
security_context_stub.create!(:resource_type => "ContainerDefinition", | ||
:resource_id => container_def.id, | ||
:se_linux_level => "s0:c1,c0") | ||
|
||
migrate | ||
|
||
expect(container_stub.first).to have_attributes(definition_hash.merge(container_hash)) | ||
expect(container_env_var_stub.first).to have_attributes(:container_id => container.id) | ||
expect(container_port_config_stub.first).to have_attributes(:container_id => container.id) | ||
expect(security_context_stub.first).to have_attributes(:resource_type => "Container", | ||
:resource_id => container.id) | ||
end | ||
end | ||
|
||
migration_context :down do | ||
it "splits container_definition columns from container" do | ||
container = container_stub.create!(definition_hash.merge(container_hash)) | ||
container_env_var_stub.create!(:name => "REGISTRY_HTTP_ADDR", | ||
:value => ":5000", :field_path => nil, | ||
:container_id => container.id) | ||
container_port_config_stub.create!(:ems_ref => "2da0c9e4", | ||
:port => 5000, | ||
:protocol => "TCP", | ||
:container_id => container.id) | ||
security_context_stub.create!(:resource_type => "Container", | ||
:resource_id => container.id, | ||
:se_linux_level => "s0:c1,c0") | ||
|
||
migrate | ||
|
||
container_def = container_definition_stub.first | ||
expect(container_def).to have_attributes(definition_hash) | ||
expect(container_env_var_stub.first).to have_attributes(:container_definition_id => container_def.id) | ||
expect(container_port_config_stub.first).to have_attributes(:container_definition_id => container_def.id) | ||
expect(security_context_stub.first).to have_attributes(:resource_type => "ContainerDefinition", | ||
:resource_id => container_def.id) | ||
end | ||
end | ||
end |