summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorThong Kuah <tkuah@gitlab.com>2019-06-21 01:26:19 +0000
committerThong Kuah <tkuah@gitlab.com>2019-06-21 01:26:19 +0000
commit148516ba36855095fa995c2d4e8077919cdb6db6 (patch)
tree1efc18b892d7cbd51bfa11e4e3350e831bb736b4 /db
parent4562ea32dac7b07c285050b404a02e2de1480752 (diff)
parent8a8ddec661eb7a4bca740285622bb3f8419732fd (diff)
downloadgitlab-ce-148516ba36855095fa995c2d4e8077919cdb6db6.tar.gz
Merge branch '62772-migrate-managed-clusters-to-unmanaged' into 'master'
Migrate managed clusters that aren't using managed features to unmanaged Closes #62772 See merge request gitlab-org/gitlab-ce!29251
Diffstat (limited to 'db')
-rw-r--r--db/post_migrate/20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/db/post_migrate/20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb b/db/post_migrate/20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb
new file mode 100644
index 00000000000..759ab939f7d
--- /dev/null
+++ b/db/post_migrate/20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb
@@ -0,0 +1,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