From 0eabb5dc87d6a3ecd6f3836773ed6777f81a7e55 Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Mon, 3 Sep 2018 21:33:19 -0700 Subject: [PATCH] Postgres: Specify public schema for schema_migrations table (#52) --- main.go | 2 +- pkg/dbmate/postgres.go | 10 +++++----- pkg/dbmate/postgres_test.go | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index a1ef072..012f6f1 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ func main() { app := NewApp() err := app.Run(os.Args) if err != nil { - fmt.Fprintf(os.Stderr, "Error: %s\n", err) + _, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err) os.Exit(1) } } diff --git a/pkg/dbmate/postgres.go b/pkg/dbmate/postgres.go index f1c71db..511688d 100644 --- a/pkg/dbmate/postgres.go +++ b/pkg/dbmate/postgres.go @@ -69,7 +69,7 @@ func (drv PostgresDriver) DropDatabase(u *url.URL) error { func postgresSchemaMigrationsDump(db *sql.DB) ([]byte, error) { // load applied migrations migrations, err := queryColumn(db, - "select quote_literal(version) from schema_migrations order by version asc") + "select quote_literal(version) from public.schema_migrations order by version asc") if err != nil { return nil, err } @@ -127,7 +127,7 @@ func (drv PostgresDriver) DatabaseExists(u *url.URL) (bool, error) { // CreateMigrationsTable creates the schema_migrations table func (drv PostgresDriver) CreateMigrationsTable(db *sql.DB) error { - _, err := db.Exec("create table if not exists schema_migrations " + + _, err := db.Exec("create table if not exists public.schema_migrations " + "(version varchar(255) primary key)") return err @@ -136,7 +136,7 @@ func (drv PostgresDriver) CreateMigrationsTable(db *sql.DB) error { // SelectMigrations returns a list of applied migrations // with an optional limit (in descending order) func (drv PostgresDriver) SelectMigrations(db *sql.DB, limit int) (map[string]bool, error) { - query := "select version from schema_migrations order by version desc" + query := "select version from public.schema_migrations order by version desc" if limit >= 0 { query = fmt.Sprintf("%s limit %d", query, limit) } @@ -162,14 +162,14 @@ func (drv PostgresDriver) SelectMigrations(db *sql.DB, limit int) (map[string]bo // InsertMigration adds a new migration record func (drv PostgresDriver) InsertMigration(db Transaction, version string) error { - _, err := db.Exec("insert into schema_migrations (version) values ($1)", version) + _, err := db.Exec("insert into public.schema_migrations (version) values ($1)", version) return err } // DeleteMigration removes a migration record func (drv PostgresDriver) DeleteMigration(db Transaction, version string) error { - _, err := db.Exec("delete from schema_migrations where version = $1", version) + _, err := db.Exec("delete from public.schema_migrations where version = $1", version) return err } diff --git a/pkg/dbmate/postgres_test.go b/pkg/dbmate/postgres_test.go index 2053df8..511bd89 100644 --- a/pkg/dbmate/postgres_test.go +++ b/pkg/dbmate/postgres_test.go @@ -150,15 +150,15 @@ func TestPostgresCreateMigrationsTable(t *testing.T) { // migrations table should not exist count := 0 - err := db.QueryRow("select count(*) from schema_migrations").Scan(&count) - require.Equal(t, "pq: relation \"schema_migrations\" does not exist", err.Error()) + err := db.QueryRow("select count(*) from public.schema_migrations").Scan(&count) + require.Equal(t, "pq: relation \"public.schema_migrations\" does not exist", err.Error()) // create table err = drv.CreateMigrationsTable(db) require.NoError(t, err) // migrations table should exist - err = db.QueryRow("select count(*) from schema_migrations").Scan(&count) + err = db.QueryRow("select count(*) from public.schema_migrations").Scan(&count) require.NoError(t, err) // create table should be idempotent @@ -174,7 +174,7 @@ func TestPostgresSelectMigrations(t *testing.T) { err := drv.CreateMigrationsTable(db) require.NoError(t, err) - _, err = db.Exec(`insert into schema_migrations (version) + _, err = db.Exec(`insert into public.schema_migrations (version) values ('abc2'), ('abc1'), ('abc3')`) require.NoError(t, err) @@ -201,7 +201,7 @@ func TestPostgresInsertMigration(t *testing.T) { require.NoError(t, err) count := 0 - err = db.QueryRow("select count(*) from schema_migrations").Scan(&count) + err = db.QueryRow("select count(*) from public.schema_migrations").Scan(&count) require.NoError(t, err) require.Equal(t, 0, count) @@ -209,7 +209,7 @@ func TestPostgresInsertMigration(t *testing.T) { err = drv.InsertMigration(db, "abc1") require.NoError(t, err) - err = db.QueryRow("select count(*) from schema_migrations where version = 'abc1'"). + err = db.QueryRow("select count(*) from public.schema_migrations where version = 'abc1'"). Scan(&count) require.NoError(t, err) require.Equal(t, 1, count) @@ -223,7 +223,7 @@ func TestPostgresDeleteMigration(t *testing.T) { err := drv.CreateMigrationsTable(db) require.NoError(t, err) - _, err = db.Exec(`insert into schema_migrations (version) + _, err = db.Exec(`insert into public.schema_migrations (version) values ('abc1'), ('abc2')`) require.NoError(t, err) @@ -231,7 +231,7 @@ func TestPostgresDeleteMigration(t *testing.T) { require.NoError(t, err) count := 0 - err = db.QueryRow("select count(*) from schema_migrations").Scan(&count) + err = db.QueryRow("select count(*) from public.schema_migrations").Scan(&count) require.NoError(t, err) require.Equal(t, 1, count) }