summaryrefslogtreecommitdiff
path: root/db/migrate/20161117114805_remove_undeleted_groups.rb
blob: 29040583aa25367eb1788ee270777570ad62d252 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class RemoveUndeletedGroups < ActiveRecord::Migration
  DOWNTIME = false

  def up
    is_ee = defined?(Gitlab::License)

    if is_ee
      execute <<-EOF.strip_heredoc
      DELETE FROM path_locks
      WHERE project_id IN (
        SELECT project_id
        FROM projects
        WHERE namespace_id IN (#{namespaces_pending_removal})
      );
      EOF

      execute <<-EOF.strip_heredoc
      DELETE FROM remote_mirrors
      WHERE project_id IN (
        SELECT project_id
        FROM projects
        WHERE namespace_id IN (#{namespaces_pending_removal})
      );
      EOF
    end

    execute <<-EOF.strip_heredoc
    DELETE FROM lists
    WHERE label_id IN (
      SELECT id
      FROM labels
      WHERE group_id IN (#{namespaces_pending_removal})
    );
    EOF

    execute <<-EOF.strip_heredoc
    DELETE FROM lists
    WHERE board_id IN (
      SELECT id
      FROM boards
      WHERE project_id IN (
        SELECT project_id
        FROM projects
        WHERE namespace_id IN (#{namespaces_pending_removal})
      )
    );
    EOF

    execute <<-EOF.strip_heredoc
    DELETE FROM labels
    WHERE group_id IN (#{namespaces_pending_removal});
    EOF

    execute <<-EOF.strip_heredoc
    DELETE FROM boards
    WHERE project_id IN (
      SELECT project_id
      FROM projects
      WHERE namespace_id IN (#{namespaces_pending_removal})
    )
    EOF

    execute <<-EOF.strip_heredoc
    DELETE FROM projects
    WHERE namespace_id IN (#{namespaces_pending_removal});
    EOF

    if is_ee
      # EE adds these columns but we have to make sure this data is cleaned up
      # here before we run the DELETE below. An alternative would be patching
      # this migration in EE but this will only result in a mess and confusing
      # migrations.
      execute <<-EOF.strip_heredoc
      DELETE FROM protected_branch_push_access_levels
      WHERE group_id IN (#{namespaces_pending_removal});
      EOF

      execute <<-EOF.strip_heredoc
      DELETE FROM protected_branch_merge_access_levels
      WHERE group_id IN (#{namespaces_pending_removal});
      EOF
    end

    # This removes namespaces that were supposed to be deleted but still reside
    # in the database.
    execute "DELETE FROM namespaces WHERE deleted_at IS NOT NULL;"
  end

  def down
    # This is an irreversible migration;
    # If someone is trying to rollback for other reasons, we should not throw an Exception.
    # raise ActiveRecord::IrreversibleMigration
  end

  def namespaces_pending_removal
    "SELECT id FROM (
      SELECT id
      FROM namespaces
      WHERE deleted_at IS NOT NULL
    ) namespace_ids"
  end
end