db/pgsql/migration: convert to pure SQL

This commit is contained in:
Jimmy Zelinskie 2017-01-03 15:59:22 -05:00
parent 8bedd0a367
commit 8df8170ba5

View File

@ -14,57 +14,16 @@
package migrations package migrations
import ( import "github.com/remind101/migrate"
"database/sql"
"strings"
"github.com/remind101/migrate"
)
func init() { func init() {
RegisterMigration(migrate.Migration{ RegisterMigration(migrate.Migration{
ID: 6, ID: 6,
Up: func(tx *sql.Tx) error { Up: migrate.Queries([]string{
_, err := tx.Exec(`ALTER TABLE Namespace ADD COLUMN version_format varchar(128);`) `ALTER TABLE Namespace ADD COLUMN version_format varchar(128);`,
if err != nil { `UPDATE Namespace SET version_format = 'rpm' WHERE name LIKE 'rhel%' OR name LIKE 'centos%' OR name LIKE 'fedora%' OR name LIKE 'amzn%' OR name LIKE 'scientific%' OR name LIKE 'ol%' OR name LIKE 'oracle%';`,
return err `UPDATE Namespace SET version_format = 'dpkg' WHERE version_format != 'rpm';`,
} }),
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{ Down: migrate.Queries([]string{
`ALTER TABLE Namespace DROP COLUMN version_format;`, `ALTER TABLE Namespace DROP COLUMN version_format;`,
}), }),