From 0e15eec86d83cbdfefe17966bf5c02e4d419a34d Mon Sep 17 00:00:00 2001
From: Thong Kuah <tkuah@gitlab.com>
Date: Mon, 15 Oct 2018 09:42:29 +1300
Subject: Associate clusters model to groups

Even though we currently only should have one group for a cluster, we
allow the flexibility to associate to other groups in the future.

This also matches the runner <=> groups association.

- Adds Cluster#first_group, aliased to Cluster#group. For the
conceivable future, a cluster will have at most one group.

- Prevent mixing of group and project clusters. If project type
clusters, it should only have projects assigned.  Similarly with groups.

- Default cluster_type to :project_type. As it's very small table we can
set default and null: false in one release.
---
 db/migrate/20181014203236_create_cluster_groups.rb     | 17 +++++++++++++++++
 .../20181017001059_add_cluster_type_to_clusters.rb     | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 db/migrate/20181014203236_create_cluster_groups.rb
 create mode 100644 db/migrate/20181017001059_add_cluster_type_to_clusters.rb

(limited to 'db/migrate')

diff --git a/db/migrate/20181014203236_create_cluster_groups.rb b/db/migrate/20181014203236_create_cluster_groups.rb
new file mode 100644
index 00000000000..69382d5c851
--- /dev/null
+++ b/db/migrate/20181014203236_create_cluster_groups.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class CreateClusterGroups < ActiveRecord::Migration
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+
+  def change
+    create_table :cluster_groups do |t|
+      t.references :cluster, null: false, foreign_key: { on_delete: :cascade }
+      t.references :group, null: false, index: true
+
+      t.index [:cluster_id, :group_id], unique: true
+      t.foreign_key :namespaces, column: :group_id, on_delete: :cascade
+    end
+  end
+end
diff --git a/db/migrate/20181017001059_add_cluster_type_to_clusters.rb b/db/migrate/20181017001059_add_cluster_type_to_clusters.rb
new file mode 100644
index 00000000000..191e7eb4fb3
--- /dev/null
+++ b/db/migrate/20181017001059_add_cluster_type_to_clusters.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AddClusterTypeToClusters < ActiveRecord::Migration
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+  PROJECT_CLUSTER_TYPE = 3
+
+  disable_ddl_transaction!
+
+  def up
+    add_column_with_default(:clusters, :cluster_type, :smallint, default: PROJECT_CLUSTER_TYPE)
+  end
+
+  def down
+    remove_column(:clusters, :cluster_type)
+  end
+end
-- 
cgit v1.2.1