summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/post_migrate/20170502101023_clean_up_pending_delete_projects.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/db/post_migrate/20170502101023_clean_up_pending_delete_projects.rb b/db/post_migrate/20170502101023_clean_up_pending_delete_projects.rb
new file mode 100644
index 00000000000..52c7c160ea2
--- /dev/null
+++ b/db/post_migrate/20170502101023_clean_up_pending_delete_projects.rb
@@ -0,0 +1,47 @@
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class CleanUpPendingDeleteProjects < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ admin = User.find_by(admin: true)
+ return unless admin
+
+ Project.unscoped.where(pending_delete: true).each { |project| delete_project(project, admin) }
+ end
+
+ def down
+ # noop
+ end
+
+ private
+
+ def delete_project(project, user)
+ project.team.truncate
+
+ unlink_fork(project) if project.forked?
+
+ [:events, :issues, :merge_requests, :labels, :milestones, :notes, :snippets].each do |thing|
+ project.send(thing).delete_all
+ end
+
+ # Override Project#remove_pages for this instance so it doesn't do anything
+ def project.remove_pages
+ end
+
+ project.destroy!
+ end
+
+ def unlink_fork(project)
+ merge_requests = project.forked_from_project.merge_requests.opened.from_project(project)
+
+ merge_requests.update_all(state: 'closed')
+
+ project.forked_project_link.destroy
+ end
+end