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

Ensure id and *_id columns are bigint #248

Merged
merged 1 commit 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
41 changes: 41 additions & 0 deletions spec/automated_review/id_columns_are_bigint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'set'
describe "*_id columns" do
EXCEPTIONS_LIST = Set.new(
[
"authentications.ldap_id",
"authentications.rhsm_pool_id",
"cloud_networks.provider_segmentation_id",
"container_volumes.common_volume_id",
"miq_queue.task_id",
"network_ports.binding_host_id",
"scan_histories.task_id",
"sessions.session_id"
]
).freeze

it "should be type bigint" do
ActiveRecord::Base.connection.tables.each do |table|
next if ManageIQ::Schema::SYSTEM_TABLES.include?(table)
DatabaseHelper.columns_for_table(table).each do |column|
Copy link
Member

Choose a reason for hiding this comment

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

Moving forward, perhaps this should move into a DatabaseHelper method...

def DatabaseHelper.each_table
  ActiveRecord::Base.connection.tables.each do |table|
    next if ManageIQ::Schema::SYSTEM_TABLES.include?(table)
    yield table
  end
end

def DatabaseHelper.each_column
  each_table do |table|
    columns_for_table(table).each do |column|
      yield table, column
    end
  end
end

Then caller just does

DatabaseHelper.each_column do |table, column|
   ...
end

next unless column.name.end_with?("_id")
if "#{table}.#{column.name}".in?(EXCEPTIONS_LIST)
expect(column.sql_type).not_to eq("bigint"), "Thanks for fixing technical debt!!! Please remove '#{table}.#{column.name}' from EXCEPTIONS_LIST in #{__FILE__}"
else
expect(column.sql_type).to eq("bigint"), "Expected foreign key column #{table}.#{column.name} to be type 'bigint', got: '#{column.sql_type}'"
end
end
end
end
end

describe "id columns" do
it "should be type bigint" do
ActiveRecord::Base.connection.tables.each do |table|
next if ManageIQ::Schema::SYSTEM_TABLES.include?(table)
DatabaseHelper.columns_for_table(table).each do |column|
next unless column.name == "id"
expect(column.sql_type).to eq("bigint"), "Expected column #{table}.#{column.name} to be type 'bigint', got: '#{column.sql_type}'"
end
end
end
end