Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 759ab939f7de416f2178627797fc7e18d8354dc1 (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
# 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 MigrateLegacyManagedClustersToUnmanaged < ActiveRecord::Migration[5.1]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class Cluster < ActiveRecord::Base
    include EachBatch

    self.table_name = 'clusters'

    has_many :kubernetes_namespaces, class_name: 'MigrateLegacyManagedClustersToUnmanaged::KubernetesNamespace'

    scope :managed, -> { where(managed: true) }

    enum cluster_type: {
      instance_type: 1,
      group_type: 2,
      project_type: 3
    }
  end

  class KubernetesNamespace < ActiveRecord::Base
    self.table_name = 'clusters_kubernetes_namespaces'

    belongs_to :cluster, class_name: 'MigrateLegacyManagedClustersToUnmanaged::Cluster'
  end

  def up
    Cluster.managed
      .project_type
      .left_joins(:kubernetes_namespaces)
      .where(clusters_kubernetes_namespaces: { cluster_id: nil })
      .where('clusters.created_at < ?', 5.minutes.ago)
      .each_batch do |batch|
        batch.update_all(managed: false)
      end
  end

  def down
  end
end