Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix and organize migration code

goenning04e14e3b2af0
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 13c26fb6c..fdb3e1fb8 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -12,7 +12,7 @@
"wechy": true,
"wechy.exe": true,
"tmp": true,
- "vendor": true,
+ "vendor": false,
"node_modules": true,
"dist": true,
"coverage.txt": true,
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.go
index afd4745cb..0d23aadb9 100644
--- a/app/pkg/dbx/dbx.go
+++ b/app/pkg/dbx/dbx.go
@@ -7,10 +7,12 @@ import (
"reflect"
"github.com/WeCanHearYou/wechy/app/pkg/env"
- "github.com/mattes/migrate/migrate"
+ "github.com/mattes/migrate"
//required
_ "github.com/lib/pq"
+ _ "github.com/mattes/migrate/database/postgres"
+ _ "github.com/mattes/migrate/source/file"
)
// New creates a new Database instance
@@ -22,6 +24,7 @@ func New() (*Database, error) {
db := &Database{conn}
if env.IsTest() {
+ db.Migrate()
db.load("/app/pkg/dbx/setup.sql")
}
return db, nil
@@ -206,12 +209,19 @@ func (db Database) load(path string) {
// Migrate the database to latest verion
func (db Database) Migrate() {
+
fmt.Printf("Running migrations... \n")
- errors, ok := migrate.UpSync(env.MustGet("DATABASE_URL"), env.Path("/migrations"))
- if !ok {
- for i, err := range errors {
- fmt.Printf("Error #%d: %s.\n", i, err)
- }
+ m, err := migrate.New(
+ "file://"+env.Path("migrations"),
+ env.MustGet("DATABASE_URL"),
+ )
+
+ if err == nil {
+ err = m.Up()
+ }
+
+ if err != nil && err != migrate.ErrNoChange {
+ fmt.Printf("Error: %s.\n", err)
panic("Migrations failed.")
} else {
diff --git a/app/pkg/dbx/dbx_test.go b/app/pkg/dbx/dbx_test.go
index 8492af7f1..023679fd3 100644
--- a/app/pkg/dbx/dbx_test.go
+++ b/app/pkg/dbx/dbx_test.go
@@ -1,11 +1,9 @@
package dbx_test
import (
- "os"
"testing"
"github.com/WeCanHearYou/wechy/app/pkg/dbx"
- _ "github.com/mattes/migrate/driver/postgres"
. "github.com/onsi/gomega"
)
@@ -26,15 +24,6 @@ type tenant struct {
Name string `db:"name"`
}
-func TestMain(m *testing.M) {
- db, _ := dbx.New()
- db.Migrate()
- db.Close()
-
- code := m.Run()
- os.Exit(code)
-}
-
func TestBind_SimpleStruct(t *testing.T) {
RegisterTestingT(t)
db, _ := dbx.New()
diff --git a/app/storage/postgres/setup_test.go b/app/storage/postgres/setup_test.go
index 20768fb7d..0159f389b 100644
--- a/app/storage/postgres/setup_test.go
+++ b/app/storage/postgres/setup_test.go
@@ -5,7 +5,6 @@ import (
"testing"
"github.com/WeCanHearYou/wechy/app/pkg/dbx"
- _ "github.com/mattes/migrate/driver/postgres"
)
func TestMain(m *testing.M) {
diff --git a/main.go b/main.go
index 2bf0354c9..1fda7f2c3 100644
--- a/main.go
+++ b/main.go
@@ -11,7 +11,6 @@ import (
"github.com/WeCanHearYou/wechy/app/pkg/oauth"
"github.com/WeCanHearYou/wechy/app/storage/postgres"
_ "github.com/lib/pq"
- mig "github.com/mattes/migrate"
_ "github.com/mattes/migrate/database/postgres"
_ "github.com/mattes/migrate/source/file"
)
@@ -19,27 +18,7 @@ import (
var buildtime string
var version = "0.2.0"
-func migrate() {
- fmt.Printf("Running migrations... \n")
- m, err := mig.New(
- "file://./migrations",
- env.MustGet("DATABASE_URL"),
- )
-
- if err == nil {
- err = m.Up()
- }
-
- if err != nil && err != mig.ErrNoChange {
- fmt.Printf("Error: %s.\n", err)
-
- panic("Migrations failed.")
- } else {
- fmt.Printf("Migrations finished with success.\n")
- }
-}
-
-func init() {
+func main() {
fmt.Printf("Application is starting...\n")
fmt.Printf("GO_ENV: %s\n", env.Current())