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
4f4269ad
Commit
4f4269ad
authored
Feb 24, 2021
by
Valery Sizov
Committed by
Michael Kozono
Feb 24, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unique foreign keys to Geo registries
We need to prevent duplicates for the registries
parent
c4af11fa
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
95 additions
and
4 deletions
+95
-4
ee/changelogs/unreleased/296928-geo-add-unique-constraint-to-registry-foreign-keys.yml
...28-geo-add-unique-constraint-to-registry-foreign-keys.yml
+5
-0
ee/db/geo/post_migrate/20210217020154_add_unique_index_on_container_repository_registry.rb
...0154_add_unique_index_on_container_repository_registry.rb
+41
-0
ee/db/geo/post_migrate/20210217020156_add_unique_index_on_terraform_state_version_registry.rb
...6_add_unique_index_on_terraform_state_version_registry.rb
+41
-0
ee/db/geo/schema.rb
ee/db/geo/schema.rb
+3
-3
ee/spec/factories/geo/container_repository_registry.rb
ee/spec/factories/geo/container_repository_registry.rb
+4
-0
ee/spec/models/geo/container_repository_registry_spec.rb
ee/spec/models/geo/container_repository_registry_spec.rb
+1
-1
No files found.
ee/changelogs/unreleased/296928-geo-add-unique-constraint-to-registry-foreign-keys.yml
0 → 100644
View file @
4f4269ad
---
title
:
'
Geo:
Add
unique
constraints
to
container
repository
and
terraform
state
registries'
merge_request
:
54841
author
:
type
:
fixed
ee/db/geo/post_migrate/20210217020154_add_unique_index_on_container_repository_registry.rb
0 → 100644
View file @
4f4269ad
# frozen_string_literal: true
class
AddUniqueIndexOnContainerRepositoryRegistry
<
ActiveRecord
::
Migration
[
6.0
]
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
OLD_INDEX_NAME
=
'index_container_repository_registry_on_repository_id'
NEW_INDEX_NAME
=
'index_container_repository_registry_repository_id_unique'
disable_ddl_transaction!
def
up
# Removing duplicated records that would prevent creating an unique index.
execute
<<-
SQL
DELETE FROM container_repository_registry
USING (
SELECT container_repository_id, MIN(id) as min_id
FROM container_repository_registry
GROUP BY container_repository_id
HAVING COUNT(id) > 1
) as container_repository_registry_duplicates
WHERE container_repository_registry_duplicates.container_repository_id = container_repository_registry.container_repository_id
AND container_repository_registry_duplicates.min_id <> container_repository_registry.id
SQL
add_concurrent_index
(
:container_repository_registry
,
:container_repository_id
,
unique:
true
,
name:
NEW_INDEX_NAME
)
remove_concurrent_index_by_name
:container_repository_registry
,
OLD_INDEX_NAME
end
def
down
add_concurrent_index
(
:container_repository_registry
,
:container_repository_id
,
name:
OLD_INDEX_NAME
)
remove_concurrent_index_by_name
:container_repository_registry
,
NEW_INDEX_NAME
end
end
ee/db/geo/post_migrate/20210217020156_add_unique_index_on_terraform_state_version_registry.rb
0 → 100644
View file @
4f4269ad
# frozen_string_literal: true
class
AddUniqueIndexOnTerraformStateVersionRegistry
<
ActiveRecord
::
Migration
[
6.0
]
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
OLD_INDEX_NAME
=
'index_tf_state_versions_registry_on_tf_state_versions_id'
NEW_INDEX_NAME
=
'index_tf_state_versions_registry_tf_state_versions_id_unique'
disable_ddl_transaction!
def
up
# Removing duplicated records that would prevent creating an unique index.
execute
<<-
SQL
DELETE FROM terraform_state_version_registry
USING (
SELECT terraform_state_version_id, MIN(id) as min_id
FROM terraform_state_version_registry
GROUP BY terraform_state_version_id
HAVING COUNT(id) > 1
) as terraform_state_version_registry_duplicates
WHERE terraform_state_version_registry_duplicates.terraform_state_version_id = terraform_state_version_registry.terraform_state_version_id
AND terraform_state_version_registry_duplicates.min_id <> terraform_state_version_registry.id
SQL
add_concurrent_index
(
:terraform_state_version_registry
,
:terraform_state_version_id
,
unique:
true
,
name:
NEW_INDEX_NAME
)
remove_concurrent_index_by_name
:terraform_state_version_registry
,
OLD_INDEX_NAME
end
def
down
add_concurrent_index
(
:terraform_state_version_registry
,
:terraform_state_version_id
,
name:
OLD_INDEX_NAME
)
remove_concurrent_index_by_name
:terraform_state_version_registry
,
NEW_INDEX_NAME
end
end
ee/db/geo/schema.rb
View file @
4f4269ad
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2021_02_
08_175408
)
do
ActiveRecord
::
Schema
.
define
(
version:
2021_02_
17_020156
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -23,7 +23,7 @@ ActiveRecord::Schema.define(version: 2021_02_08_175408) do
t
.
datetime
"retry_at"
t
.
datetime
"last_synced_at"
t
.
datetime
"created_at"
,
null:
false
t
.
index
[
"container_repository_id"
],
name:
"index_container_repository_registry_
on_repository_id"
t
.
index
[
"container_repository_id"
],
name:
"index_container_repository_registry_
repository_id_unique"
,
unique:
true
t
.
index
[
"retry_at"
],
name:
"index_container_repository_registry_on_retry_at"
t
.
index
[
"state"
],
name:
"index_container_repository_registry_on_state"
end
...
...
@@ -217,7 +217,7 @@ ActiveRecord::Schema.define(version: 2021_02_08_175408) do
t
.
text
"last_sync_failure"
t
.
index
[
"retry_at"
],
name:
"index_terraform_state_version_registry_on_retry_at"
t
.
index
[
"state"
],
name:
"index_terraform_state_version_registry_on_state"
t
.
index
[
"terraform_state_version_id"
],
name:
"index_tf_state_versions_registry_
on_tf_state_versions_id"
t
.
index
[
"terraform_state_version_id"
],
name:
"index_tf_state_versions_registry_
tf_state_versions_id_unique"
,
unique:
true
end
end
ee/spec/factories/geo/container_repository_registry.rb
View file @
4f4269ad
...
...
@@ -24,5 +24,9 @@ FactoryBot.define do
last_synced_at
{
1
.
day
.
ago
}
retry_count
{
0
}
end
trait
:with_repository_id
do
sequence
(
:container_repository_id
)
end
end
end
ee/spec/models/geo/container_repository_registry_spec.rb
View file @
4f4269ad
...
...
@@ -6,7 +6,7 @@ RSpec.describe Geo::ContainerRepositoryRegistry, :geo do
include
::
EE
::
GeoHelpers
it_behaves_like
'a BulkInsertSafe model'
,
Geo
::
ContainerRepositoryRegistry
do
let
(
:valid_items_for_bulk_insertion
)
{
build_list
(
:container_repository_registry
,
10
,
container_repository:
create
(
:container_repository
)
,
created_at:
Time
.
zone
.
now
)
}
let
(
:valid_items_for_bulk_insertion
)
{
build_list
(
:container_repository_registry
,
10
,
:with_repository_id
,
created_at:
Time
.
zone
.
now
)
}
let
(
:invalid_items_for_bulk_insertion
)
{
[]
}
# class does not have any validations defined
end
...
...
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