Postgres: Specify public schema for schema_migrations table (#52)

This commit is contained in:
Adrian Macneil 2018-09-03 21:33:19 -07:00 committed by GitHub
parent 7603e83f0e
commit 0eabb5dc87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 14 deletions

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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)
}