Skip to content

Commit

Permalink
Update pgx
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jul 1, 2022
1 parent 0e7015c commit 20ca770
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dbump_pgx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions dbump_pgx/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 15 additions & 4 deletions dbump_pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion dbump_pgx/pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 20ca770

Please sign in to comment.