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

Add a STI disabled spec #245

Merged
merged 2 commits into from
Aug 9, 2018
Merged
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
@@ -33,7 +33,10 @@ class MiqWorker < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class Authentication < ActiveRecord::Base; end
class Authentication < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class MiqQueue < ActiveRecord::Base; end

def up
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class RemoveCentralAdminRegionAuthRecords < ActiveRecord::Migration[5.0]
class Authentication < ActiveRecord::Base; end
class Authentication < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def up
Authentication.where(:resource_type => 'MiqRegion').delete_all
Original file line number Diff line number Diff line change
@@ -22,7 +22,10 @@ class MiqWorker < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class Authentication < ActiveRecord::Base; end
class Authentication < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

class MiqQueue < ActiveRecord::Base; end

def up
4 changes: 3 additions & 1 deletion db/migrate/20170222214902_add_initiator_to_service.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class AddInitiatorToService < ActiveRecord::Migration[5.0]
class Service < ActiveRecord::Base; end
class Service < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def up
add_column :services, :initiator, :string, :comment => "Entity that initiated the service creation"
4 changes: 3 additions & 1 deletion db/migrate/20170409083720_add_enabled_field_to_ems.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class AddEnabledFieldToEms < ActiveRecord::Migration[5.0]
class ExtManagementSystem < ActiveRecord::Base; end
class ExtManagementSystem < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def change
add_column :ext_management_systems, :enabled, :boolean
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ class ContainerGroup < ActiveRecord::Base
end

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

class ContainerProject < ActiveRecord::Base
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ class ContainerDefinition < ActiveRecord::Base
end

class Container < ActiveRecord::Base
self.inheritance_column = :_type_disabled
belongs_to :container_definition, :class_name => 'UnifyContainerDefinitionAndContainer::ContainerDefinition'
end

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class DowncaseResourceGroupEmsRefForAzure < ActiveRecord::Migration[5.0]
class ResourceGroup < ActiveRecord::Base; end
class ResourceGroup < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def up
say_with_time("Downcase ems_ref for Azure resource groups") do
9 changes: 9 additions & 0 deletions spec/support/database_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module DatabaseHelper
def self.columns_for_table(table_name)
ActiveRecord::Base.connection.data_source_exists?(table_name) ? ActiveRecord::Base.connection.columns(table_name) : []
end

def self.table_has_type_column?(table_name)
columns_for_table(table_name).detect { |i| i.name == "type" }
end
end
16 changes: 16 additions & 0 deletions spec/support/migration_helper.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,22 @@ def migration_context(direction, &block)

it("with empty tables") { migrate }

it "STI is disabled for all involved ActiveRecord::Base descendants" do
classes = described_class.constants.collect do |symbol|
const = described_class.const_get(symbol)
const if const.kind_of?(Class) && const < ActiveRecord::Base
end.compact

classes_with_type_column_before = classes.select { |klass| DatabaseHelper.table_has_type_column?(klass.table_name) }

migrate

classes_with_type_column_after = classes.select { |klass| DatabaseHelper.table_has_type_column?(klass.table_name) }
(classes_with_type_column_before | classes_with_type_column_after).each do |klass|
expect(klass.inheritance_column.to_s).to eq("_type_disabled"), "The line `self.inheritance_column = :_type_disabled` is missing from the definition of: #{klass.name}"
end
end

instance_eval(&block)
end
end