Added -pgschema option for Postgres

pull/2/head
Kevin Gorjan 2014-05-24 17:35:33 +02:00 committed by Liam Staskawicz
parent e885648f5a
commit 206ca342c5
7 changed files with 46 additions and 5 deletions

View File

@ -44,6 +44,16 @@ Apply all available migrations.
$ OK 002_next.sql
$ OK 003_and_again.go
## -pgschema up
Apply all available migrations to a specific postgres-schema
$ goose -pgschema=my_schema_name up
$ goose: migrating db environment 'development', current version: 0, target: 3
$ OK 001_basics.sql
$ OK 002_next.sql
$ OK 003_and_again.go
## down
Roll back a single migration from the current version.

View File

@ -43,6 +43,13 @@ func statusRun(cmd *Command, args ...string) {
}
defer db.Close()
if conf.Driver.Name == "postgres" {
_, err := db.Exec("SET search_path TO " + conf.PgSchema)
if err != nil {
log.Fatal(err)
}
}
// must ensure that the version table exists if we're running on a pristine DB
if _, e := goose.EnsureDBVersion(conf, db); e != nil {
log.Fatal(e)

View File

@ -12,10 +12,11 @@ import (
// global options. available to any subcommands.
var flagPath = flag.String("path", "db", "folder containing db info")
var flagEnv = flag.String("env", "development", "which DB environment to use")
var flagPgSchema = flag.String("pgschema", "", "which postgres-schema to migrate (default = none)")
// helper to create a DBConf from the given flags
func dbConfFromFlags() (dbconf *goose.DBConf, err error) {
return goose.NewDBConf(*flagPath, *flagEnv)
return goose.NewDBConf(*flagPath, *flagEnv, *flagPgSchema)
}
var commands = []*Command{

View File

@ -22,10 +22,11 @@ type DBConf struct {
MigrationsDir string
Env string
Driver DBDriver
PgSchema string
}
// extract configuration details from the given file
func NewDBConf(p, env string) (*DBConf, error) {
func NewDBConf(p, env string, pgschema string) (*DBConf, error) {
cfgFile := filepath.Join(p, "dbconf.yml")
@ -74,6 +75,7 @@ func NewDBConf(p, env string) (*DBConf, error) {
MigrationsDir: filepath.Join(p, "migrations"),
Env: env,
Driver: d,
PgSchema: pgschema,
}, nil
}

View File

@ -7,7 +7,7 @@ import (
func TestBasics(t *testing.T) {
dbconf, err := NewDBConf("../../db-sample", "test")
dbconf, err := NewDBConf("../../db-sample", "test", "")
if err != nil {
t.Fatal(err)
}
@ -24,7 +24,7 @@ func TestBasics(t *testing.T) {
func TestImportOverride(t *testing.T) {
dbconf, err := NewDBConf("../../db-sample", "customimport")
dbconf, err := NewDBConf("../../db-sample", "customimport", "")
if err != nil {
t.Fatal(err)
}

View File

@ -5,8 +5,8 @@ import (
"errors"
"fmt"
_ "github.com/lib/pq"
_ "github.com/ziutek/mymysql/godrv"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
"log"
"os"
"path/filepath"
@ -54,6 +54,13 @@ func RunMigrations(conf *DBConf, migrationsDir string, target int64) (err error)
}
defer db.Close()
if conf.Driver.Name == "postgres" {
_, err := db.Exec("SET search_path TO " + conf.PgSchema)
if err != nil {
return err
}
}
current, err := EnsureDBVersion(conf, db)
if err != nil {
return err
@ -268,6 +275,13 @@ func GetDBVersion(conf *DBConf) (version int64, err error) {
}
defer db.Close()
if conf.Driver.Name == "postgres" {
_, err := db.Exec("SET search_path TO " + conf.PgSchema)
if err != nil {
return -1, err
}
}
version, err = EnsureDBVersion(conf, db)
if err != nil {
return -1, err

View File

@ -86,6 +86,13 @@ func main() {
log.Fatal("failed to open DB:", err)
}
defer db.Close()
if conf.Driver.Name == "postgres" {
_, err := db.Exec("SET search_path TO " + conf.PgSchema)
if err != nil {
log.Fatal("SET search_path:", err)
}
}
txn, err := db.Begin()
if err != nil {