Commit 4ae4c53a authored by João Pereira's avatar João Pereira 🌴
Browse files

feat(gc): use statement-level trigger for tracking deleted layers

parent 3785b420
......@@ -41,6 +41,32 @@ END;
$$;
```
##### Create Trigger
`CREATE OR REPLACE` for triggers is only supported on PostreSQL 14, so we need a custom idempotent `CREATE`:
```sql
DO $$
BEGIN
IF NOT EXISTS (
SELECT
FROM
pg_trigger
WHERE
tgname = 'gc_track_deleted_layers_trigger'
AND tgrelid = 'layers'::regclass) THEN
CREATE TRIGGER gc_track_deleted_layers_trigger
AFTER DELETE ON layers REFERENCING OLD TABLE AS old_table
FOR EACH STATEMENT
EXECUTE FUNCTION gc_track_deleted_layers ();
END IF;
END
$$
```
If replacing an existing trigger in a single migration (`DROP` existent followed by `CREATE` new), there is no need to
do this check as the trigger is guaranteed to no longer exist after the `DROP` statement.
##### Insert Rows
When inserting rows on tables we should take care to ensure idempotency. This means
......
......@@ -635,6 +635,9 @@ func TestGC_TrackDeletedLayers_PostponeReviewOnConflict(t *testing.T) {
}
func TestGC_TrackDeletedLayers_DoesNothingIfTriggerDisabled(t *testing.T) {
// FIXME: See https://gitlab.com/gitlab-org/container-registry/-/merge_requests/1104#note_1055446124 for context.
t.Skip("Test skipped due to PosgtreSQL bug. Re-enable once PostgreSQL 12.12 is released.")
require.NoError(t, testutil.TruncateAllTables(suite.db))
enable, err := testutil.GCTrackDeletedLayersTrigger.Disable(suite.db)
......
package migrations
import migrate "github.com/rubenv/sql-migrate"
func init() {
m := &Migration{
Migration: &migrate.Migration{
Id: "20220803114849_update_gc_track_deleted_layers_trigger",
// switch from row-level to statement-level execution
Up: []string{
"DROP TRIGGER IF EXISTS gc_track_deleted_layers_trigger ON layers",
`CREATE TRIGGER gc_track_deleted_layers_trigger
AFTER DELETE ON layers REFERENCING OLD TABLE AS old_table
FOR EACH STATEMENT
EXECUTE FUNCTION gc_track_deleted_layers ()`,
},
// restore previous version
Down: []string{
"DROP TRIGGER IF EXISTS gc_track_deleted_layers_trigger ON layers",
`CREATE TRIGGER gc_track_deleted_layers_trigger
AFTER DELETE ON layers
FOR EACH ROW
EXECUTE FUNCTION gc_track_deleted_layers ()`,
},
},
PostDeployment: false,
}
allMigrations = append(allMigrations, m)
}
......@@ -15477,8 +15477,8 @@ CREATE TRIGGER gc_track_configuration_blobs_trigger
EXECUTE FUNCTION public.gc_track_configuration_blobs ();
 
CREATE TRIGGER gc_track_deleted_layers_trigger
AFTER DELETE ON public.layers
FOR EACH ROW
AFTER DELETE ON public.layers REFERENCING OLD TABLE AS old_table
FOR EACH STATEMENT
EXECUTE FUNCTION public.gc_track_deleted_layers ();
 
CREATE TRIGGER gc_track_deleted_manifest_lists_trigger
......
Supports Markdown
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