Skip to content

Commit

Permalink
db/pgsql/migration: convert to pure SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jan 3, 2017
1 parent 8bedd0a commit 8df8170
Showing 1 changed file with 6 additions and 47 deletions.
53 changes: 6 additions & 47 deletions database/pgsql/migrations/00006_add_version_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;`,
}),
Expand Down

0 comments on commit 8df8170

Please sign in to comment.