-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
postgres support #360
base: main
Are you sure you want to change the base?
postgres support #360
Conversation
func SQLReplacer(src string) string { | ||
for nParam := 1; strings.Contains(src, "?"); nParam++ { | ||
src = strings.Replace(src, "?", fmt.Sprintf("$%d", nParam), 1) | ||
} | ||
return src | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that the only change in the code compared to the mysql backend or was there more you had to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I can remember it correct, the migration sql files and the SQLReplacer was the only thing i changed in order to get postgres to work. Wanted the query's to remain as same as possible so there would be only copy-paste without much thinking to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are minor changes in backend/postgres/postgres.go file:
"github.com/golang-migrate/migrate/v4/database/postgres"
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=require", host, port, user, password, database)
db, err := sql.Open("postgres", dsn)
db, err := sql.Open("postgres", schemaDsn)
dbi, err := postgres.WithInstance(db, &postgres.Config{})
m, err := migrate.NewWithInstance("iofs", migrations, "postgres", dbi)
opt(options) | ||
} | ||
|
||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=require", host, port, user, password, database) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postgres specific change
Any idea when this may be reviewed? I just stumbled across this and would really like postgres as a backend. |
@@ -0,0 +1,70 @@ | |||
CREATE TABLE IF NOT EXISTS instances ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason NOT EXISTS
is used here? In cases like migration (especially creation) being critical path, this may skip creating a table when one already exists with different schema. Reentrancy here has too much of an edge case.
backend/postgres/postgres.go
Outdated
"github.com/golang-migrate/migrate/v4/database/postgres" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
"github.com/google/uuid" | ||
_ "github.com/lib/pq" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/pq is largely stale at this point, jackc/pgx is actively worked on. it is compatible with database/sql` as well, see https://pkg.go.dev/github.com/jackc/pgx/v5/stdlib
} | ||
defer tx.Rollback() | ||
|
||
row := tx.QueryRowContext(ctx, SQLReplacer("SELECT state FROM instances WHERE instance_id = ? AND execution_id = ? LIMIT 1"), instance.InstanceID, instance.ExecutionID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erma07 I think you are fine to use positional parameters when using pgx, e.g.
row := tx.QueryRowContext(ctx, SQLReplacer("SELECT state FROM instances WHERE instance_id = ? AND execution_id = ? LIMIT 1"), instance.InstanceID, instance.ExecutionID) | |
row := tx.QueryRowContext( | |
ctx, | |
"SELECT state FROM instances WHERE instance_id = $1 AND execution_id = $2 LIMIT 1", | |
instance.InstanceID, | |
instance.ExecutionID, | |
) |
Is this being worked on? |
No description provided.