Commit 0c417ef0 authored by Shinya Maeda's avatar Shinya Maeda

Improve migration file. Add migration spec. Reorder columns of the table.

parent 6571efb6
...@@ -3,16 +3,17 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration ...@@ -3,16 +3,17 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
def change def change
create_table :clusters do |t| create_table :clusters do |t|
t.references :user, foreign_key: { on_delete: :nullify } t.references :user, null: false, index: true, foreign_key: { on_delete: :nullify }
t.boolean :enabled, default: true
t.string :name, null: false # If manual, read-write. If gcp, read-only.
t.integer :provider_type, null: false t.integer :provider_type, null: false
t.integer :platform_type, null: false t.integer :platform_type, null: false
t.datetime_with_timezone :created_at, null: false t.datetime_with_timezone :created_at, null: false
t.datetime_with_timezone :updated_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 end
create_table :cluster_projects do |t| create_table :cluster_projects do |t|
...@@ -26,7 +27,10 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration ...@@ -26,7 +27,10 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
create_table :cluster_platforms_kubernetes do |t| create_table :cluster_platforms_kubernetes do |t|
t.references :cluster, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade } 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.text :ca_cert
t.string :namespace t.string :namespace
...@@ -37,20 +41,21 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration ...@@ -37,20 +41,21 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
t.text :encrypted_token t.text :encrypted_token
t.string :encrypted_token_iv t.string :encrypted_token_iv
t.datetime_with_timezone :created_at, null: false
t.datetime_with_timezone :updated_at, null: false
end end
create_table :cluster_providers_gcp do |t| create_table :cluster_providers_gcp do |t|
t.references :cluster, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade } t.references :cluster, null: false, index: { unique: true }, foreign_key: { on_delete: :cascade }
t.integer :status 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.text :status_reason
t.string :gcp_project_id, null: false t.string :gcp_project_id, null: false
t.string :zone, null: false t.string :zone, null: false
t.integer :num_nodes, null: false
t.string :machine_type t.string :machine_type
t.string :operation_id t.string :operation_id
...@@ -58,9 +63,6 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration ...@@ -58,9 +63,6 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
t.text :encrypted_access_token t.text :encrypted_access_token
t.string :encrypted_access_token_iv 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 end
end end
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
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
...@@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171017145932) do ...@@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171017145932) do
create_table "cluster_platforms_kubernetes", force: :cascade do |t| create_table "cluster_platforms_kubernetes", force: :cascade do |t|
t.integer "cluster_id", null: false 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.text "ca_cert"
t.string "namespace" t.string "namespace"
t.string "username" t.string "username"
...@@ -472,8 +474,6 @@ ActiveRecord::Schema.define(version: 20171017145932) do ...@@ -472,8 +474,6 @@ ActiveRecord::Schema.define(version: 20171017145932) do
t.string "encrypted_password_iv" t.string "encrypted_password_iv"
t.text "encrypted_token" t.text "encrypted_token"
t.string "encrypted_token_iv" t.string "encrypted_token_iv"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
add_index "cluster_platforms_kubernetes", ["cluster_id"], name: "index_cluster_platforms_kubernetes_on_cluster_id", unique: true, using: :btree 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 ...@@ -491,31 +491,34 @@ ActiveRecord::Schema.define(version: 20171017145932) do
create_table "cluster_providers_gcp", force: :cascade do |t| create_table "cluster_providers_gcp", force: :cascade do |t|
t.integer "cluster_id", null: false t.integer "cluster_id", null: false
t.integer "status" 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.text "status_reason"
t.string "gcp_project_id", null: false t.string "gcp_project_id", null: false
t.string "zone", null: false t.string "zone", null: false
t.integer "num_nodes", null: false
t.string "machine_type" t.string "machine_type"
t.string "operation_id" t.string "operation_id"
t.string "endpoint" t.string "endpoint"
t.text "encrypted_access_token" t.text "encrypted_access_token"
t.string "encrypted_access_token_iv" t.string "encrypted_access_token_iv"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
add_index "cluster_providers_gcp", ["cluster_id"], name: "index_cluster_providers_gcp_on_cluster_id", unique: true, using: :btree 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| create_table "clusters", force: :cascade do |t|
t.integer "user_id" t.integer "user_id", null: false
t.boolean "enabled", default: true
t.string "name", null: false
t.integer "provider_type", null: false t.integer "provider_type", null: false
t.integer "platform_type", null: false t.integer "platform_type", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.boolean "enabled", default: true
t.string "name", null: false
end 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| create_table "container_repositories", force: :cascade do |t|
t.integer "project_id", null: false t.integer "project_id", null: false
t.string "name", null: false t.string "name", null: false
......
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb')
describe MigrateGcpClustersToNewClustersArchitectures, :migration do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:service) { create(:kubernetes_service, project: project) }
let(:project_id) { project.id }
let(:user_id) { user.id }
let(:service_id) { service.id }
let(:status) { 3 }
let(:gcp_cluster_size) { 1 }
let(:created_at) { '2017-10-17 20:24:02.219679' }
let(:updated_at) { '2017-10-17 20:28:44.738998' }
let(:enabled) { true }
let(:status_reason) { 'general error' }
let(:project_namespace) { 'sample-app' }
let(:endpoint) { '111.111.111.111' }
let(:ca_cert) { 'ca_cert' }
let(:encrypted_kubernetes_token) { 'encrypted_kubernetes_token' }
let(:encrypted_kubernetes_token_iv) { 'encrypted_kubernetes_token_iv' }
let(:username) { 'username' }
let(:encrypted_password) { 'encrypted_password' }
let(:encrypted_password_iv) { 'encrypted_password_iv' }
let(:gcp_project_id) { 'gcp_project_id' }
let(:gcp_cluster_zone) { 'gcp_cluster_zone' }
let(:gcp_cluster_name) { 'gcp_cluster_name' }
let(:gcp_machine_type) { 'gcp_machine_type' }
let(:gcp_operation_id) { 'gcp_operation_id' }
let(:encrypted_gcp_token) { 'encrypted_gcp_token' }
let(:encrypted_gcp_token_iv) { 'encrypted_gcp_token_iv' }
let(:cluster) { Clusters::Cluster.last }
let(:cluster_id) { cluster.id }
before do
ActiveRecord::Base.connection.execute <<-SQL
INSERT INTO gcp_clusters (project_id, user_id, service_id, status, gcp_cluster_size, created_at, updated_at, enabled, status_reason, project_namespace, endpoint, ca_cert, encrypted_kubernetes_token, encrypted_kubernetes_token_iv, username, encrypted_password, encrypted_password_iv, gcp_project_id, gcp_cluster_zone, gcp_cluster_name, gcp_machine_type, gcp_operation_id, encrypted_gcp_token, encrypted_gcp_token_iv)
VALUES ('#{project_id}', '#{user_id}', '#{service_id}', '#{status}', '#{gcp_cluster_size}', '#{created_at}', '#{updated_at}', '#{enabled}', '#{status_reason}', '#{project_namespace}', '#{endpoint}', '#{ca_cert}', '#{encrypted_kubernetes_token}', '#{encrypted_kubernetes_token_iv}', '#{username}', '#{encrypted_password}', '#{encrypted_password_iv}', '#{gcp_project_id}', '#{gcp_cluster_zone}', '#{gcp_cluster_name}', '#{gcp_machine_type}', '#{gcp_operation_id}', '#{encrypted_gcp_token}', '#{encrypted_gcp_token_iv}');
SQL
end
it 'correctly migrate to new clusters architectures' do
migrate!
expect(Clusters::Cluster.count).to eq(1)
expect(Clusters::Project.count).to eq(1)
expect(Clusters::Providers::Gcp.count).to eq(1)
expect(Clusters::Platforms::Kubernetes.count).to eq(1)
expect(cluster.user).to eq(user)
expect(cluster.enabled).to eq(enabled)
expect(cluster.name).to eq(gcp_cluster_name)
expect(cluster.provider_type).to eq('gcp')
expect(cluster.platform_type).to eq('kubernetes')
expect(cluster.created_at).to eq(created_at)
expect(cluster.updated_at).to eq(updated_at)
expect(cluster.project).to eq(project)
expect(cluster.provider_gcp.cluster).to eq(cluster)
expect(cluster.provider_gcp.status).to eq(status)
expect(cluster.provider_gcp.status_reason).to eq(status_reason)
expect(cluster.provider_gcp.gcp_project_id).to eq(gcp_project_id)
expect(cluster.provider_gcp.zone).to eq(gcp_cluster_zone)
expect(cluster.provider_gcp.num_nodes).to eq(gcp_cluster_size)
expect(cluster.provider_gcp.machine_type).to eq(gcp_machine_type)
expect(cluster.provider_gcp.operation_id).to eq(gcp_operation_id)
expect(cluster.provider_gcp.endpoint).to eq(endpoint)
expect(cluster.provider_gcp.encrypted_access_token).to eq(encrypted_gcp_token)
expect(cluster.provider_gcp.encrypted_access_token_iv).to eq(encrypted_gcp_token_iv)
expect(cluster.provider_gcp.created_at).to eq(created_at)
expect(cluster.provider_gcp.updated_at).to eq(updated_at)
expect(cluster.platform_kubernetes.cluster).to eq(cluster)
expect(cluster.platform_kubernetes.api_url).to eq('https://' + endpoint)
expect(cluster.platform_kubernetes.ca_cert).to eq(ca_cert)
expect(cluster.platform_kubernetes.namespace).to eq(project_namespace)
expect(cluster.platform_kubernetes.username).to eq(username)
expect(cluster.platform_kubernetes.encrypted_password).to eq(encrypted_password)
expect(cluster.platform_kubernetes.encrypted_password_iv).to eq(encrypted_password_iv)
expect(cluster.platform_kubernetes.encrypted_token).to eq(encrypted_kubernetes_token)
expect(cluster.platform_kubernetes.encrypted_token_iv).to eq(encrypted_kubernetes_token_iv)
expect(cluster.platform_kubernetes.created_at).to eq(created_at)
expect(cluster.platform_kubernetes.updated_at).to eq(updated_at)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment