Update sqly to get the migration path from the env

This commit is contained in:
Grégoire Delattre 2016-06-14 14:28:05 +02:00
parent a2adc4ccf5
commit f0cf5f9b2e
2 changed files with 15 additions and 9 deletions

View File

@ -53,15 +53,14 @@ docker:
$(DOCKER_COMPOSE) up -d $(DOCKER_COMPOSE) up -d
sleep 4 sleep 4
migration-tool: migration-schema: docker
go get -v github.com/mattes/migrate
migration-schema: docker migration-tool
$(MIGRATION) -path $(MIGRATION_SCHEMA) up $(MIGRATION) -path $(MIGRATION_SCHEMA) up
migration-dev-data: docker migration-schema migration-dev-data: docker migration-schema
$(MIGRATION) -path $(MIGRATION_TEST_DATA) up $(MIGRATION) -path $(MIGRATION_TEST_DATA) up
migration: migration-schema migration-dev-data
dev: docker migration watch dev: docker migration watch
clean: clean:

View File

@ -1,7 +1,7 @@
package sqly package sqly
import ( import (
"fmt" "os"
"testing" "testing"
"time" "time"
@ -9,6 +9,15 @@ import (
"github.com/mattes/migrate/migrate" "github.com/mattes/migrate/migrate"
) )
var migrationPath string
func init() {
migrationPath = os.Getenv("MIGRATION_PATH")
if migrationPath == "" {
migrationPath = "../../../sql/migration"
}
}
// BaseModel have to be embeded in all your struct which reflect a table // BaseModel have to be embeded in all your struct which reflect a table
type BaseModel struct { type BaseModel struct {
ID string ID string
@ -19,9 +28,8 @@ type BaseModel struct {
// RunWithLastestMigration runs your test with database migration set to the lastest // RunWithLastestMigration runs your test with database migration set to the lastest
func RunWithLastestMigration(db *sqlx.DB, pgdsn string, t *testing.T, test func(db *sqlx.DB, t *testing.T)) { func RunWithLastestMigration(db *sqlx.DB, pgdsn string, t *testing.T, test func(db *sqlx.DB, t *testing.T)) {
defer func() { defer func() {
allErrors, ok := migrate.DownSync(pgdsn, "../sql") allErrors, ok := migrate.DownSync(pgdsn, migrationPath)
if !ok { if !ok {
fmt.Println("Oh no ...")
for _, err := range allErrors { for _, err := range allErrors {
t.Log(err) t.Log(err)
t.Fatal("We get some errors when reset the database schema") t.Fatal("We get some errors when reset the database schema")
@ -29,9 +37,8 @@ func RunWithLastestMigration(db *sqlx.DB, pgdsn string, t *testing.T, test func(
} }
}() }()
allErrors, ok := migrate.UpSync(pgdsn, "../sql") allErrors, ok := migrate.UpSync(pgdsn, migrationPath)
if !ok { if !ok {
fmt.Println("Oh no ...")
for _, err := range allErrors { for _, err := range allErrors {
t.Log(err) t.Log(err)
t.Fatal("Impossible to run test we get some errors when initialize the database schema") t.Fatal("Impossible to run test we get some errors when initialize the database schema")