From 8b18112b4e01b1b654bd8feee30e64d8dc835926 Mon Sep 17 00:00:00 2001
From: Abdul Wadood <awadood@gitlab.com>
Date: Tue, 6 Jun 2023 07:06:32 +0000
Subject: [PATCH] Fix false positives for Migration/SchemaAdditionMethodsNoPost
 cop

We allow `add_column` and `create_table` methods inside the `down`
method in post-deploy migrations. But if these methods were wrapped
inside a statement like `if` then the cop was raising an error.
For example, in the following, the 2nd example was being incorrectly
flagged as an offense:

```
def down
  add_column(:table, :column)
end
```

```
def down
  add_column(:table, :column) unless column_exists(:table, :column)
end
```
---
 .../20221027203951_drop_experiment_users_table.rb         | 2 +-
 .../20221114142602_drop_experiment_subjects_table.rb      | 2 +-
 db/post_migrate/20221114142616_drop_experiments_table.rb  | 2 +-
 ...21115184525_remove_namespaces_tmp_project_id_column.rb | 4 ++--
 .../20230104103748_remove_new_amount_used_column.rb       | 4 ++--
 ...w_amount_used_column_on_ci_namespace_monthly_usages.rb | 4 ++--
 .../20230201082038_drop_web_hook_calls_high_column.rb     | 2 --
 .../20230316185746_drop_packages_events_table.rb          | 2 +-
 ...230323131521_remove_machine_id_from_builds_metadata.rb | 2 +-
 ...e_application_settings_clickhouse_connection_string.rb | 2 --
 ...0501180958_drop_clusters_applications_cert_managers.rb | 2 --
 .../20230502134532_drop_clusters_applications_cilium.rb   | 2 --
 ...0230502182754_drop_clusters_applications_crossplane.rb | 2 --
 .../20230502193525_drop_clusters_applications_helm.rb     | 2 --
 .../20230502201251_drop_clusters_applications_ingress.rb  | 2 --
 .../20230503115918_drop_clusters_applications_jupyter.rb  | 2 --
 .../20230503152349_drop_clusters_applications_knative.rb  | 2 --
 ...0230503173101_drop_clusters_applications_prometheus.rb | 2 --
 .../20230503175406_drop_clusters_applications_runners.rb  | 2 --
 .../20230503181808_drop_serverless_domain_cluster.rb      | 2 --
 .../20230510130050_remove_ci_triggers_ref_column.rb       | 2 --
 .../20230518121320_remove_time_format_in_24h_column.rb    | 2 --
 ...5245_remove_project_ci_cd_setting_opt_in_jwt_column.rb | 2 --
 ...63059_remove_broadcast_messages_namespace_id_column.rb | 2 --
 rubocop/cop/migration/schema_addition_methods_no_post.rb  | 4 ++--
 .../cop/migration/schema_addition_methods_no_post_spec.rb | 8 ++++++++
 26 files changed, 21 insertions(+), 45 deletions(-)

diff --git a/db/post_migrate/20221027203951_drop_experiment_users_table.rb b/db/post_migrate/20221027203951_drop_experiment_users_table.rb
index 95455db98e5c6..838a960574004 100644
--- a/db/post_migrate/20221027203951_drop_experiment_users_table.rb
+++ b/db/post_migrate/20221027203951_drop_experiment_users_table.rb
@@ -6,7 +6,7 @@ def up
   end
 
   def down
-    create_table :experiment_users do |t| # rubocop:disable Migration/SchemaAdditionMethodsNoPost
+    create_table :experiment_users do |t|
       t.bigint :experiment_id, null: false
       t.bigint :user_id, null: false
       t.integer :group_type, limit: 2, null: false, default: 0
diff --git a/db/post_migrate/20221114142602_drop_experiment_subjects_table.rb b/db/post_migrate/20221114142602_drop_experiment_subjects_table.rb
index 371f214de6de7..f7c10192afb92 100644
--- a/db/post_migrate/20221114142602_drop_experiment_subjects_table.rb
+++ b/db/post_migrate/20221114142602_drop_experiment_subjects_table.rb
@@ -7,7 +7,7 @@ def up
 
   def down
     unless table_exists?(:experiment_subjects)
