db/pgsql/migration: convert to pure SQL
This commit is contained in:
parent
8bedd0a367
commit
8df8170ba5
@ -14,57 +14,16 @@
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/remind101/migrate"
|
||||
)
|
||||
import "github.com/remind101/migrate"
|
||||
|
||||
func init() {
|
||||
RegisterMigration(migrate.Migration{
|
||||
ID: 6,
|
||||
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
|
||||
},
|
||||
Up: migrate.Queries([]string{
|
||||
`ALTER TABLE Namespace ADD COLUMN version_format varchar(128);`,
|
||||
`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%';`,
|
||||
`UPDATE Namespace SET version_format = 'dpkg' WHERE version_format != 'rpm';`,
|
||||
}),
|
||||
Down: migrate.Queries([]string{
|
||||
`ALTER TABLE Namespace DROP COLUMN version_format;`,
|
||||
}),
|
||||
|
Loading…
Reference in New Issue
Block a user