Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
183dbdc8
Commit
183dbdc8
authored
Jan 06, 2018
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert bulk_insert and bring back AR insert(one by one)
parent
1c404c91
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
64 deletions
+54
-64
db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb
...grate_kubernetes_service_to_new_clusters_architectures.rb
+26
-52
spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb
..._kubernetes_service_to_new_clusters_architectures_spec.rb
+28
-12
No files found.
db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb
View file @
183dbdc8
class
MigrateKubernetesServiceToNewClustersArchitectures
<
ActiveRecord
::
Migration
class
MigrateKubernetesServiceToNewClustersArchitectures
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
DOWNTIME
=
false
DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME
=
'KubernetesService'
.
freeze
DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME
=
'KubernetesService'
.
freeze
disable_ddl_transaction!
class
Project
<
ActiveRecord
::
Base
class
Project
<
ActiveRecord
::
Base
self
.
table_name
=
'projects'
self
.
table_name
=
'projects'
...
@@ -58,14 +62,16 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
...
@@ -58,14 +62,16 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
joins
(
'LEFT JOIN projects ON projects.id = services.project_id'
)
joins
(
'LEFT JOIN projects ON projects.id = services.project_id'
)
.
joins
(
'LEFT JOIN cluster_projects ON cluster_projects.project_id = projects.id'
)
.
joins
(
'LEFT JOIN cluster_projects ON cluster_projects.project_id = projects.id'
)
.
joins
(
'LEFT JOIN cluster_platforms_kubernetes ON cluster_platforms_kubernetes.cluster_id = cluster_projects.cluster_id'
)
.
joins
(
'LEFT JOIN cluster_platforms_kubernetes ON cluster_platforms_kubernetes.cluster_id = cluster_projects.cluster_id'
)
.
where
(
category:
'deployment'
)
.
where
(
category:
'deployment'
,
type:
'KubernetesService'
,
template:
false
)
.
where
(
type:
'KubernetesService'
)
.
where
(
template:
false
)
.
where
(
"services.properties LIKE '%api_url%'"
)
.
where
(
"services.properties LIKE '%api_url%'"
)
.
where
(
"(services.properties NOT LIKE CONCAT('%', cluster_platforms_kubernetes.api_url, '%')) OR cluster_platforms_kubernetes.api_url IS NULL"
)
.
where
(
"(services.properties NOT LIKE CONCAT('%', cluster_platforms_kubernetes.api_url, '%')) OR cluster_platforms_kubernetes.api_url IS NULL"
)
.
group
(
:id
)
.
group
(
:id
)
.
order
(
id: :asc
)
.
order
(
id: :asc
)
end
end
scope
:kubernetes_service_without_template
,
->
do
where
(
category:
'deployment'
,
type:
'KubernetesService'
,
template:
false
)
end
end
end
def
find_dedicated_environement_scope
(
project
)
def
find_dedicated_environement_scope
(
project
)
...
@@ -86,65 +92,33 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
...
@@ -86,65 +92,33 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
end
end
def
up
def
up
MigrateKubernetesServiceToNewClustersArchitectures
::
Service
ActiveRecord
::
Base
.
transaction
do
.
unmanaged_kubernetes_service
.
each_batch
(
of:
100
)
do
|
kubernetes_services
|
MigrateKubernetesServiceToNewClustersArchitectures
::
Service
.
unmanaged_kubernetes_service
.
find_each
(
batch_size:
1
)
do
|
kubernetes_service
|
rows_for_clusters
=
kubernetes_services
.
map
do
|
kubernetes_service
|
MigrateKubernetesServiceToNewClustersArchitectures
::
Cluster
.
create
(
{
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
),
created_at:
Gitlab
::
Database
.
sanitize_timestamp
(
kubernetes_service
.
created_at
),
platform_kubernetes_attributes:
{
updated_at:
Gitlab
::
Database
.
sanitize_timestamp
(
kubernetes_service
.
updated_at
)
api_url:
kubernetes_service
.
api_url
,
}
ca_cert:
kubernetes_service
.
ca_pem
,
end
namespace:
kubernetes_service
.
namespace
,
username:
nil
,
# KubernetesService doesn't have
inserted_cluster_ids
=
Gitlab
::
Database
.
bulk_insert
(
'clusters'
,
rows_for_clusters
,
return_ids:
true
)
encrypted_password:
nil
,
# KubernetesService doesn't have
encrypted_password_iv:
nil
,
# KubernetesService doesn't have
rows_for_cluster_platforms_kubernetes
=
kubernetes_services
.
each_with_index
.
map
do
|
kubernetes_service
,
i
|
token:
kubernetes_service
.
token
# encrypted_token and encrypted_token_iv
}
)
# 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
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
end
Gitlab
::
Database
.
bulk_insert
(
'cluster_projects'
,
rows_for_cluster_projects
)
end
end
MigrateKubernetesServiceToNewClustersArchitectures
::
Service
MigrateKubernetesServiceToNewClustersArchitectures
::
Service
.
where
(
category:
'deployment'
,
type:
'KubernetesService'
,
template:
false
)
.
kubernetes_service_without_template
.
each_batch
(
of:
100
)
do
|
kubernetes_service
|
.
each_batch
(
of:
100
)
do
|
batch
|
kubernetes_service
.
update_all
(
active:
false
)
batch
.
update_all
(
active:
false
)
end
end
end
end
def
down
def
down
...
...
spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb
View file @
183dbdc8
...
@@ -38,18 +38,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
...
@@ -38,18 +38,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
it_behaves_like
'KubernetesService migration'
it_behaves_like
'KubernetesService migration'
end
end
context
'when KubernetesService is not active'
do
let
(
:active
)
{
false
}
# Platforms::Kubernetes validates `token` reagdless of the activeness,
# whereas KubernetesService validates `token` if only it's activated
# However, in this migration file, there are no validations because of the migration specific model class
# therefore, Validation Error will not happen in this case and just migrate data
let
(
:token
)
{
''
}
it_behaves_like
'KubernetesService migration'
end
end
end
context
'when unique KubernetesService spawned from Service Template'
do
context
'when unique KubernetesService spawned from Service Template'
do
...
@@ -186,6 +174,34 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
...
@@ -186,6 +174,34 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
end
end
end
end
# Platforms::Kubernetes validates `token` reagdless of the activeness,
# whereas KubernetesService validates `token` if only it's activated
# However, in this migration file, there are no validations because of the re-defined model class
# therefore, we should safely add this raw to Platform::Kubernetes
context
'when KubernetesService has empty token'
do
let
(
:project
)
{
create
(
:project
)
}
before
do
ActiveRecord
::
Base
.
connection
.
execute
<<~
SQL
INSERT INTO services (project_id, active, category, type, properties)
VALUES (
#{
project
.
id
}
, false, 'deployment', 'KubernetesService', '{"namespace":"prod","api_url":"http://111.111.111.111","ca_pem":"a","token":""}');
SQL
end
it
'does not migrate the KubernetesService and disables the kubernetes_service'
do
expect
{
migrate!
}.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
1
)
project
.
clusters
.
last
.
tap
do
|
cluster
|
expect
(
cluster
.
environment_scope
).
to
eq
(
'*'
)
expect
(
cluster
.
platform_kubernetes
.
namespace
).
to
eq
(
'prod'
)
expect
(
cluster
.
platform_kubernetes
.
api_url
).
to
eq
(
'http://111.111.111.111'
)
expect
(
cluster
.
platform_kubernetes
.
ca_pem
).
to
eq
(
'a'
)
expect
(
cluster
.
platform_kubernetes
.
token
).
to
be_empty
expect
(
project
.
kubernetes_service
).
not_to
be_active
end
end
end
context
'when KubernetesService does not exist'
do
context
'when KubernetesService does not exist'
do
let!
(
:project
)
{
create
(
:project
)
}
let!
(
:project
)
{
create
(
:project
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment