From 2074f39a47645b5ea453adfba84247f2fcc4f3c7 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Wed, 16 Aug 2017 14:46:26 +0200 Subject: Migration to remove pending delete projects with non-existing namespace There might be some projects where the namespace was removed, but the project wasn't. For these the projects still have a `namespace_id` set. So this adds a post-deploy migration remove all projects that are pending delete, and have a `namespace_id` that is non-existing. --- ...onexisting_namespace_pending_delete_projects.rb | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 db/post_migrate/20170816102555_cleanup_nonexisting_namespace_pending_delete_projects.rb (limited to 'db/post_migrate') diff --git a/db/post_migrate/20170816102555_cleanup_nonexisting_namespace_pending_delete_projects.rb b/db/post_migrate/20170816102555_cleanup_nonexisting_namespace_pending_delete_projects.rb new file mode 100644 index 00000000000..fe88f25827f --- /dev/null +++ b/db/post_migrate/20170816102555_cleanup_nonexisting_namespace_pending_delete_projects.rb @@ -0,0 +1,54 @@ +# Follow up of CleanupNamespacelessPendingDeleteProjects and it cleans +# all projects with `pending_delete = true` and for which the +# namespace no longer exists. +class CleanupNonexistingNamespacePendingDeleteProjects < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + @offset = 0 + + loop do + ids = pending_delete_batch + + break if ids.empty? + + args = ids.map { |id| Array(id) } + + NamespacelessProjectDestroyWorker.bulk_perform_async(args) + + @offset += 1 + end + end + + def down + # noop + end + + private + + def pending_delete_batch + connection.exec_query(find_batch).map { |row| row['id'].to_i } + end + + BATCH_SIZE = 5000 + + def find_batch + projects = Project.arel_table + namespaces = Namespace.arel_table + + namespace_query = namespaces.project(1) + .where(namespaces[:id].eq(projects[:namespace_id])) + .exists.not + + projects.project(projects[:id]) + .where(projects[:pending_delete].eq(true)) + .where(namespace_query) + .skip(@offset * BATCH_SIZE) + .take(BATCH_SIZE) + .to_sql + end +end -- cgit v1.2.3