From 3f1cbe1e8fa5257ba7de858a1a34048f141712ce Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 11 Feb 2021 16:53:56 +0530 Subject: [PATCH] migration - 202102111035-template-revisions Fixes: https://github.com/tinkerbell/tink/issues/413 The migration introduces template revisions. A template can have multiple revisions, which are stored in the 'template_revisions' table. The migration also transforms the existing templates to the new template-revisions structure. Therefore ensuring that end users do not face any issues with the introduction of revisions. Signed-off-by: Gaurav Gahlot --- .../202102111035-template-revisions.go | 60 +++++++++++++++++++ db/migration/migration.go | 1 + 2 files changed, 61 insertions(+) create mode 100644 db/migration/202102111035-template-revisions.go diff --git a/db/migration/202102111035-template-revisions.go b/db/migration/202102111035-template-revisions.go new file mode 100644 index 000000000..2fb0aa853 --- /dev/null +++ b/db/migration/202102111035-template-revisions.go @@ -0,0 +1,60 @@ +package migration + +import migrate "github.com/rubenv/sql-migrate" + +// Get202102111035 introduces template revisions. +// +// Fixes: https://github.com/tinkerbell/tink/issues/413 +// +// The migration introduces template revisions. A template can have multiple +// revisions, which are stored in the 'template_revisions' table. +// +// The migration also transforms the existing templates to the new +// template-revisions structure. Therefore ensuring that end users do not +// face any issues with the introduction of revisions. +func Get202102111035() *migrate.Migration { + return &migrate.Migration{ + Id: "202102111035-template-revisions", + Up: []string{` +CREATE TABLE IF NOT EXISTS template_revisions( + template_id UUID + , revision SMALLINT DEFAULT 1 + , data BYTEA + , created_at TIMESTAMPTZ DEFAULT now() + , deleted_at TIMESTAMPTZ + , PRIMARY KEY(template_id, revision) +); + +CREATE INDEX IF NOT EXISTS idx_template_revisions_template_id ON template_revisions (template_id); + +ALTER TABLE template ADD COLUMN revision INT DEFAULT 1; + +CREATE OR REPLACE FUNCTION migrate_to_template_revisions() +RETURNS void AS $$ +DECLARE + template_count integer; + revisions_count integer; +BEGIN + SELECT COUNT(id) INTO template_count + FROM template WHERE deleted_at IS NULL; + + INSERT INTO template_revisions(template_id, data, created_at) + SELECT id, data, created_at + FROM template + WHERE template.deleted_at IS NULL; + + SELECT COUNT(template_id) INTO revisions_count + FROM template_revisions WHERE deleted_at IS NULL; + + IF revisions_count != template_count THEN + RAISE EXCEPTION 'failed to migrate templates'; + END IF; + + ALTER TABLE template DROP COLUMN data; +END; +$$ LANGUAGE plpgsql; + +SELECT migrate_to_template_revisions(); + `}, + } +} diff --git a/db/migration/migration.go b/db/migration/migration.go index a921f577d..8c4e02a74 100644 --- a/db/migration/migration.go +++ b/db/migration/migration.go @@ -11,6 +11,7 @@ var migrations = []func() *migrate.Migration{ Get202012041103, Get202012091055, Get2020121691335, + Get202102111035, } func GetMigrations() *migrate.MemoryMigrationSource {