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

Don't load container models from app folder in migration #7312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
class AddCreatedOnForContainerEntities < ActiveRecord::Migration
CONTAINER_TABLES = [:container_nodes, :container_projects, :container_services, :container_routes, :container_groups,
:container_replicators, :container_quotas, :container_builds, :container_build_pods,
:container_limits, :container_volumes]
class ContainerNode < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI
end

class ContainerProject < ActiveRecord::Base; end
Copy link
Member

Choose a reason for hiding this comment

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

This was pre-rails 5, but in new migrations created with rails 5, I believe these should be < ApplicationRecord; end ... Right @matthewd?

Copy link
Contributor

Choose a reason for hiding this comment

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

No.

Part of the advantage of ApplicationRecord is that we can leave AR::Base unmolested, which is exactly what migrations want: a plain ordinary AR object with no app-code-of-the-day surprises.

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

@matthewd That is really interesting and good to know! I didn't even think of that.


class ContainerService < ActiveRecord::Base; end

class ContainerRoute < ActiveRecord::Base; end

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

class ContainerReplicator < ActiveRecord::Base; end

class ContainerQuota < ActiveRecord::Base; end

class ContainerBuild < ActiveRecord::Base; end

class ContainerBuildPod < ActiveRecord::Base; end

class ContainerLimit < ActiveRecord::Base; end

class ContainerVolume < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI
end

CONTAINER_MODELS = [ContainerNode, ContainerProject, ContainerService, ContainerRoute, ContainerGroup,
ContainerReplicator, ContainerQuota, ContainerBuild, ContainerBuildPod, ContainerLimit,
ContainerVolume].freeze

def change
CONTAINER_TABLES.each do |t|
add_column t, :created_on, :datetime
rename_column t, :creation_timestamp, :ems_created_on
CONTAINER_MODELS.each do |model|
add_column model.table_name, :created_on, :datetime
rename_column model.table_name, :creation_timestamp, :ems_created_on

say_with_time("adding created_on datetime to all existing #{t.to_s.tr("_", " ")}") do
t.to_s.classify.constantize.update_all("created_on=ems_created_on")
say_with_time("adding created_on datetime to all existing #{model}") do
model.update_all("created_on=ems_created_on")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_migration

describe AddCreatedOnForContainerEntities do
CONTAINER_TABLES = described_class::CONTAINER_MODELS.collect { |m| m.table_name.to_sym }

CONTAINER_TABLES.each { |table| let("#{table}_stub") { migration_stub(table.to_s.classify.to_sym) } }

let(:mock_timestamp) { Time.zone.parse('2016-03-09 12:00:38.711120') }
let(:mock_name) { "Name_1" }

migration_context :up do
it "populates new column created_on and ems_created_on with value from creation_timestamp" do
records = {}

CONTAINER_TABLES.each do |table|
records[table] = send("#{table}_stub").create!(:name => mock_name, :creation_timestamp => mock_timestamp)
end

migrate

CONTAINER_TABLES.each do |table|
expect(records[table].reload.created_on).to eq(mock_timestamp)
expect(records[table].reload.ems_created_on).to eq(mock_timestamp)
end
end
end

migration_context :down do
it "renames ems_created_on back to creation_timestamp and it has same value as ems_created_on" do
records = {}

CONTAINER_TABLES.each do |table|
records[table] = send("#{table}_stub").create!(:name => mock_name, :ems_created_on => mock_timestamp)
end

migrate

CONTAINER_TABLES.each do |table|
expect(records[table].reload.creation_timestamp).to eq(mock_timestamp)
end
end
end
end