From 9e39a26f26e44b2d89f68320c509d7738201d894 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 3 Jan 2017 13:15:14 -0500 Subject: [PATCH] backfill version_format column --- .../migrations/00006_add_version_format.go | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/database/pgsql/migrations/00006_add_version_format.go b/database/pgsql/migrations/00006_add_version_format.go index b1e894e7..bf1c55c2 100644 --- a/database/pgsql/migrations/00006_add_version_format.go +++ b/database/pgsql/migrations/00006_add_version_format.go @@ -14,14 +14,57 @@ package migrations -import "github.com/remind101/migrate" +import ( + "database/sql" + "strings" + + "github.com/remind101/migrate" +) func init() { RegisterMigration(migrate.Migration{ ID: 6, - Up: migrate.Queries([]string{ - `ALTER TABLE Namespace ADD COLUMN version_format varchar(128);`, - }), + Up: func(tx *sql.Tx) error { + _, err := tx.Exec(`ALTER TABLE Namespace ADD COLUMN version_format varchar(128);`) + if err != nil { + return err + } + + rows, err := tx.Query(`SELECT name FROM Namespace FOR UPDATE;`) + if err != nil { + return err + } + + for rows.Next() { + var nsName string + err = rows.Scan(&nsName) + if err != nil { + return err + } + + // Backfill rpm namespaces. + if strings.HasPrefix(nsName, "rhel") || + strings.HasPrefix(nsName, "centos") || + strings.HasPrefix(nsName, "fedora") || + strings.HasPrefix(nsName, "amzn") || + strings.HasPrefix(nsName, "scientific") || + strings.HasPrefix(nsName, "ol") || + strings.HasPrefix(nsName, "oracle") { + _, err := tx.Exec(`UPDATE Namespace SET version_format = 'rpm' WHERE name = ?;`, nsName) + if err != nil { + return err + } + } else { + // Fallback to dpkg. + _, err := tx.Exec(`UPDATE Namespace SET version_format = 'dpkg' WHERE name = ?;`, nsName) + if err != nil { + return err + } + } + } + + return nil + }, Down: migrate.Queries([]string{ `ALTER TABLE Namespace DROP COLUMN version_format;`, }),