Skip to content

Commit

Permalink
Update pgx with new tests package
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jul 6, 2022
1 parent 7a3d79c commit 6d16391
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 480 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.11.0
github.com/cristalhq/dbump v0.12.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.11.0 h1:PhPRtOh2j/HXPabmeVv27bnIz0pMR9g/4OKHDIz/oWo=
github.com/cristalhq/dbump v0.11.0/go.mod h1:rAjULuStbuNPCLrJT62Eu7Sp/2gVt/4URUvsnPK1yFA=
github.com/cristalhq/dbump v0.12.0 h1:hu4oKPyYbPYBfUJ5BbopFEKNcr5szKqAOzA97xwXJQA=
github.com/cristalhq/dbump v0.12.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
24 changes: 17 additions & 7 deletions dbump_pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ type Config struct {
// Table for the dbump version table. Default is empty which means "_dbump_log" table.
Table string

// [schema].table
// [schema.]table
tableName string
// to prevent multiple migrations running at the same time
lockNum int64
}

// NewMigrator instantiates new Migrator.
func NewMigrator(conn *pgx.Conn, cfg Config) *Migrator {
if cfg.Schema != "" {
cfg.tableName += cfg.Schema + "."
if cfg.Schema == "" {
cfg.Schema = "public"
}
if cfg.Table == "" {
cfg.Table = "_dbump_log"
}

cfg.tableName += cfg.Table
cfg.tableName = cfg.Schema + "." + cfg.Table
cfg.lockNum = hashTableName(cfg.tableName)

return &Migrator{
Expand All @@ -51,18 +51,28 @@ func NewMigrator(conn *pgx.Conn, cfg Config) *Migrator {

// Init is a method from Migrator interface.
func (pg *Migrator) Init(ctx context.Context) error {
query := fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s;
CREATE TABLE IF NOT EXISTS %s (
var query string
if pg.cfg.Schema != "" {
query = fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s;`, pg.cfg.Schema)
}

query += fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
version BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL
);`, pg.cfg.Schema, pg.cfg.tableName)
);`, pg.cfg.tableName)

_, err := pg.conn.Exec(ctx, query)
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)

// TODO: probably should ignore error for this query
// if pg.cfg.Schema != "" {
// query = fmt.Sprintf(`DROP SCHEMA IF EXISTS %s RESTRICT;`, pg.cfg.Schema)
// }
_, err := pg.conn.Exec(ctx, query)
return err
}
Expand Down
Loading

0 comments on commit 6d16391

Please sign in to comment.