Skip to content

Commit

Permalink
Add driver for pgx v5 (#848)
Browse files Browse the repository at this point in the history
* Add driver for pgx v5

Fixes #827

- Move existing driver into database/pgx/v4
- Make exported items into database/pgx into links to database/pgx/v4,
  marked as Deprecated
- Remove most tests in database/pgx, leaving a couple of e2e tests
  behind to help ensure backwards compatibility.
- Mark defunct ErrDatabaseDirty as Deprecated - no code returns it
- Bump pgx/v4 dependency
- Add test runs for newer Postgres versions

* Tweak pgx tags and registration strings

* Tweak registration strings
  • Loading branch information
treuherz committed Apr 5, 2023
1 parent c34675f commit b94ed1a
Show file tree
Hide file tree
Showing 11 changed files with 1,343 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@
SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher
VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-)
TEST_FLAGS ?=
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -24,7 +24,8 @@ Forked from [mattes/migrate](https://github.com/mattes/migrate)
Database drivers run migrations. [Add a new database?](database/driver.go)

* [PostgreSQL](database/postgres)
* [PGX](database/pgx)
* [PGX v4](database/pgx)
* [PGX v5](database/pgx/v5)
* [Redshift](database/redshift)
* [Ql](database/ql)
* [Cassandra](database/cassandra)
Expand Down
2 changes: 2 additions & 0 deletions database/pgx/README.md
@@ -1,5 +1,7 @@
# pgx

This package is for [pgx/v4](https://pkg.go.dev/github.com/jackc/pgx/v4). A backend for the newer [pgx/v5](https://pkg.go.dev/github.com/jackc/pgx/v5) is [also available](v5).

`pgx://user:password@host:port/dbname?query`

| URL Query | WithInstance Config | Description |
Expand Down
3 changes: 2 additions & 1 deletion database/pgx/pgx.go
Expand Up @@ -28,6 +28,7 @@ import (
func init() {
db := Postgres{}
database.Register("pgx", &db)
database.Register("pgx4", &db)
}

var (
Expand Down Expand Up @@ -150,7 +151,7 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
// i.e. pgx://user:password@host:port/db => postgres://user:password@host:port/db
purl.Scheme = "postgres"

db, err := sql.Open("pgx", migrate.FilterCustomQuery(purl).String())
db, err := sql.Open("pgx/v4", migrate.FilterCustomQuery(purl).String())
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions database/pgx/pgx_test.go
Expand Up @@ -8,19 +8,17 @@ import (
sqldriver "database/sql/driver"
"errors"
"fmt"
"log"

"io"
"log"
"strconv"
"strings"
"sync"
"testing"

"github.com/golang-migrate/migrate/v4"

"github.com/dhui/dktest"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"

dt "github.com/golang-migrate/migrate/v4/database/testing"
"github.com/golang-migrate/migrate/v4/dktesting"
_ "github.com/golang-migrate/migrate/v4/source/file"
Expand All @@ -41,6 +39,9 @@ var (
{ImageName: "postgres:10", Options: opts},
{ImageName: "postgres:11", Options: opts},
{ImageName: "postgres:12", Options: opts},
{ImageName: "postgres:13", Options: opts},
{ImageName: "postgres:14", Options: opts},
{ImageName: "postgres:15", Options: opts},
}
)

Expand Down
41 changes: 41 additions & 0 deletions database/pgx/v5/README.md
@@ -0,0 +1,41 @@
# pgx

This package is for [pgx/v5](https://pkg.go.dev/github.com/jackc/pgx/v5). A backend for the older [pgx/v4](https://pkg.go.dev/github.com/jackc/pgx/v4). is [also available](..).

`pgx://user:password@host:port/dbname?query`

| URL Query | WithInstance Config | Description |
|------------|---------------------|-------------|
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
| `x-migrations-table-quoted` | `MigrationsTableQuoted` | By default, migrate quotes the migration table for SQL injection safety reasons. This option disable quoting and naively checks that you have quoted the migration table name. e.g. `"my_schema"."schema_migrations"` |
| `x-statement-timeout` | `StatementTimeout` | Abort any statement that takes more than the specified number of milliseconds |
| `x-multi-statement` | `MultiStatementEnabled` | Enable multi-statement execution (default: false) |
| `x-multi-statement-max-size` | `MultiStatementMaxSize` | Maximum size of single statement in bytes (default: 10MB) |
| `dbname` | `DatabaseName` | The name of the database to connect to |
| `search_path` | | This variable specifies the order in which schemas are searched when an object is referenced by a simple name with no schema specified. |
| `user` | | The user to sign in as |
| `password` | | The user's password |
| `host` | | The host to connect to. Values that start with / are for unix domain sockets. (default is localhost) |
| `port` | | The port to bind to. (default is 5432) |
| `fallback_application_name` | | An application_name to fall back to if one isn't provided. |
| `connect_timeout` | | Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely. |
| `sslcert` | | Cert file location. The file must contain PEM encoded data. |
| `sslkey` | | Key file location. The file must contain PEM encoded data. |
| `sslrootcert` | | The location of the root certificate file. The file must contain PEM encoded data. |
| `sslmode` | | Whether or not to use SSL (disable\|require\|verify-ca\|verify-full) |


## Upgrading from v1

1. Write down the current migration version from schema_migrations
1. `DROP TABLE schema_migrations`
2. Wrap your existing migrations in transactions ([BEGIN/COMMIT](https://www.postgresql.org/docs/current/static/transaction-iso.html)) if you use multiple statements within one migration.
3. Download and install the latest migrate version.
4. Force the current migration version with `migrate force <current_version>`.

## Multi-statement mode

In PostgreSQL running multiple SQL statements in one `Exec` executes them inside a transaction. Sometimes this
behavior is not desirable because some statements can be only run outside of transaction (e.g.
`CREATE INDEX CONCURRENTLY`). If you want to use `CREATE INDEX CONCURRENTLY` without activating multi-statement mode
you have to put such statements in a separate migration files.

0 comments on commit b94ed1a

Please sign in to comment.