-      create_table :experiment_subjects do |t| # rubocop:disable Migration/SchemaAdditionMethodsNoPost
+      create_table :experiment_subjects do |t|
         t.bigint :experiment_id, null: false
         t.bigint :user_id
         t.bigint :project_id
diff --git a/db/post_migrate/20221114142616_drop_experiments_table.rb b/db/post_migrate/20221114142616_drop_experiments_table.rb
index da6c112249402..a8280eb9f6954 100644
--- a/db/post_migrate/20221114142616_drop_experiments_table.rb
+++ b/db/post_migrate/20221114142616_drop_experiments_table.rb
@@ -9,7 +9,7 @@ def up
 
   def down
     unless table_exists?(:experiments)
-      create_table :experiments do |t| # rubocop:disable Migration/SchemaAdditionMethodsNoPost
+      create_table :experiments do |t|
         t.text :name, null: false
 
         t.index :name, unique: true
diff --git a/db/post_migrate/20221115184525_remove_namespaces_tmp_project_id_column.rb b/db/post_migrate/20221115184525_remove_namespaces_tmp_project_id_column.rb
index 01424f8113f71..d044c44e95e36 100644
--- a/db/post_migrate/20221115184525_remove_namespaces_tmp_project_id_column.rb
+++ b/db/post_migrate/20221115184525_remove_namespaces_tmp_project_id_column.rb
@@ -14,9 +14,9 @@ def up
   def down
     unless column_exists?(:namespaces, :tmp_project_id)
       with_lock_retries do
-        # rubocop:disable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+        # rubocop:disable Migration/AddColumnsToWideTables
         add_column :namespaces, :tmp_project_id, :integer
-        # rubocop:enable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+        # rubocop:enable Migration/AddColumnsToWideTables
       end
     end
 
diff --git a/db/post_migrate/20230104103748_remove_new_amount_used_column.rb b/db/post_migrate/20230104103748_remove_new_amount_used_column.rb
index 0aaa7c1bd8f90..b93879c2b5039 100644
--- a/db/post_migrate/20230104103748_remove_new_amount_used_column.rb
+++ b/db/post_migrate/20230104103748_remove_new_amount_used_column.rb
@@ -10,10 +10,10 @@ def up
   def down
     return if column_exists?(:ci_project_monthly_usages, :new_amount_used)
 
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+    # rubocop:disable Migration/AddColumnsToWideTables
     add_column :ci_project_monthly_usages, :new_amount_used, :decimal, default: 0.0,
                                                                        precision: 18, scale: 2, null: false
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+    # rubocop:enable Migration/AddColumnsToWideTables
 
     install_rename_triggers :ci_project_monthly_usages, :amount_used, :new_amount_used, trigger_name: TRIGGER_NAME
   end
diff --git a/db/post_migrate/20230105180002_remove_new_amount_used_column_on_ci_namespace_monthly_usages.rb b/db/post_migrate/20230105180002_remove_new_amount_used_column_on_ci_namespace_monthly_usages.rb
index cebda3e353e25..11e212a697d31 100644
--- a/db/post_migrate/20230105180002_remove_new_amount_used_column_on_ci_namespace_monthly_usages.rb
+++ b/db/post_migrate/20230105180002_remove_new_amount_used_column_on_ci_namespace_monthly_usages.rb
@@ -12,10 +12,10 @@ def up
   def down
     return if column_exists?(:ci_namespace_monthly_usages, :new_amount_used)
 
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+    # rubocop:disable Migration/AddColumnsToWideTables
     add_column :ci_namespace_monthly_usages, :new_amount_used, :decimal, default: 0.0,
                                                                        precision: 18, scale: 2, null: false
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost, Migration/AddColumnsToWideTables
+    # rubocop:enable Migration/AddColumnsToWideTables
 
     install_rename_triggers :ci_namespace_monthly_usages, :amount_used, :new_amount_used, trigger_name: TRIGGER_NAME
   end
