Skip to content

Commit

Permalink
Add instructions for migrations work-around
Browse files Browse the repository at this point in the history
Creating initial migrations that don't create new SQL tables seems to
fail silently for some reason. This commit adds some information about a
work-around to the README and example files.
  • Loading branch information
kohrVid committed May 15, 2022
1 parent 5da4c86 commit 6d57856
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ format:
VERSION_migration_name.up.sql
VERSION_migration_name.down.sql

The first migration file must generate a table (as opposed to an SQL function,
for example) with at least one field. Without this, the migration command will
fail silently. If you would like to create an SQL function in your first
migration an example work-around can be found
[here](https://github.com/kohrVid/pg-cli/blob/master/example/migrations/1_initialise_schema.up.sql).
Further details on how to write migration files can be found
[here](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md).
on the [golang-migrate github repo](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md).

Apply all up migrations:

Expand Down
2 changes: 1 addition & 1 deletion db/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestMigrateUp(t *testing.T) {
t.Fatal(err)
}

checkDBVersion(t, conf, "../example/migrations", 1)
checkDBVersion(t, conf, "../example/migrations", 2)

forceDBDrop(conf)
}
Expand Down
2 changes: 2 additions & 0 deletions example/migrations/1_initialise_schema.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP FUNCTION modify_updated_at_column();
DROP TABLE table0;
13 changes: 13 additions & 0 deletions example/migrations/1_initialise_schema.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS table0 (
field VARCHAR(16)
);

CREATE OR REPLACE FUNCTION modify_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';

DROP TABLE table0;
File renamed without changes.
File renamed without changes.

0 comments on commit 6d57856

Please sign in to comment.