storj/storage/postgreskv/schema/migrate.go
Jess G 8d92c288e2
satellitedb: separate migration into subcommand (#3436)
* separate sadb migration, add version check

* update checkversion to do same validation as migration

* changes per CR

* add sa migration to storj-sim

* add different debug port in storj-sim for migration

* add wait for exit for storj-sim migration

* update sa docker entrypoint to support migration

* storj-sim satellite parts all wait for migration

* upgrade golang-migrate/migrate to v4 because bug

* fix go mod tidy
2019-11-02 13:09:07 -07:00

52 lines
1.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
//go:generate go-bindata -o data.go -pkg schema -ignore ".*go" .
package schema
import (
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
"github.com/zeebo/errs"
"storj.io/storj/internal/dbutil/pgutil"
)
// PrepareDB applies schema migrations as necessary to the given database to
// get it up to date.
func PrepareDB(db *sql.DB, dbURL string) error {
srcDriver, err := bindata.WithInstance(bindata.Resource(AssetNames(), Asset))
if err != nil {
return err
}
schema, err := pgutil.ParseSchemaFromConnstr(dbURL)
if err != nil {
return errs.New("error parsing schema: %+v", err)
}
if schema != "" {
err := pgutil.CreateSchema(db, schema)
if err != nil {
return errs.New("error creating schema: %+v", err)
}
}
dbDriver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
return err
}
m, err := migrate.NewWithInstance("go-bindata migrations", srcDriver, "postgreskv db", dbDriver)
if err != nil {
return err
}
err = m.Up()
if err == migrate.ErrNoChange {
err = nil
}
return err
}