diff --git a/db/post_migrate/20230201082038_drop_web_hook_calls_high_column.rb b/db/post_migrate/20230201082038_drop_web_hook_calls_high_column.rb
index 7f90b3293d8fc..76c93be0f4233 100644
--- a/db/post_migrate/20230201082038_drop_web_hook_calls_high_column.rb
+++ b/db/post_migrate/20230201082038_drop_web_hook_calls_high_column.rb
@@ -14,9 +14,7 @@ def up
   def down
     with_lock_retries do
       unless column_exists?(:plan_limits, :web_hook_calls_high)
-        # rubocop:disable Migration/SchemaAdditionMethodsNoPost
         add_column :plan_limits, :web_hook_calls_high, :integer, default: 0
-        # rubocop:enable Migration/SchemaAdditionMethodsNoPost
       end
     end
   end
diff --git a/db/post_migrate/20230316185746_drop_packages_events_table.rb b/db/post_migrate/20230316185746_drop_packages_events_table.rb
index bb51fc7ae9c5b..f8b37651249f5 100644
--- a/db/post_migrate/20230316185746_drop_packages_events_table.rb
+++ b/db/post_migrate/20230316185746_drop_packages_events_table.rb
@@ -8,7 +8,7 @@ def up
   def down
     return if table_exists?(:packages_events)
 
-    create_table :packages_events do |t| # rubocop:disable Migration/SchemaAdditionMethodsNoPost
+    create_table :packages_events do |t|
       t.integer :event_type, limit: 2, null: false
       t.integer :event_scope, limit: 2, null: false
       t.integer :originator_type, limit: 2, null: false
diff --git a/db/post_migrate/20230323131521_remove_machine_id_from_builds_metadata.rb b/db/post_migrate/20230323131521_remove_machine_id_from_builds_metadata.rb
index f034bc2184731..41d2373580795 100644
--- a/db/post_migrate/20230323131521_remove_machine_id_from_builds_metadata.rb
+++ b/db/post_migrate/20230323131521_remove_machine_id_from_builds_metadata.rb
@@ -14,7 +14,7 @@ def up
   end
 
   def down
-    add_column :p_ci_builds_metadata, :runner_machine_id, :bigint, if_not_exists: true # rubocop: disable Migration/SchemaAdditionMethodsNoPost
+    add_column :p_ci_builds_metadata, :runner_machine_id, :bigint, if_not_exists: true
 
     add_concurrent_partitioned_index :p_ci_builds_metadata, :runner_machine_id, name: INDEX_NAME,
       where: 'runner_machine_id IS NOT NULL'
diff --git a/db/post_migrate/20230425114355_remove_application_settings_clickhouse_connection_string.rb b/db/post_migrate/20230425114355_remove_application_settings_clickhouse_connection_string.rb
index d5bdd33a95285..3c27c48a7f3df 100644
--- a/db/post_migrate/20230425114355_remove_application_settings_clickhouse_connection_string.rb
+++ b/db/post_migrate/20230425114355_remove_application_settings_clickhouse_connection_string.rb
@@ -9,9 +9,7 @@ def up
 
   def down
     unless column_exists?(:application_settings, :clickhouse_connection_string)
-      # rubocop:disable Migration/SchemaAdditionMethodsNoPost
       add_column :application_settings, :clickhouse_connection_string, :text
-      # rubocop:enable Migration/SchemaAdditionMethodsNoPost
     end
 
     add_text_limit :application_settings, :clickhouse_connection_string, 1024
