summaryrefslogtreecommitdiff
path: root/db/post_migrate/20230405132104_remove_saml_provider_and_identities_non_root_group.rb
blob: 55a017464c20478477e36a222f2f964826c87619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

class RemoveSamlProviderAndIdentitiesNonRootGroup < Gitlab::Database::Migration[2.1]
  BATCH_SIZE = 500

  disable_ddl_transaction!
  restrict_gitlab_migration gitlab_schema: :gitlab_main

  def up
    each_batch_range('saml_providers', scope: ->(table) { table.all }, of: BATCH_SIZE) do |min, max|
      execute <<~SQL
        DELETE FROM identities
        WHERE identities.saml_provider_id
        IN
        (
          SELECT saml_providers.id FROM saml_providers
          INNER JOIN namespaces ON namespaces.id=saml_providers.group_id
          AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
          AND saml_providers.id BETWEEN #{min} AND #{max}
        );

        DELETE FROM saml_providers
        USING namespaces
        WHERE namespaces.id=saml_providers.group_id
        AND namespaces.type='Group' AND namespaces.parent_id IS NOT NULL
        AND saml_providers.id BETWEEN #{min} AND #{max};
      SQL
    end
  end

  def down
    # noop
  end
end