Commit 76d3607f authored by pbair's avatar pbair

Raise error on missing column in MigrationsHelper

Change MigrationsHelper#column_for to always raise an error if column
being looked for doesn't exist.
parent b09e26d3
......@@ -750,7 +750,7 @@ module Gitlab
check_trigger_permissions!(table)
old_col = column_for(table, old_column, raise_if_missing: true)
old_col = column_for(table, old_column)
new_type = type || old_col.type
max_index = 0
......@@ -922,11 +922,11 @@ module Gitlab
end
# Returns the column for the given table and column name.
def column_for(table, name, raise_if_missing: false)
def column_for(table, name)
name = name.to_s
column = columns(table).find { |column| column.name == name }
raise(missing_schema_object_message(table, "column", name)) if column.nil? && raise_if_missing
raise(missing_schema_object_message(table, "column", name)) if column.nil?
column
end
......@@ -1166,7 +1166,7 @@ into similar problems in the future (e.g. when new tables are created).
end
def create_column_from(table, old, new, type: nil)
old_col = column_for(table, old, raise_if_missing: true)
old_col = column_for(table, old)
new_type = type || old_col.type
add_column(table, new, new_type,
......
......@@ -588,6 +588,8 @@ describe Gitlab::Database::MigrationHelpers do
end
describe '#add_column_with_default' do
let(:column) { Project.columns.find { |c| c.name == "id" } }
context 'outside of a transaction' do
context 'when a column limit is not set' do
before do
......@@ -602,6 +604,9 @@ describe Gitlab::Database::MigrationHelpers do
expect(model).to receive(:change_column_default)
.with(:projects, :foo, 10)
expect(model).to receive(:column_for)
.with(:projects, :foo).and_return(column)
end
it 'adds the column while allowing NULL values' do
......@@ -653,9 +658,12 @@ describe Gitlab::Database::MigrationHelpers do
end
context 'when a column limit is set' do
let(:column) { Project.columns.find { |c| c.name == "id" } }
it 'adds the column with a limit' do
allow(model).to receive(:transaction_open?).and_return(false)
allow(model).to receive(:transaction).and_yield
allow(model).to receive(:column_for).with(:projects, :foo).and_return(column)
allow(model).to receive(:update_column_in_batches).with(:projects, :foo, 10)
allow(model).to receive(:change_column_null).with(:projects, :foo, false)
allow(model).to receive(:change_column_default).with(:projects, :foo, 10)
......@@ -1152,8 +1160,9 @@ describe Gitlab::Database::MigrationHelpers do
expect(column.name).to eq('id')
end
it 'returns nil when a column does not exist' do
expect(model.column_for(:users, :kittens)).to be_nil
it 'raises an error when a column does not exist' do
error_message = /Could not find column "kittens" on table "users"/
expect { model.column_for(:users, :kittens) }.to raise_error(error_message)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment