diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-07-17 15:23:59 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-07-18 15:38:54 +0200 |
commit | c156030ef965bed019def3993ee21d214fe2f2ba (patch) | |
tree | 2923a0a90a73ce6509f40397874f7a4483e0393e /lib/gitlab/background_migration | |
parent | 79f591df4dfd6577c55d3bb843e423bba859e9b9 (diff) | |
download | gitlab-ce-c156030ef965bed019def3993ee21d214fe2f2ba.tar.gz |
Add a background migration to rename `uploads` in the uploads table
Diffstat (limited to 'lib/gitlab/background_migration')
-rw-r--r-- | lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb b/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb new file mode 100644 index 00000000000..601c874bc9b --- /dev/null +++ b/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb @@ -0,0 +1,47 @@ +module Gitlab + module BackgroundMigration + class MigrateSystemUploadsToNewFolder + include Gitlab::Database::MigrationHelpers + attr_reader :old_folder, :new_folder + + def perform(old_folder, new_folder) + @old_folder = old_folder + @new_folder = new_folder + + replace_sql = replace_sql(uploads[:path], old_folder, new_folder) + + while remaining_rows > 0 + sql = "UPDATE uploads "\ + "SET path = #{replace_sql} "\ + "WHERE uploads.id IN "\ + " (SELECT uploads.id FROM uploads "\ + " WHERE #{affected_uploads.to_sql} LIMIT 1000)" + connection.execute(sql) + end + end + + def uploads + Arel::Table.new('uploads') + end + + def remaining_rows + remaining_result = connection.exec_query("SELECT count(id) FROM uploads WHERE #{affected_uploads.to_sql}") + remaining = remaining_result.first['count'].to_i + logger.info "#{remaining} uploads remaining" + remaining + end + + def affected_uploads + uploads[:path].matches("#{old_folder}%") + end + + def connection + ActiveRecord::Base.connection + end + + def logger + Sidekiq.logger || Rails.logger || Logger.new(STDOUT) + end + end + end +end |