Postgres driver not running migrations with the configured schema
Author: dhuiCreated Mar 4, 2020Updated Sep 23, 2026
Describe the Bug It looks like the schema is only used for generating the advisory lock id and for dropping migrations.
Steps to Reproduce See: https://github.com/golang-migrate/migrate/issues/262#issuecomment-594076769
Expected Behavior
Migrations should not need to run SET SCHEMA. Instead, the Postgres db driver should handle that for you
Migrate Version v4.9.1
Loaded Source Drivers N/A
Loaded Database Drivers N/A
Go Version 1.14
Stacktrace N/A
Additional context N/A
Potential Solutions
- I think we can add a method (
ensureUsingSchema()) to the postgres in a backwards compatible manner. Not sure if we should runensureUsingSchema()before every migration since that may break existing migrations, we could run it when the connection opens. That way any existSET SCHEMAcall is honored and subsequent migrations would continue to use the set schema.
Testcases Any fix should test the following cases:
| Where schema specified: | In config | Not in config |
|---|---|---|
| In migration | ||
| Not in migration |
Source: golang-migrate/migrate