diff --git a/db/post_migrate/20230501180958_drop_clusters_applications_cert_managers.rb b/db/post_migrate/20230501180958_drop_clusters_applications_cert_managers.rb
index 8949e37dcc357..3eb5fc13a2a21 100644
--- a/db/post_migrate/20230501180958_drop_clusters_applications_cert_managers.rb
+++ b/db/post_migrate/20230501180958_drop_clusters_applications_cert_managers.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init migration:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L680-L689
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table "clusters_applications_cert_managers", id: :serial, force: :cascade do |t|
       t.integer "cluster_id", null: false
@@ -23,5 +22,4 @@ def down
       t.index ["cluster_id"], name: "index_clusters_applications_cert_managers_on_cluster_id", unique: true
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230502134532_drop_clusters_applications_cilium.rb b/db/post_migrate/20230502134532_drop_clusters_applications_cilium.rb
index 8d80bae0a5230..2c7b7d89bf5fc 100644
--- a/db/post_migrate/20230502134532_drop_clusters_applications_cilium.rb
+++ b/db/post_migrate/20230502134532_drop_clusters_applications_cilium.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on original migration:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20200615234047_create_clusters_applications_cilium.rb
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table :clusters_applications_cilium do |t|
       t.references :cluster, null: false, index: { unique: true }
@@ -19,5 +18,4 @@ def down
       t.text :status_reason # rubocop:disable Migration/AddLimitToTextColumns
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230502182754_drop_clusters_applications_crossplane.rb b/db/post_migrate/20230502182754_drop_clusters_applications_crossplane.rb
index c35cfc8fd963a..1eeb7770c82a5 100644
--- a/db/post_migrate/20230502182754_drop_clusters_applications_crossplane.rb
+++ b/db/post_migrate/20230502182754_drop_clusters_applications_crossplane.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on original migration:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/8b1637296b286a5c46e0d8fdf6da42a43a7c9986/db/migrate/20191017191341_create_clusters_applications_crossplane.rb
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table :clusters_applications_crossplane, id: :integer do |t|
       t.timestamps_with_timezone null: false
@@ -22,5 +21,4 @@ def down
       t.index :cluster_id, unique: true
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230502193525_drop_clusters_applications_helm.rb b/db/post_migrate/20230502193525_drop_clusters_applications_helm.rb
index 21df851229c63..c435b356e0102 100644
--- a/db/post_migrate/20230502193525_drop_clusters_applications_helm.rb
+++ b/db/post_migrate/20230502193525_drop_clusters_applications_helm.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init schema:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L691-L702
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   # rubocop:disable Migration/Datetime
   def down
     create_table "clusters_applications_helm", id: :serial, force: :cascade do |t|
@@ -26,6 +25,5 @@ def down
       t.index ["cluster_id"], name: "index_clusters_applications_helm_on_cluster_id", unique: true
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
   # rubocop:enable Migration/Datetime
 end
diff --git a/db/post_migrate/20230502201251_drop_clusters_applications_ingress.rb b/db/post_migrate/20230502201251_drop_clusters_applications_ingress.rb
index a349346f91b5b..3ccc536cb7e77 100644
--- a/db/post_migrate/20230502201251_drop_clusters_applications_ingress.rb
+++ b/db/post_migrate/20230502201251_drop_clusters_applications_ingress.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init schema:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L704-L715
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   # rubocop:disable Migration/Datetime
   def down
     create_table "clusters_applications_ingress", id: :serial, force: :cascade do |t|
@@ -27,6 +26,5 @@ def down
       t.index ["cluster_id"], name: "index_clusters_applications_ingress_on_cluster_id", unique: true
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
   # rubocop:enable Migration/Datetime
 end
diff --git a/db/post_migrate/20230503115918_drop_clusters_applications_jupyter.rb b/db/post_migrate/20230503115918_drop_clusters_applications_jupyter.rb
index 3c2a0cc3ee844..bd98aa6d19ca8 100644
--- a/db/post_migrate/20230503115918_drop_clusters_applications_jupyter.rb
+++ b/db/post_migrate/20230503115918_drop_clusters_applications_jupyter.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init schema:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L717-L728
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table "clusters_applications_jupyter", id: :serial, force: :cascade do |t|
       t.integer "cluster_id", null: false
