mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-11 23:50:04 +01:00
Postgres: Specify public schema for schema_migrations table (#52)
This commit is contained in:
parent
7603e83f0e
commit
0eabb5dc87
3 changed files with 14 additions and 14 deletions
2
main.go
2
main.go
|
|
@ -17,7 +17,7 @@ func main() {
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ func (drv PostgresDriver) DropDatabase(u *url.URL) error {
|
||||||
func postgresSchemaMigrationsDump(db *sql.DB) ([]byte, error) {
|
func postgresSchemaMigrationsDump(db *sql.DB) ([]byte, error) {
|
||||||
// load applied migrations
|
// load applied migrations
|
||||||
migrations, err := queryColumn(db,
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -127,7 +127,7 @@ func (drv PostgresDriver) DatabaseExists(u *url.URL) (bool, error) {
|
||||||
|
|
||||||
// CreateMigrationsTable creates the schema_migrations table
|
// CreateMigrationsTable creates the schema_migrations table
|
||||||
func (drv PostgresDriver) CreateMigrationsTable(db *sql.DB) error {
|
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)")
|
"(version varchar(255) primary key)")
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
@ -136,7 +136,7 @@ func (drv PostgresDriver) CreateMigrationsTable(db *sql.DB) error {
|
||||||
// SelectMigrations returns a list of applied migrations
|
// SelectMigrations returns a list of applied migrations
|
||||||
// with an optional limit (in descending order)
|
// with an optional limit (in descending order)
|
||||||
func (drv PostgresDriver) SelectMigrations(db *sql.DB, limit int) (map[string]bool, error) {
|
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 {
|
if limit >= 0 {
|
||||||
query = fmt.Sprintf("%s limit %d", query, limit)
|
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
|
// InsertMigration adds a new migration record
|
||||||
func (drv PostgresDriver) InsertMigration(db Transaction, version string) error {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMigration removes a migration record
|
// DeleteMigration removes a migration record
|
||||||
func (drv PostgresDriver) DeleteMigration(db Transaction, version string) error {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -150,15 +150,15 @@ func TestPostgresCreateMigrationsTable(t *testing.T) {
|
||||||
|
|
||||||
// migrations table should not exist
|
// migrations table should not exist
|
||||||
count := 0
|
count := 0
|
||||||
err := db.QueryRow("select count(*) from schema_migrations").Scan(&count)
|
err := db.QueryRow("select count(*) from public.schema_migrations").Scan(&count)
|
||||||
require.Equal(t, "pq: relation \"schema_migrations\" does not exist", err.Error())
|
require.Equal(t, "pq: relation \"public.schema_migrations\" does not exist", err.Error())
|
||||||
|
|
||||||
// create table
|
// create table
|
||||||
err = drv.CreateMigrationsTable(db)
|
err = drv.CreateMigrationsTable(db)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// migrations table should exist
|
// 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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// create table should be idempotent
|
// create table should be idempotent
|
||||||
|
|
@ -174,7 +174,7 @@ func TestPostgresSelectMigrations(t *testing.T) {
|
||||||
err := drv.CreateMigrationsTable(db)
|
err := drv.CreateMigrationsTable(db)
|
||||||
require.NoError(t, err)
|
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')`)
|
values ('abc2'), ('abc1'), ('abc3')`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
@ -201,7 +201,7 @@ func TestPostgresInsertMigration(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
count := 0
|
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.NoError(t, err)
|
||||||
require.Equal(t, 0, count)
|
require.Equal(t, 0, count)
|
||||||
|
|
||||||
|
|
@ -209,7 +209,7 @@ func TestPostgresInsertMigration(t *testing.T) {
|
||||||
err = drv.InsertMigration(db, "abc1")
|
err = drv.InsertMigration(db, "abc1")
|
||||||
require.NoError(t, err)
|
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)
|
Scan(&count)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 1, count)
|
require.Equal(t, 1, count)
|
||||||
|
|
@ -223,7 +223,7 @@ func TestPostgresDeleteMigration(t *testing.T) {
|
||||||
err := drv.CreateMigrationsTable(db)
|
err := drv.CreateMigrationsTable(db)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = db.Exec(`insert into schema_migrations (version)
|
_, err = db.Exec(`insert into public.schema_migrations (version)
|
||||||
values ('abc1'), ('abc2')`)
|
values ('abc1'), ('abc2')`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
@ -231,7 +231,7 @@ func TestPostgresDeleteMigration(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
count := 0
|
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.NoError(t, err)
|
||||||
require.Equal(t, 1, count)
|
require.Equal(t, 1, count)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue