Reset column information after the schema is migrated in MigrationsHelpers.schema_migrate_up!

Signed-off-by: 's avatarRémy Coutable <remy@rymai.me>
parent 826105df
......@@ -15,10 +15,6 @@ describe Gitlab::BackgroundMigration::DeserializeMergeRequestDiffsAndCommits, :m
.to receive(:commits_count=).and_return(nil)
end
after do
[Project, MergeRequest, MergeRequestDiff].each(&:reset_column_information)
end
def diffs_to_hashes(diffs)
diffs.as_json(only: Gitlab::Git::Diff::SERIALIZE_KEYS).map(&:with_indifferent_access)
end
......
......@@ -7,10 +7,6 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestMetricsWithEventsData,
.to receive(:commits_count=).and_return(nil)
end
after do
[Project, MergeRequest, MergeRequestDiff].each(&:reset_column_information)
end
describe '#perform' do
let(:mr_with_event) { create(:merge_request) }
let!(:merged_event) { create(:event, :merged, target: mr_with_event) }
......
......@@ -21,7 +21,7 @@ describe ConvertCustomNotificationSettingsToColumns, :migration do
events[event] = true
end
user = build(:user).becomes(user_class).tap(&:save!)
user = user_class.create!(email: "user-#{SecureRandom.hex}@example.org", username: "user-#{SecureRandom.hex}", encrypted_password: '12345678')
create_params = { user_id: user.id, level: params[:level], events: events }
notification_setting = described_class::NotificationSetting.create(create_params)
......@@ -37,7 +37,7 @@ describe ConvertCustomNotificationSettingsToColumns, :migration do
events[event] = true
end
user = build(:user).becomes(user_class).tap(&:save!)
user = user_class.create!(email: "user-#{SecureRandom.hex}@example.org", username: "user-#{SecureRandom.hex}", encrypted_password: '12345678')
create_params = events.merge(user_id: user.id, level: params[:level])
notification_setting = described_class::NotificationSetting.create(create_params)
......
......@@ -15,18 +15,22 @@ module MigrationsHelpers
ActiveRecord::Migrator.migrations(migrations_paths)
end
def reset_column_in_migration_models
def clear_schema_cache!
ActiveRecord::Base.connection_pool.connections.each do |conn|
conn.schema_cache.clear!
end
end
described_class.constants.sort.each do |name|
const = described_class.const_get(name)
def reset_column_in_all_models
clear_schema_cache!
if const.is_a?(Class) && const < ActiveRecord::Base
const.reset_column_information
end
end
# Reset column information for the most offending classes **after** we
# migrated the schema up, otherwise, column information could be outdated
ActiveRecord::Base.descendants.each { |klass| klass.reset_column_information }
# Without that, we get errors because of missing attributes, e.g.
# super: no superclass method `elasticsearch_indexing' for #<ApplicationSetting:0x00007f85628508d8>
ApplicationSetting.define_attribute_methods
end
def previous_migration
......@@ -45,7 +49,7 @@ module MigrationsHelpers
migration_schema_version)
end
reset_column_in_migration_models
reset_column_in_all_models
end
def schema_migrate_up!
......@@ -53,7 +57,7 @@ module MigrationsHelpers
ActiveRecord::Migrator.migrate(migrations_paths)
end
reset_column_in_migration_models
reset_column_in_all_models
end
def disable_migrations_output
......
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