postgres: Support custom schema for schema_migrations table (#167)

Instead of hardcoding `schema_migrations` table to the `public` schema, add support for specifying a schema via the `search_path` URL parameter.

**Backwards compatibility note**: If anyone was using the previously undocumented `search_path` behavior (affecting migrations themselves, but always storing the `schema_migrations` table in `public`), you will need to either prepend `public` to your `search_path`, or migrate your `schema_migrations` table to your primary schema:

```sql
ALTER TABLE public.schema_migrations SET SCHEMA myschema;
```

Closes #110
This commit is contained in:
Adrian Macneil 2020-11-01 13:30:35 +13:00 committed by GitHub
parent d4ecd0b259
commit 55a8065efe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 141 additions and 20 deletions

View file

@ -139,6 +139,17 @@ A `socket` or `host` parameter can be specified to connect through a unix socket
DATABASE_URL="postgres://username:password@/database_name?socket=/var/run/postgresql"
```
A `search_path` parameter can be used to specify the [current schema](https://www.postgresql.org/docs/13/ddl-schemas.html#DDL-SCHEMAS-PATH) while applying migrations, as well as for dbmate's `schema_migrations` table.
If multiple comma-separated schemas are passed, the first will be used for the `schema_migrations` table.
```sh
DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?search_path=myschema"
```
```sh
DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?search_path=myschema,public"
```
**SQLite**
SQLite databases are stored on the filesystem, so you do not need to specify a host. By default, files are relative to the current directory. For example, the following will create a database at `./db/database_name.sqlite3`: