From 736ca136604e9ad86ac800b7f1481831cf41d696 Mon Sep 17 00:00:00 2001 From: Lee Brown Date: Wed, 21 Aug 2019 23:51:42 -0800 Subject: [PATCH] try to fix broken schema migration for accounts table --- README.md | 2 +- build/cicd/README.md | 2 +- internal/schema/migrations.go | 28 +++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5900d72..add8e69 100644 --- a/README.md +++ b/README.md @@ -616,7 +616,7 @@ so each job is dependant on the previous or run jobs for each target environment A build tool called [devops](https://gitlab.com/geeks-accelerator/oss/saas-starter-kit/tree/master/tools/devops) has been included apart of this project. _Devops_ handles creating AWS resources and deploying your services with minimal -additional configuration. You can customizing any of the configuration in the code. While AWS is already a core part of +additional configuration. You can customize any of the configuration in the code. While AWS is already a core part of the saas-starter-kit, keeping the deployment in GoLang limits the scope of additional technologies required to get your project successfully up and running. If you understand Golang, then you will be a master at devops with this tool. diff --git a/build/cicd/README.md b/build/cicd/README.md index db7ddc1..35f1d9f 100644 --- a/build/cicd/README.md +++ b/build/cicd/README.md @@ -249,7 +249,7 @@ the install instructions for Go](http://golang.org/doc/install.html). To install _cicd_, simply run: ``` -$ go get geeks-accelerator/oss/saas-starter-kit/build/cicd +$ go get -v geeks-accelerator/oss/saas-starter-kit/build/cicd ``` Make sure your `PATH` includes the `$GOPATH/bin` directory so your commands can diff --git a/internal/schema/migrations.go b/internal/schema/migrations.go index 3e5c40a..78a9433 100644 --- a/internal/schema/migrations.go +++ b/internal/schema/migrations.go @@ -59,6 +59,9 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest { ID: "20190522-01c", Migrate: func(tx *sql.Tx) error { + if err := dropTypeIfExists(tx, "account_status_t"); err != nil { + return err + } q1 := `CREATE TYPE account_status_t as enum('active','pending','disabled')` if _, err := tx.Exec(q1); err != nil && !errorIsAlreadyExists(err) { return errors.WithMessagef(err, "Query failed %s", q1) @@ -89,7 +92,7 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest return nil }, Rollback: func(tx *sql.Tx) error { - q1 := `DROP TYPE account_status_t` + q1 := `DROP TYPE IF EXISTS account_status_t` if _, err := tx.Exec(q1); err != nil { return errors.WithMessagef(err, "Query failed %s", q1) } @@ -105,11 +108,17 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest { ID: "20190522-01e", Migrate: func(tx *sql.Tx) error { + if err := dropTypeIfExists(tx, "user_account_role_t"); err != nil { + return err + } q1 := `CREATE TYPE user_account_role_t as enum('admin', 'user')` if _, err := tx.Exec(q1); err != nil && !errorIsAlreadyExists(err) { return errors.WithMessagef(err, "Query failed %s", q1) } + if err := dropTypeIfExists(tx, "user_account_status_t"); err != nil { + return err + } q2 := `CREATE TYPE user_account_status_t as enum('active', 'invited','disabled')` if _, err := tx.Exec(q2); err != nil && !errorIsAlreadyExists(err) { return errors.WithMessagef(err, "Query failed %s", q2) @@ -139,7 +148,7 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest return errors.WithMessagef(err, "Query failed %s", q1) } - q2 := `DROP TYPE userr_account_status_t` + q2 := `DROP TYPE user_account_status_t` if _, err := tx.Exec(q2); err != nil { return errors.WithMessagef(err, "Query failed %s", q2) } @@ -156,6 +165,10 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest { ID: "20190622-01", Migrate: func(tx *sql.Tx) error { + if err := dropTypeIfExists(tx, "project_status_t"); err != nil { + return err + } + q1 := `CREATE TYPE project_status_t as enum('active','disabled')` if _, err := tx.Exec(q1); err != nil && !errorIsAlreadyExists(err) { return errors.WithMessagef(err, "Query failed %s", q1) @@ -669,10 +682,19 @@ func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest } } +// dropTypeIfExists executes drop type. +func dropTypeIfExists(tx *sql.Tx, name string) error { + q := "DROP TYPE IF EXISTS" + name + if _, err := tx.Exec(q); err != nil && !errorIsAlreadyExists(err) { + return errors.WithMessagef(err, "Query failed %s", q) + } + return nil +} + // errorIsAlreadyExists checks an error message for the error "already exists" func errorIsAlreadyExists(err error) bool { if strings.Contains(err.Error(), "already exists") { return true } return false -} \ No newline at end of file +}