@@ -25,5 +24,4 @@ def down
       t.index ["oauth_application_id"], name: "index_clusters_applications_jupyter_on_oauth_application_id"
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230503152349_drop_clusters_applications_knative.rb b/db/post_migrate/20230503152349_drop_clusters_applications_knative.rb
index c94b9bba64b80..e8473b7440851 100644
--- a/db/post_migrate/20230503152349_drop_clusters_applications_knative.rb
+++ b/db/post_migrate/20230503152349_drop_clusters_applications_knative.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init migration:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L730-L740
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table "clusters_applications_knative", id: :serial, force: :cascade do |t|
       t.integer "cluster_id", null: false
@@ -25,5 +24,4 @@ def down
       t.index ["cluster_id"], name: "index_clusters_applications_knative_on_cluster_id", unique: true
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230503173101_drop_clusters_applications_prometheus.rb b/db/post_migrate/20230503173101_drop_clusters_applications_prometheus.rb
index 6391c1ee5ae04..b62452f2052e7 100644
--- a/db/post_migrate/20230503173101_drop_clusters_applications_prometheus.rb
+++ b/db/post_migrate/20230503173101_drop_clusters_applications_prometheus.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init schema:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L742-L750
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table "clusters_applications_prometheus", id: :serial, force: :cascade do |t|
       t.integer "cluster_id", null: false
@@ -26,5 +25,4 @@ def down
       t.boolean "healthy"
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230503175406_drop_clusters_applications_runners.rb b/db/post_migrate/20230503175406_drop_clusters_applications_runners.rb
index 813cb23f56ad7..a4ab75dbce5af 100644
--- a/db/post_migrate/20230503175406_drop_clusters_applications_runners.rb
+++ b/db/post_migrate/20230503175406_drop_clusters_applications_runners.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on init schema:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/b237f836df215a4ada92b9406733e6cd2483ca2d/db/migrate/20181228175414_init_schema.rb#L752-L763
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table "clusters_applications_runners", id: :serial, force: :cascade do |t|
       t.integer "cluster_id", null: false
@@ -25,5 +24,4 @@ def down
       t.index ["runner_id"], name: "index_clusters_applications_runners_on_runner_id"
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230503181808_drop_serverless_domain_cluster.rb b/db/post_migrate/20230503181808_drop_serverless_domain_cluster.rb
index 94d3a2faf3bbd..003f2d91b549e 100644
--- a/db/post_migrate/20230503181808_drop_serverless_domain_cluster.rb
+++ b/db/post_migrate/20230503181808_drop_serverless_domain_cluster.rb
@@ -10,7 +10,6 @@ def up
 
   # Based on original migration:
   # https://gitlab.com/gitlab-org/gitlab/-/blob/5f7bd5b1455d87e2f1a2d1ae2c1147d51e97aa55/db/migrate/20191127030005_create_serverless_domain_cluster.rb
-  # rubocop:disable Migration/SchemaAdditionMethodsNoPost
   def down
     create_table :serverless_domain_cluster, id: false, primary_key: :uuid do |t|
       t.string :uuid, null: false, limit: 14, primary_key: true
@@ -29,5 +28,4 @@ def down
       t.index :creator_id, name: 'index_serverless_domain_cluster_on_creator_id'
     end
   end
-  # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 end
diff --git a/db/post_migrate/20230510130050_remove_ci_triggers_ref_column.rb b/db/post_migrate/20230510130050_remove_ci_triggers_ref_column.rb
index fa4fab5b8df30..84b69f7bc86e8 100644
--- a/db/post_migrate/20230510130050_remove_ci_triggers_ref_column.rb
+++ b/db/post_migrate/20230510130050_remove_ci_triggers_ref_column.rb
@@ -8,8 +8,6 @@ def up
   end
 
   def down
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost
     add_column :ci_triggers, :ref, :string, if_not_exists: true
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost
   end
 end
