From f0cf5f9b2e880c42eb8d27a70ca6556203f5205a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Tue, 14 Jun 2016 14:28:05 +0200 Subject: [PATCH] Update sqly to get the migration path from the env --- Makefile | 7 +++---- src/internal/sqly/sqly.go | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index ac84aa2..faff1ed 100644 --- a/Makefile +++ b/Makefile @@ -53,15 +53,14 @@ docker: $(DOCKER_COMPOSE) up -d sleep 4 -migration-tool: - go get -v github.com/mattes/migrate - -migration-schema: docker migration-tool +migration-schema: docker $(MIGRATION) -path $(MIGRATION_SCHEMA) up migration-dev-data: docker migration-schema $(MIGRATION) -path $(MIGRATION_TEST_DATA) up +migration: migration-schema migration-dev-data + dev: docker migration watch clean: diff --git a/src/internal/sqly/sqly.go b/src/internal/sqly/sqly.go index 1584847..faee992 100644 --- a/src/internal/sqly/sqly.go +++ b/src/internal/sqly/sqly.go @@ -1,7 +1,7 @@ package sqly import ( - "fmt" + "os" "testing" "time" @@ -9,6 +9,15 @@ import ( "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 type BaseModel struct { ID string @@ -19,9 +28,8 @@ type BaseModel struct { // 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)) { defer func() { - allErrors, ok := migrate.DownSync(pgdsn, "../sql") + allErrors, ok := migrate.DownSync(pgdsn, migrationPath) if !ok { - fmt.Println("Oh no ...") for _, err := range allErrors { t.Log(err) 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 { - fmt.Println("Oh no ...") for _, err := range allErrors { t.Log(err) t.Fatal("Impossible to run test we get some errors when initialize the database schema")