summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/migration_helpers.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/database/migration_helpers.rb')
-rw-r--r--lib/gitlab/database/migration_helpers.rb19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb
index f75e943671b..82a84508959 100644
--- a/lib/gitlab/database/migration_helpers.rb
+++ b/lib/gitlab/database/migration_helpers.rb
@@ -215,7 +215,7 @@ module Gitlab
fk_name = name || concurrent_foreign_key_name(source, column)
unless foreign_key_exists?(source, name: fk_name)
- raise "cannot find #{fk_name} on #{source} table"
+ raise missing_schema_object_message(source, "foreign key", fk_name)
end
disable_statement_timeout do
@@ -931,7 +931,10 @@ module Gitlab
def column_for(table, name)
name = name.to_s
- columns(table).find { |column| column.name == name }
+ column = columns(table).find { |column| column.name == name }
+ raise(missing_schema_object_message(table, "column", name)) if column.nil?
+
+ column
end
# This will replace the first occurrence of a string in a column with
@@ -1166,6 +1169,18 @@ into similar problems in the future (e.g. when new tables are created).
private
+ def missing_schema_object_message(table, type, name)
+ <<~MESSAGE
+ Could not find #{type} "#{name}" on table "#{table}" which was referenced during the migration.
+ This issue could be caused by the database schema straying from the expected state.
+
+ To resolve this issue, please verify:
+ 1. all previous migrations have completed
+ 2. the database objects used in this migration match the Rails definition in schema.rb or structure.sql
+
+ MESSAGE
+ end
+
def tables_match?(target_table, foreign_key_table)
target_table.blank? || foreign_key_table == target_table
end