diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-11-01 21:28:47 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-11-01 21:28:47 +0900 |
commit | 0c417ef0435ca49dba451a7270235f775d1d9a75 (patch) | |
tree | b0f7cc6d57bec3ab0844df97cde903bcada19563 /db | |
parent | 6571efb6c3afd568c019e7bb46aba84328a4e821 (diff) | |
download | gitlab-ce-0c417ef0435ca49dba451a7270235f775d1d9a75.tar.gz |
Improve migration file. Add migration spec. Reorder columns of the table.
Diffstat (limited to 'db')
4 files changed, 112 insertions, 105 deletions
diff --git a/db/migrate/20171013094327_create_new_clusters_architectures.rb b/db/migrate/20171013094327_create_new_clusters_architectures.rb index 2eae72595c6..a212288f859 100644 --- a/db/migrate/20171013094327_create_new_clusters_architectures.rb +++ b/db/migrate/20171013094327_create_new_clusters_architectures.rb @@ -3,16 +3,17 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration def change create_table :clusters do |t| - t.references :user, foreign_key: { on_delete: :nullify } - - t.boolean :enabled, default: true - t.string :name, null: false # If manual, read-write. If gcp, read-only. + t.references :user, null: false, index: true, foreign_key: { on_delete: :nullify } t.integer :provider_type, null: false t.integer :platform_type, null: false t.datetime_with_timezone :created_at, null: false t.datetime_with_timezone :updated_at, null: false + + t.boolean :enabled, index: true, default: true + + t.string :name, null: false # If manual, read-write. If gcp, read-only. end create_table :cluster_projects do |t| @@ -26,7 +27,10 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration create_table :cluster_platforms_kubernetes do |t| t.references :cluster, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade } - t.string :api_url + t.datetime_with_timezone :created_at, null: false + t.datetime_with_timezone :updated_at, null: false + + t.text :api_url t.text :ca_cert t.string :namespace @@ -37,20 +41,21 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration t.text :encrypted_token t.string :encrypted_token_iv - - t.datetime_with_timezone :created_at, null: false - t.datetime_with_timezone :updated_at, null: false end create_table :cluster_providers_gcp do |t| t.references :cluster, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade } t.integer :status + t.integer :num_nodes, null: false + + t.datetime_with_timezone :created_at, null: false + t.datetime_with_timezone :updated_at, null: false + t.text :status_reason t.string :gcp_project_id, null: false t.string :zone, null: false - t.integer :num_nodes, null: false t.string :machine_type t.string :operation_id @@ -58,9 +63,6 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration t.text :encrypted_access_token t.string :encrypted_access_token_iv - - t.datetime_with_timezone :created_at, null: false - t.datetime_with_timezone :updated_at, null: false end end end diff --git a/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb b/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb new file mode 100644 index 00000000000..6ff98899bcb --- /dev/null +++ b/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb @@ -0,0 +1,86 @@ +class MigrateGcpClustersToNewClustersArchitectures < ActiveRecord::Migration + DOWNTIME = false + + def up + gcp_clusters = ActiveRecord::Base.connection.select_all('SELECT * from gcp_clusters;') + + rows_for_clusters = Array.new + rows_for_cluster_projects = Array.new + rows_for_cluster_providers_gcp = Array.new + rows_for_cluster_platforms_kubernetes = Array.new + + gcp_clusters.each do |gcp_cluster| + rows_for_clusters << params_for_clusters(gcp_cluster) + rows_for_cluster_projects << params_for_cluster_projects(gcp_cluster) + rows_for_cluster_providers_gcp << params_for_cluster_providers_gcp(gcp_cluster) + rows_for_cluster_platforms_kubernetes << params_for_cluster_platforms_kubernetes(gcp_cluster) + end + + Gitlab::Database.bulk_insert('clusters', rows_for_clusters) + Gitlab::Database.bulk_insert('cluster_projects', rows_for_cluster_projects) + Gitlab::Database.bulk_insert('cluster_providers_gcp', rows_for_cluster_providers_gcp) + Gitlab::Database.bulk_insert('cluster_platforms_kubernetes', rows_for_cluster_platforms_kubernetes) + end + + def down + execute('DELETE FROM clusters') + end + + private + + def params_for_clusters(gcp_cluster) + { + id: gcp_cluster['id'], + user_id: gcp_cluster['user_id'], + enabled: gcp_cluster['enabled'], + name: gcp_cluster['gcp_cluster_name'], + provider_type: Clusters::Cluster.provider_types[:gcp], + platform_type: Clusters::Cluster.platform_types[:kubernetes], + created_at: gcp_cluster['created_at'], + updated_at: gcp_cluster['updated_at'] + } + end + + def params_for_cluster_projects(gcp_cluster) + { + cluster_id: gcp_cluster['id'], + project_id: gcp_cluster['project_id'], + created_at: gcp_cluster['created_at'], + updated_at: gcp_cluster['updated_at'] + } + end + + def params_for_cluster_providers_gcp(gcp_cluster) + { + cluster_id: gcp_cluster['id'], + status: gcp_cluster['status'], + status_reason: gcp_cluster['status_reason'], + gcp_project_id: gcp_cluster['gcp_project_id'], + zone: gcp_cluster['gcp_cluster_zone'], + num_nodes: gcp_cluster['gcp_cluster_size'], + machine_type: gcp_cluster['gcp_machine_type'], + operation_id: gcp_cluster['gcp_operation_id'], + endpoint: gcp_cluster['endpoint'], + encrypted_access_token: gcp_cluster['encrypted_gcp_token'], + encrypted_access_token_iv: gcp_cluster['encrypted_gcp_token_iv'], + created_at: gcp_cluster['created_at'], + updated_at: gcp_cluster['updated_at'] + } + end + + def params_for_cluster_platforms_kubernetes(gcp_cluster) + { + cluster_id: gcp_cluster['id'], + api_url: 'https://' + gcp_cluster['endpoint'], + ca_cert: gcp_cluster['ca_cert'], + namespace: gcp_cluster['project_namespace'], + username: gcp_cluster['username'], + encrypted_password: gcp_cluster['encrypted_password'], + encrypted_password_iv: gcp_cluster['encrypted_password_iv'], + encrypted_token: gcp_cluster['encrypted_kubernetes_token'], + encrypted_token_iv: gcp_cluster['encrypted_kubernetes_token_iv'], + created_at: gcp_cluster['created_at'], + updated_at: gcp_cluster['updated_at'] + } + end +end diff --git a/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb.rb b/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb.rb deleted file mode 100644 index 5510b036d24..00000000000 --- a/db/migrate/20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb.rb +++ /dev/null @@ -1,84 +0,0 @@ -class MigrateGcpClustersToNewClustersArchitectures < ActiveRecord::Migration - DOWNTIME = false - - def up - # TODO: Chnage to something reaistic - ActiveRecord::Base.connection.select_rows('SELECT * from gcp_clusters;').each do |old_cluster| - id = old_cluster[0] - project_id = old_cluster[1] - user_id = old_cluster[2] - service_id = old_cluster[3] - status = old_cluster[4] - gcp_cluster_size = old_cluster[5] - created_at = old_cluster[6] - updated_at = old_cluster[7] - enabled = old_cluster[8] - status_reason = old_cluster[9] - project_namespace = old_cluster[10] - endpoint = old_cluster[11] - ca_cert = old_cluster[12] - encrypted_kubernetes_token = old_cluster[13] - encrypted_kubernetes_token_iv = old_cluster[14] - username = old_cluster[15] - encrypted_password = old_cluster[16] - encrypted_password_iv = old_cluster[17] - gcp_project_id = old_cluster[18] - gcp_cluster_zone = old_cluster[19] - gcp_cluster_name = old_cluster[20] - gcp_machine_type = old_cluster[21] - gcp_operation_id = old_cluster[22] - encrypted_gcp_token = old_cluster[23] - encrypted_gcp_token_iv = old_cluster[24] - - cluster = Clusters::Cluster.create!( - user_id: user_id, - enabled: enabled, - name: gcp_cluster_name, - provider_type: :gcp, - platform_type: :kubernetes, - created_at: created_at, - updated_at: updated_at) - - Clusters::Project.create!( - cluster: cluster, - project_id: project_id, - created_at: created_at, - updated_at: updated_at) - - Clusters::Platforms::Kubernetes.create!( - cluster: cluster, - api_url: 'https://' + endpoint, - ca_cert: ca_cert, - namespace: project_namespace, - username: username, - encrypted_password: encrypted_password, - encrypted_password_iv: encrypted_password_iv, - encrypted_token: encrypted_kubernetes_token, - encrypted_token_iv: encrypted_kubernetes_token_iv, - created_at: created_at, - updated_at: updated_at) - - Clusters::Providers::Gcp.create!( - cluster: cluster, - status: status, - status_reason: status_reason, - gcp_project_id: gcp_project_id, - zone: gcp_cluster_zone, - num_nodes: gcp_cluster_size, - machine_type: gcp_machine_type, - operation_id: gcp_operation_id, - endpoint: endpoint, - encrypted_access_token: encrypted_gcp_token, - encrypted_access_token_iv: encrypted_gcp_token_iv, - created_at: created_at, - updated_at: updated_at) - end - end - - def down - Clusters::Cluster.delete_all - Clusters::Project.delete_all - Clusters::Providers::Gcp.delete_all - Clusters::Platforms::Kubernetes.delete_all - end -end diff --git a/db/schema.rb b/db/schema.rb index 611435af082..adf8b9594fb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171017145932) do create_table "cluster_platforms_kubernetes", force: :cascade do |t| t.integer "cluster_id", null: false - t.string "api_url" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "api_url" t.text "ca_cert" t.string "namespace" t.string "username" @@ -472,8 +474,6 @@ ActiveRecord::Schema.define(version: 20171017145932) do t.string "encrypted_password_iv" t.text "encrypted_token" t.string "encrypted_token_iv" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false end add_index "cluster_platforms_kubernetes", ["cluster_id"], name: "index_cluster_platforms_kubernetes_on_cluster_id", unique: true, using: :btree @@ -491,31 +491,34 @@ ActiveRecord::Schema.define(version: 20171017145932) do create_table "cluster_providers_gcp", force: :cascade do |t| t.integer "cluster_id", null: false t.integer "status" + t.integer "num_nodes", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.text "status_reason" t.string "gcp_project_id", null: false t.string "zone", null: false - t.integer "num_nodes", null: false t.string "machine_type" t.string "operation_id" t.string "endpoint" t.text "encrypted_access_token" t.string "encrypted_access_token_iv" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false end add_index "cluster_providers_gcp", ["cluster_id"], name: "index_cluster_providers_gcp_on_cluster_id", unique: true, using: :btree create_table "clusters", force: :cascade do |t| - t.integer "user_id" - t.boolean "enabled", default: true - t.string "name", null: false + t.integer "user_id", null: false t.integer "provider_type", null: false t.integer "platform_type", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "enabled", default: true + t.string "name", null: false end + add_index "clusters", ["enabled"], name: "index_clusters_on_enabled", using: :btree + add_index "clusters", ["user_id"], name: "index_clusters_on_user_id", using: :btree + create_table "container_repositories", force: :cascade do |t| t.integer "project_id", null: false t.string "name", null: false |