Added -pgschema option for Postgres

This commit is contained in:
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 002_next.sql
$ OK 003_and_again.go $ 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 ## down
Roll back a single migration from the current version. Roll back a single migration from the current version.

View File

@ -43,6 +43,13 @@ func statusRun(cmd *Command, args ...string) {
} }
defer db.Close() 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 // must ensure that the version table exists if we're running on a pristine DB
if _, e := goose.EnsureDBVersion(conf, db); e != nil { if _, e := goose.EnsureDBVersion(conf, db); e != nil {
log.Fatal(e) log.Fatal(e)

View File

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

View File

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

View File

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

View File

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

View File

@ -87,6 +87,13 @@ func main() {
} }
defer db.Close() 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() txn, err := db.Begin()
if err != nil { if err != nil {
log.Fatal("db.Begin:", err) log.Fatal("db.Begin:", err)