diff --git a/db/post_migrate/20230518121320_remove_time_format_in_24h_column.rb b/db/post_migrate/20230518121320_remove_time_format_in_24h_column.rb
index 60abf4b6d8bc7..489c2446fe278 100644
--- a/db/post_migrate/20230518121320_remove_time_format_in_24h_column.rb
+++ b/db/post_migrate/20230518121320_remove_time_format_in_24h_column.rb
@@ -8,8 +8,6 @@ def up
   end
 
   def down
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost
     add_column :user_preferences, :time_format_in_24h, :boolean
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost
   end
 end
diff --git a/db/post_migrate/20230523125245_remove_project_ci_cd_setting_opt_in_jwt_column.rb b/db/post_migrate/20230523125245_remove_project_ci_cd_setting_opt_in_jwt_column.rb
index 7f7d58edc1b76..2826077d6f073 100644
--- a/db/post_migrate/20230523125245_remove_project_ci_cd_setting_opt_in_jwt_column.rb
+++ b/db/post_migrate/20230523125245_remove_project_ci_cd_setting_opt_in_jwt_column.rb
@@ -8,8 +8,6 @@ def up
   end
 
   def down
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost
     add_column(:project_ci_cd_settings, :opt_in_jwt, :boolean, default: false, null: false, if_not_exists: true)
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost
   end
 end
diff --git a/db/post_migrate/20230602063059_remove_broadcast_messages_namespace_id_column.rb b/db/post_migrate/20230602063059_remove_broadcast_messages_namespace_id_column.rb
index ad7e23b7cb168..144b16e480621 100644
--- a/db/post_migrate/20230602063059_remove_broadcast_messages_namespace_id_column.rb
+++ b/db/post_migrate/20230602063059_remove_broadcast_messages_namespace_id_column.rb
@@ -10,9 +10,7 @@ def up
   end
 
   def down
-    # rubocop:disable Migration/SchemaAdditionMethodsNoPost
     add_column :broadcast_messages, :namespace_id, :bigint unless column_exists?(:broadcast_messages, :namespace_id)
-    # rubocop:enable Migration/SchemaAdditionMethodsNoPost
 
     add_concurrent_index :broadcast_messages, :namespace_id, name: INDEX_NAME
   end
diff --git a/rubocop/cop/migration/schema_addition_methods_no_post.rb b/rubocop/cop/migration/schema_addition_methods_no_post.rb
index c35b82f91573a..53b47dc99dc3e 100644
--- a/rubocop/cop/migration/schema_addition_methods_no_post.rb
+++ b/rubocop/cop/migration/schema_addition_methods_no_post.rb
@@ -5,7 +5,7 @@
 module RuboCop
   module Cop
     module Migration
-      # Cop that checks that no background batched migration helpers are called by regular migrations.
+      # Cop that checks that no schema migration methods are called by post-deployment migrations.
       class SchemaAdditionMethodsNoPost < RuboCop::Cop::Base
         include MigrationHelpers
 
@@ -39,7 +39,7 @@ def on_send(node)
         private
 
         def rolling_back_migration?(node)
-          rolling_back_migration(node.parent)
+          node.each_ancestor(:def).any? { |a| rolling_back_migration(a) }
         end
       end
     end
diff --git a/spec/rubocop/cop/migration/schema_addition_methods_no_post_spec.rb b/spec/rubocop/cop/migration/schema_addition_methods_no_post_spec.rb
index 94c0638cf1bd8..92e714d7a0217 100644
--- a/spec/rubocop/cop/migration/schema_addition_methods_no_post_spec.rb
+++ b/spec/rubocop/cop/migration/schema_addition_methods_no_post_spec.rb
@@ -42,5 +42,13 @@ def down
         end
       CODE
     end
+
+    it "allows forbidden method to be called within nested statement" do
+      expect_no_offenses(<<~CODE)
+        def down
+          add_column(:table, :column, :boolean) unless column_exists?(:table, :column)
+        end
+      CODE
+    end
   end
 end
-- 
GitLab