-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes 4741: associate snapshots to templates directly (#836)
* Fixes 4741: associate snapshots to templates directly * make snapshot_uuid not null, insert uuids on create, fix tests * lint * fix migration * remove snaps from trc table before deleting
- Loading branch information
Showing
21 changed files
with
411 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
20241011084507 | ||
20241018154315 | ||
|
8 changes: 8 additions & 0 deletions
8
db/migrations/20241018154315_associate_snaps_to_templates.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE templates_repository_configurations DROP COLUMN IF EXISTS snapshot_uuid; | ||
|
||
ALTER TABLE templates_repository_configurations | ||
DROP CONSTRAINT IF EXISTS fk_templates_repository_configurations_snapshots; | ||
|
||
COMMIT; |
65 changes: 65 additions & 0 deletions
65
db/migrations/20241018154315_associate_snaps_to_templates.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE templates_repository_configurations ADD COLUMN IF NOT EXISTS snapshot_uuid UUID; | ||
|
||
-- migrate snapshots for use_latest templates | ||
UPDATE templates_repository_configurations trc | ||
SET snapshot_uuid = ( | ||
SELECT rc.last_snapshot_uuid | ||
FROM repository_configurations rc | ||
JOIN templates t ON t.uuid = trc.template_uuid | ||
WHERE rc.uuid = trc.repository_configuration_uuid | ||
AND t.use_latest = true | ||
AND rc.last_snapshot_uuid IS NOT NULL | ||
) | ||
WHERE trc.template_uuid IN ( | ||
SELECT t.uuid | ||
FROM templates t | ||
WHERE t.use_latest = true | ||
); | ||
|
||
-- migrate snapshots for templates with a snapshot date | ||
UPDATE templates_repository_configurations trc | ||
SET snapshot_uuid = ( | ||
SELECT closest_snapshots.uuid | ||
FROM ( | ||
(SELECT s.uuid, s.created_at | ||
FROM snapshots s | ||
JOIN templates t ON t.uuid = trc.template_uuid | ||
WHERE s.repository_configuration_uuid = trc.repository_configuration_uuid | ||
AND t.use_latest = false | ||
AND s.created_at <= t.date | ||
ORDER BY s.created_at DESC | ||
LIMIT 1) | ||
|
||
UNION | ||
|
||
(SELECT s.uuid, s.created_at | ||
FROM snapshots s | ||
JOIN templates t ON t.uuid = trc.template_uuid | ||
WHERE s.repository_configuration_uuid = trc.repository_configuration_uuid | ||
AND t.use_latest = false | ||
AND s.created_at > t.date | ||
ORDER BY s.created_at ASC | ||
LIMIT 1) | ||
) AS closest_snapshots | ||
ORDER BY closest_snapshots.created_at ASC | ||
LIMIT 1 | ||
) | ||
WHERE trc.template_uuid IN ( | ||
SELECT t.uuid | ||
FROM templates t | ||
WHERE t.use_latest = false | ||
); | ||
|
||
DELETE FROM templates_repository_configurations WHERE snapshot_uuid IS NULL; | ||
|
||
ALTER TABLE templates_repository_configurations ALTER COLUMN snapshot_uuid SET NOT NULL; | ||
|
||
ALTER TABLE templates_repository_configurations | ||
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 RESTRICT; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.