-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes 4741: associate snapshots to templates directly #836
Conversation
aca123c
to
6255c8d
Compare
a5a308b
to
76d1694
Compare
DROP CONSTRAINT IF EXISTS fk_templates_repository_configurations_snapshots, | ||
ADD CONSTRAINT fk_templates_repository_configurations_snapshots | ||
FOREIGN KEY (snapshot_uuid) REFERENCES snapshots(uuid) | ||
ON DELETE SET NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question is whether it should restrict the deletion or set the column null. If we restricted the deletion, it would require all snapshots to be updated for the template prior to deleting the snapshot.
If we allow for null, we could potentially end up with a null value. What would be the ramifications of that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think restricting the deletion instead makes sense, especially if we don't allow the snapshot_uuid
to be null (see reply further down). and before deleting snapshots we should be updating the snapshots in the template anyway (looking at the ticket for delete and bulk-delete)
pkg/dao/snapshots.go
Outdated
Select("snapshots.*, STRING_AGG(repository_configurations.name, '') as repo_name"). | ||
Where("repository_configuration_uuid IN ?", template.RepositoryUUIDS). | ||
Joins("JOIN templates_repository_configurations ON templates_repository_configurations.snapshot_uuid = snapshots.uuid"). | ||
Where("snapshots.repository_configuration_uuid IN ?", template.RepositoryUUIDS). | ||
Where("repository_configurations.name ILIKE ?", fmt.Sprintf("%%%s%%", repositorySearch)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like these lines are repeated between the count query and the actual query:
readableSnapshots(sDao.db.WithContext(ctx), orgID).
Joins("JOIN templates_repository_configurations ON templates_repository_configurations.snapshot_uuid = snapshots.uuid").
Where("snapshots.repository_configuration_uuid IN ?", template.RepositoryUUIDS).
Where("repository_configurations.name ILIKE ?", fmt.Sprintf("%%%s%%", repositorySearch))
you can build a base query struct and re-use it for both
@@ -0,0 +1,11 @@ | |||
BEGIN; | |||
|
|||
ALTER TABLE templates_repository_configurations ADD COLUMN IF NOT EXISTS snapshot_uuid UUID DEFAULT NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This question kinda goes to the question below about the ramifications around a null value here.
Would it be possible to (eventually) have the column be not null? (it might take more than 1 PR to pull this off).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point! i made the default snapshot_uuid
value null because in the API we allow template creation with repositories without snapshots. if we wanted to make this not null, we'd have to validate that all repositories have snapshots before they're added to a template i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd also need to migrate any existing snapshots that are in templates to be included on the table
765ce2f
to
bdb8359
Compare
FOREIGN KEY (snapshot_uuid) REFERENCES snapshots(uuid) | ||
ON DELETE RESTRICT; | ||
|
||
COMMIT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to make the snapshot_uuid
column not null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think so, do you think we should do that here or in a following PR?
to do that we'd need to make sure all repos have snapshots before they're added to a template (which i've added in here). we'd also need to set not null on the column and the model, and since there would be a few seconds where the column would be null until the update-template-content task runs and adds in the snapshot uuids, creating a template would fail. so we'd probably need to add a method to insert the snapshots before the template is created. (and there might be some additional steps that i'm missing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed up these changes!
@@ -620,33 +620,6 @@ func (s *SnapshotsSuite) TestListByTemplate() { | |||
assert.Equal(t, repoConfig2.UUID, snapshots.Data[1].RepositoryUUID) | |||
} | |||
|
|||
func (s *SnapshotsSuite) TestListByTemplateNoRepos() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed this test because templates should now always have snapshots
@@ -1,6 +1,6 @@ | |||
BEGIN; | |||
|
|||
ALTER TABLE templates_repository_configurations ADD COLUMN IF NOT EXISTS snapshot_uuid UUID; | |||
ALTER TABLE templates_repository_configurations ADD COLUMN IF NOT EXISTS snapshot_uuid UUID NOT NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'd have to set it to NOT NULL after the update below
in fact you may want to do the update to set the values, and then delete any null row to be safe and only then set it to NOT NULL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohhh right, makes sense! just pushed up that fix
overall this seemed to work fine. I did see one error though if i:
i get an error:
|
ah good catch. because we're restricting deletion we'll need to delete any rows from template_repo_configs with that repo's snapshot uuids before the repo is deleted. then the snapshot integration test updates are probably not necessary. fixing! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK!
Summary
Testing steps
To test the migration:
GET /templates/:uuid/snapshots/
endpoint)To test a template's snapshots are updated correctly:
GET /templates/:uuid/snapshots/
) should still work as expected