Commit b8a275d3 authored by Shinya Maeda's avatar Shinya Maeda

Use bulk_insert instead of AR create

parent c8059881
...@@ -94,24 +94,57 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati ...@@ -94,24 +94,57 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
def up def up
MigrateKubernetesServiceToNewClustersArchitectures::Service MigrateKubernetesServiceToNewClustersArchitectures::Service
.unmanaged_kubernetes_service.find_each(batch_size: 1) do |kubernetes_service| .unmanaged_kubernetes_service.each_batch(of: 100) do |kubernetes_services|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create(
rows_for_clusters = kubernetes_services.map do |kubernetes_service|
{
enabled: kubernetes_service.active, enabled: kubernetes_service.active,
user_id: nil, # KubernetesService doesn't have user_id: nil, # KubernetesService doesn't have
name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME, name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME,
provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user], provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user],
platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes], platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes],
projects: [kubernetes_service.project.becomes(MigrateKubernetesServiceToNewClustersArchitectures::Project)],
environment_scope: find_dedicated_environement_scope(kubernetes_service.project), environment_scope: find_dedicated_environement_scope(kubernetes_service.project),
platform_kubernetes_attributes: { 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, api_url: kubernetes_service.api_url,
ca_cert: kubernetes_service.ca_pem, ca_cert: kubernetes_service.ca_pem,
namespace: kubernetes_service.namespace, namespace: kubernetes_service.namespace,
username: nil, # KubernetesService doesn't have username: nil, # KubernetesService doesn't have
encrypted_password: nil, # KubernetesService doesn't have encrypted_password: nil, # KubernetesService doesn't have
encrypted_password_iv: 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 end
MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service_without_template.update_all(active: false) MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service_without_template.update_all(active: false)
......
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