diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-14 09:06:26 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-14 09:06:26 +0300 |
commit | d9251dac4c8f9e27f1e2db4b537d47d7d5a9e5be (patch) | |
tree | a57ef4e98f815acc864bfd523e1436d3774fa05a /db | |
parent | 29c01c6c91558358c37ba45b03f240632bfb918d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
-rw-r--r-- | db/post_migrate/20191008143850_fix_any_approver_rule_for_projects.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/db/post_migrate/20191008143850_fix_any_approver_rule_for_projects.rb b/db/post_migrate/20191008143850_fix_any_approver_rule_for_projects.rb new file mode 100644 index 00000000000..c1f4b7e42ab --- /dev/null +++ b/db/post_migrate/20191008143850_fix_any_approver_rule_for_projects.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class FixAnyApproverRuleForProjects < ActiveRecord::Migration[5.2] + DOWNTIME = false + BATCH_SIZE = 1000 + + disable_ddl_transaction! + + class ApprovalProjectRule < ActiveRecord::Base + NON_EXISTENT_RULE_TYPE = 4 + ANY_APPROVER_RULE_TYPE = 3 + + include EachBatch + + self.table_name = 'approval_project_rules' + + scope :any_approver, -> { where(rule_type: ANY_APPROVER_RULE_TYPE) } + scope :non_existent_rule_type, -> { where(rule_type: NON_EXISTENT_RULE_TYPE) } + end + + def up + return unless Gitlab.ee? + + # Remove approval project rule with rule type 4 if the project has a rule with rule_type 3 + # + # Currently, there is no projects on gitlab.com which have both rules with 3 and 4 rule type + # There's a code-level validation for a rule, which doesn't allow to create rules with the same names + # + # But in order to avoid failing the update query due to uniqueness constraint + # Let's run the delete query to be sure + project_ids = FixAnyApproverRuleForProjects::ApprovalProjectRule.any_approver.select(:project_id) + FixAnyApproverRuleForProjects::ApprovalProjectRule + .non_existent_rule_type + .where(project_id: project_ids) + .delete_all + + # Set approval project rule types to 3 + # Currently there are 18_445 records to be updated + FixAnyApproverRuleForProjects::ApprovalProjectRule.non_existent_rule_type.each_batch(of: BATCH_SIZE) do |rules| + rules.update_all(rule_type: FixAnyApproverRuleForProjects::ApprovalProjectRule::ANY_APPROVER_RULE_TYPE) + end + end + + def down + # The migration doesn't leave the database in an inconsistent state + # And can be run multiple times + end +end |