Skip to content

Commit 2f0c075

Browse files
authored
Add pgx driver package (#517)
* replace lib/pq dependency with pgx in postgres driver * fix pgx error handling pgconn is a low-level library, and errors from queries flow through higher-level pgx library * update postgres tests to use pgx driver and error reporting * update quoteIdentifier implementation to match lib/pq * move changes for pgx support into new driver package * add pgx driver to CLI * restore examples used in testing for pgx driver * replace postgres driver name with pgx in DSN in tests and docs * consolidate connection string formatting in pgx testing * lock pgx package at v4 Prior to v4, pgx did not support go modules. Version locking the import is the safest way to ensure we get the (as of now) latest version of the module. Additionally v4 implements some breaking changes to connection and error handling, which are also addressed in this commit. * update parsed URL scheme instead of using string operation * add pgx for builds using make * add pgx to top level docs * remove autogenerated TOC
1 parent 7dc03e3 commit 2f0c075

23 files changed

+1136
-5
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ e.g. s3, github, go-bindata, gcs, file
2525
Obtained by running: `migrate -help`
2626

2727
**Loaded Database Drivers**
28-
e.g. spanner, stub, clickhouse, cockroachdb, crdb-postgres, postgres, postgresql, redshift, cassandra, cockroach, mysql
28+
e.g. spanner, stub, clickhouse, cockroachdb, crdb-postgres, postgres, postgresql, pgx, redshift, cassandra, cockroach, mysql
2929
Obtained by running: `migrate -help`
3030

3131
**Go Version**

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab
2-
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb clickhouse mongodb sqlserver firebird neo4j
2+
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb clickhouse mongodb sqlserver firebird neo4j pgx
33
DATABASE_TEST ?= $(DATABASE) sqlite sqlcipher
44
VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-)
55
TEST_FLAGS ?=

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Forked from [mattes/migrate](https://github.com/mattes/migrate)
2424
Database drivers run migrations. [Add a new database?](database/driver.go)
2525

2626
* [PostgreSQL](database/postgres)
27+
* [PGX](database/pgx)
2728
* [Redshift](database/redshift)
2829
* [Ql](database/ql)
2930
* [Cassandra](database/cassandra)

database/pgx/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# pgx
2+
3+
`pgx://user:password@host:port/dbname?query`
4+
5+
| URL Query | WithInstance Config | Description |
6+
|------------|---------------------|-------------|
7+
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
8+
| `x-statement-timeout` | `StatementTimeout` | Abort any statement that takes more than the specified number of milliseconds |
9+
| `x-multi-statement` | `MultiStatementEnabled` | Enable multi-statement execution (default: false) |
10+
| `x-multi-statement-max-size` | `MultiStatementMaxSize` | Maximum size of single statement in bytes (default: 10MB) |
11+
| `dbname` | `DatabaseName` | The name of the database to connect to |
12+
| `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. |
13+
| `user` | | The user to sign in as |
14+
| `password` | | The user's password |
15+
| `host` | | The host to connect to. Values that start with / are for unix domain sockets. (default is localhost) |
16+
| `port` | | The port to bind to. (default is 5432) |
17+
| `fallback_application_name` | | An application_name to fall back to if one isn't provided. |
18+
| `connect_timeout` | | Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely. |
19+
| `sslcert` | | Cert file location. The file must contain PEM encoded data. |
20+
| `sslkey` | | Key file location. The file must contain PEM encoded data. |
21+
| `sslrootcert` | | The location of the root certificate file. The file must contain PEM encoded data. |
22+
| `sslmode` | | Whether or not to use SSL (disable\|require\|verify-ca\|verify-full) |
23+
24+
25+
## Upgrading from v1
26+
27+
1. Write down the current migration version from schema_migrations
28+
1. `DROP TABLE schema_migrations`
29+
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.
30+
3. Download and install the latest migrate version.
31+
4. Force the current migration version with `migrate force <current_version>`.
32+
33+
## Multi-statement mode
34+
35+
In PostgreSQL running multiple SQL statements in one `Exec` executes them inside a transaction. Sometimes this
36+
behavior is not desirable because some statements can be only run outside of transaction (e.g.
37+
`CREATE INDEX CONCURRENTLY`). If you want to use `CREATE INDEX CONCURRENTLY` without activating multi-statement mode
38+
you have to put such statements in a separate migration files.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE users (
2+
user_id integer unique,
3+
name varchar(40),
4+
email varchar(40)
5+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE users DROP COLUMN IF EXISTS city;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE users ADD COLUMN city varchar(100);
2+
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX IF EXISTS users_email_index;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE UNIQUE INDEX CONCURRENTLY users_email_index ON users (email);
2+
3+
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS books;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE books (
2+
user_id integer,
3+
name varchar(40),
4+
author varchar(40)
5+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS movies;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE movies (
2+
user_id integer,
3+
name varchar(40),
4+
director varchar(40)
5+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed interdum velit, tristique iaculis justo. Pellentesque ut porttitor dolor. Donec sit amet pharetra elit. Cras vel ligula ex. Phasellus posuere.

0 commit comments

Comments
 (0)