backfill version_format column

This commit is contained in:
Jimmy Zelinskie 2017-01-03 13:15:14 -05:00
parent 6864a8efea
commit 9e39a26f26

View File

@ -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;`,
}),