Use bulk_insert instead of AR create

This commit is contained in:
Shinya Maeda 2018-01-05 00:53:50 +09:00
parent c80598816c
commit b8a275d3e4

View file

@ -94,24 +94,57 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
def up
MigrateKubernetesServiceToNewClustersArchitectures::Service
.unmanaged_kubernetes_service.find_each(batch_size: 1) do |kubernetes_service|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create(
enabled: kubernetes_service.active,
user_id: nil, # KubernetesService doesn't have
name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME,
provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user],
platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes],
projects: [kubernetes_service.project.becomes(MigrateKubernetesServiceToNewClustersArchitectures::Project)],
environment_scope: find_dedicated_environement_scope(kubernetes_service.project),
platform_kubernetes_attributes: {
.unmanaged_kubernetes_service.each_batch(of: 100) do |kubernetes_services|
rows_for_clusters = kubernetes_services.map do |kubernetes_service|
{
enabled: kubernetes_service.active,
user_id: nil, # KubernetesService doesn't have
name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME,
provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user],
platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes],
environment_scope: find_dedicated_environement_scope(kubernetes_service.project),
created_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.created_at),
updated_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.updated_at)
}
end
inserted_cluster_ids = Gitlab::Database.bulk_insert('clusters', rows_for_clusters, return_ids: true)
rows_for_cluster_platforms_kubernetes = kubernetes_services.each_with_index.map do |kubernetes_service, i|
# Create PlatformsKubernetes instance for generating an encrypted token
platforms_kubernetes =
MigrateKubernetesServiceToNewClustersArchitectures::PlatformsKubernetes
.new(token: kubernetes_service.token)
{
cluster_id: inserted_cluster_ids[i],
api_url: kubernetes_service.api_url,
ca_cert: kubernetes_service.ca_pem,
namespace: kubernetes_service.namespace,
username: nil, # KubernetesService doesn't have
encrypted_password: nil, # KubernetesService doesn't have
encrypted_password_iv: nil, # KubernetesService doesn't have
token: kubernetes_service.token # encrypted_token and encrypted_token_iv
} )
encrypted_token: platforms_kubernetes.encrypted_token, # encrypted_token and encrypted_token_iv
encrypted_token_iv: platforms_kubernetes.encrypted_token_iv, # encrypted_token and encrypted_token_iv
created_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.created_at),
updated_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.updated_at)
}
end
Gitlab::Database.bulk_insert('cluster_platforms_kubernetes', rows_for_cluster_platforms_kubernetes)
rows_for_cluster_projects = kubernetes_services.each_with_index.map do |kubernetes_service, i|
{
cluster_id: inserted_cluster_ids[i],
project_id: kubernetes_service.project_id,
created_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.created_at),
updated_at: Gitlab::Database.sanitize_timestamp(kubernetes_service.updated_at)
}
end
Gitlab::Database.bulk_insert('cluster_projects', rows_for_cluster_projects)
end
MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service_without_template.update_all(active: false)