From 20ca77028f13f4a43ea2d633ad6b32bdef7bd3a8 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 1 Jul 2022 22:41:40 +0200 Subject: [PATCH] Update pgx --- dbump_pgx/go.mod | 2 +- dbump_pgx/go.sum | 4 ++-- dbump_pgx/pgx.go | 19 +++++++++++++++---- dbump_pgx/pgx_test.go | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/dbump_pgx/go.mod b/dbump_pgx/go.mod index d73defa..97a60ec 100644 --- a/dbump_pgx/go.mod +++ b/dbump_pgx/go.mod @@ -3,6 +3,6 @@ module github.com/cristalhq/dbump/dbump_pgx go 1.16 require ( - github.com/cristalhq/dbump v0.9.0 + github.com/cristalhq/dbump v0.11.0 github.com/jackc/pgx/v4 v4.16.1 ) diff --git a/dbump_pgx/go.sum b/dbump_pgx/go.sum index 4ce2520..a0eada0 100644 --- a/dbump_pgx/go.sum +++ b/dbump_pgx/go.sum @@ -5,8 +5,8 @@ github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMe github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= -github.com/cristalhq/dbump v0.9.0 h1:kcyQ0T3KkUMaq9PiGpyGXfVjmO7+l/P0ZM67w9XBwZc= -github.com/cristalhq/dbump v0.9.0/go.mod h1:rAjULuStbuNPCLrJT62Eu7Sp/2gVt/4URUvsnPK1yFA= +github.com/cristalhq/dbump v0.11.0 h1:PhPRtOh2j/HXPabmeVv27bnIz0pMR9g/4OKHDIz/oWo= +github.com/cristalhq/dbump v0.11.0/go.mod h1:rAjULuStbuNPCLrJT62Eu7Sp/2gVt/4URUvsnPK1yFA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/dbump_pgx/pgx.go b/dbump_pgx/pgx.go index cfe35d9..80e0e21 100644 --- a/dbump_pgx/pgx.go +++ b/dbump_pgx/pgx.go @@ -39,11 +39,9 @@ func NewMigrator(conn *pgx.Conn, cfg Config) *Migrator { if cfg.Table == "" { cfg.Table = "_dbump_log" } - cfg.tableName += cfg.Table - h := fnv.New64() - h.Write([]byte(cfg.tableName)) - cfg.lockNum = int64(h.Sum64()) + cfg.tableName += cfg.Table + cfg.lockNum = hashTableName(cfg.tableName) return &Migrator{ conn: conn, @@ -62,6 +60,13 @@ CREATE TABLE IF NOT EXISTS %s ( return err } +// Drop is a method from Migrator interface. +func (pg *Migrator) Drop(ctx context.Context) error { + query := fmt.Sprintf(`DROP TABLE IF EXISTS %s;`, pg.cfg.tableName) + _, err := pg.conn.Exec(ctx, query) + return err +} + // LockDB is a method from Migrator interface. func (pg *Migrator) LockDB(ctx context.Context) error { _, err := pg.conn.Exec(ctx, "SELECT pg_advisory_lock($1);", pg.cfg.lockNum) @@ -105,3 +110,9 @@ func (pg *Migrator) DoStep(ctx context.Context, step dbump.Step) error { return err }) } + +func hashTableName(s string) int64 { + h := fnv.New64() + h.Write([]byte(s)) + return int64(h.Sum64()) +} diff --git a/dbump_pgx/pgx_test.go b/dbump_pgx/pgx_test.go index 29d2559..aba889f 100644 --- a/dbump_pgx/pgx_test.go +++ b/dbump_pgx/pgx_test.go @@ -104,7 +104,7 @@ func TestMigrateUp(t *testing.T) { Table: "TestMigrateUp", }), Loader: dbump.NewSliceLoader(migrations), - Mode: dbump.ModeUp, + Mode: dbump.ModeApplyAll, } failIfErr(t, dbump.Run(context.Background(), cfg))