Skip to content

Commit

Permalink
refactor migrate commands (#430)
Browse files Browse the repository at this point in the history
* refactor migrate commands

- remove create command and goto command, refactor migration to use
transaction to be able roolback when a file with multiple statements
fail

In case of goto we removed because it do the same of next.
Create: we need a command that just create a file?

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* lint fix

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* fix tests

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* try to fix called twice on migration

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* Revert "try to fix called twice on migration"

This reverts commit 8733843.

* try to fix called twice on migration

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* update migration

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* migration v3

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* backwards compatible

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* lint

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* fix query

Signed-off-by: Felipe Oliveira <fpo@felipeweb.dev>

* set docker registry path (#432)

* set docker registry path
fixed: #431

Signed-off-by: Avelino <t@avelino.xxx>

* fixed travis PR test

Signed-off-by: Avelino <t@avelino.xxx>

* upgrade all go dep packages

Signed-off-by: Avelino <t@avelino.xxx>

Co-authored-by: Avelino <t@avelino.xxx>
  • Loading branch information
felipeweb and avelino committed Sep 14, 2020
1 parent d9d3ebc commit cfa0f1f
Show file tree
Hide file tree
Showing 24 changed files with 690 additions and 404 deletions.
1 change: 1 addition & 0 deletions adapters/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ type Adapter interface {
UpdateWithTransaction(tx *sql.Tx, SQL string, params ...interface{}) (sc Scanner)
UpdateSQL(database string, schema string, table string, setSyntax string) string
WhereByRequest(r *http.Request, initialPlaceholderID int) (whereSyntax string, values []interface{}, err error)
ShowTable(schema, table string) (sc Scanner)
}
13 changes: 9 additions & 4 deletions adapters/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ go 1.13
require (
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.8.0
github.com/mattn/go-sqlite3 v1.14.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nuveo/log v0.0.0-20190430190217-44d02db6bdf8
github.com/prest/prest/config v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/template v0.0.0-20200729234819-07cc1b6b492f
github.com/spf13/viper v1.4.0 // indirect
github.com/stretchr/testify v1.2.2
)

replace (
github.com/prest/prest/adapters => ../adapters
github.com/prest/prest/config => ../config
github.com/prest/prest/controllers => ../controllers
github.com/prest/prest/helpers => ../helpers
github.com/prest/prest/middlewares => ../middlewares
github.com/prest/prest/template => ../template
)
199 changes: 181 additions & 18 deletions adapters/go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions adapters/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ func (m *Mock) BatchInsertCopy(dbname, schema, table string, keys []string, valu
return
}

// ShowTable shows table structure
func (m *Mock) ShowTable(schema, table string) (sc adapters.Scanner) {
return
}

// AddItem on mock object
func (m *Mock) AddItem(body []byte, err error, isCount bool) {
i := Item{
Expand Down
16 changes: 16 additions & 0 deletions adapters/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,3 +1394,19 @@ func (adapter *Postgres) SchemaTablesOrderBy(order string) (orderBy string) {
}
return
}

// ShowTable shows table structure
func (adapter *Postgres) ShowTable(schema, table string) adapters.Scanner {
query := `SELECT table_schema, table_name, ordinal_position as position, column_name,data_type,
CASE WHEN character_maximum_length is not null
THEN character_maximum_length
ELSE numeric_precision end as max_length,
is_nullable,
is_generated,
is_updatable,
column_default as default_value
FROM information_schema.columns
WHERE table_name=$1 AND table_schema=$2
ORDER BY table_schema, table_name, ordinal_position`
return adapter.Query(query, table, schema)
}
35 changes: 0 additions & 35 deletions cmd/create.go

This file was deleted.

33 changes: 17 additions & 16 deletions cmd/down.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package cmd

import (
"context"
"fmt"
"os"
"time"

"github.com/gosidekick/migration/v3"
"github.com/spf13/cobra"
// postgres driver for migrate
_ "gopkg.in/mattes/migrate.v1/driver/postgres"
"gopkg.in/mattes/migrate.v1/migrate"
)

// downCmd represents the down command
var downCmd = &cobra.Command{
Use: "down",
Short: "Roll back all migrations",
Long: `Roll back all migrations`,
Run: func(cmd *cobra.Command, args []string) {
verifyMigrationsPath(path)
timerStart = time.Now()
pipe := migrate.NewPipe()
go migrate.Down(pipe, urlConn, path)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(-1)
Use: "down",
Short: "Roll back all migrations",
Long: `Roll back all migrations`,
PreRunE: checkTable,
RunE: func(cmd *cobra.Command, args []string) error {
n, executed, err := migration.Run(context.Background(), path, urlConn, "down")
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "exec migrations located in %v\n", path)
fmt.Fprintf(os.Stdout, "executed %v migrations\n", n)
for _, e := range executed {
fmt.Fprintf(os.Stdout, "%v SUCCESS\n", e)
}
return nil
},
}
20 changes: 12 additions & 8 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ module github.com/prest/prest/cmd
go 1.13

require (
github.com/auth0/go-jwt-middleware v0.0.0-20200507191422-d30d7b9ece63 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/fatih/color v1.9.0
github.com/gorilla/mux v1.7.4
github.com/jmoiron/sqlx v1.2.0 // indirect
github.com/lib/pq v1.8.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/gosidekick/migration/v3 v3.0.0
github.com/lib/pq v1.8.0
github.com/nuveo/log v0.0.0-20190430190217-44d02db6bdf8
github.com/prest/prest/adapters v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/config v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/controllers v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/helpers v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/middlewares v0.0.0-20200729234819-07cc1b6b492f
github.com/prest/prest/template v0.0.0-20200729234819-07cc1b6b492f // indirect
github.com/spf13/cobra v1.0.0
github.com/urfave/negroni v1.0.0
gopkg.in/mattes/migrate.v1 v1.3.2
)

replace (
github.com/prest/prest/adapters => ../adapters
github.com/prest/prest/config => ../config
github.com/prest/prest/controllers => ../controllers
github.com/prest/prest/helpers => ../helpers
github.com/prest/prest/middlewares => ../middlewares
github.com/prest/prest/template => ../template
)

0 comments on commit cfa0f1f

Please sign in to comment.