diff --git a/resetdb.sh b/resetdb.sh index c81a11a5..a79775a4 100755 --- a/resetdb.sh +++ b/resetdb.sh @@ -9,8 +9,8 @@ set -eou pipefail # TODO(opensource): We should adapt Asaf's seedfile command and then delete this. THIS_PATH=$(pwd) -#BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' -BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta' +BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' +# BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta' cd $BETA_PATH docker-compose down -v @@ -22,5 +22,5 @@ cd $THIS_PATH go run src/main.go migrate 2021-03-10T05:16:21Z cd $BETA_PATH -#./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-26 -./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-25 +./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-26 +# ./scripts/db_import -d -n hmn_two -a ./dbdumps/hmn_pg_dump_2021-04-25 diff --git a/src/migration/migrations/2021-09-05T223354Z_PublicInFunctions.go b/src/migration/migrations/2021-09-05T223354Z_PublicInFunctions.go new file mode 100644 index 00000000..561898e6 --- /dev/null +++ b/src/migration/migrations/2021-09-05T223354Z_PublicInFunctions.go @@ -0,0 +1,88 @@ +package migrations + +import ( + "context" + "time" + + "git.handmade.network/hmn/hmn/src/migration/types" + "git.handmade.network/hmn/hmn/src/oops" + "github.com/jackc/pgx/v4" +) + +func init() { + registerMigration(PublicInFunctions{}) +} + +type PublicInFunctions struct{} + +func (m PublicInFunctions) Version() types.MigrationVersion { + return types.MigrationVersion(time.Date(2021, 9, 5, 22, 33, 54, 0, time.UTC)) +} + +func (m PublicInFunctions) Name() string { + return "PublicInFunctions" +} + +func (m PublicInFunctions) Description() string { + return "Make sure to put the schema in front of everything inside our postgres functions" +} + +func (m PublicInFunctions) Up(ctx context.Context, tx pgx.Tx) error { + /* + This is due to a really stupid behavior of Postgres where, when restoring + data, it apparently forgets that `tablename` is generally equivalent to + `public.tablename`. This makes it totally impossible for us to actually + restore our database backups, since these functions fail when adding new + rows. + + https://www.postgresql.org/message-id/20180825020418.GA7869%40momjian.us + */ + + _, err := tx.Exec(ctx, ` + CREATE OR REPLACE FUNCTION thread_type_for_post(int) RETURNS int AS $$ + SELECT thread.type + FROM + public.handmade_post AS post + JOIN public.handmade_thread AS thread ON post.thread_id = thread.id + WHERE post.id = $1 + $$ LANGUAGE SQL; + + CREATE OR REPLACE FUNCTION project_id_for_post(int) RETURNS int AS $$ + SELECT thread.project_id + FROM + public.handmade_post AS post + JOIN public.handmade_thread AS thread ON post.thread_id = thread.id + WHERE post.id = $1 + $$ LANGUAGE SQL; + `) + if err != nil { + return oops.New(err, "failed to add post constraints") + } + + return nil +} + +func (m PublicInFunctions) Down(ctx context.Context, tx pgx.Tx) error { + _, err := tx.Exec(ctx, ` + CREATE OR REPLACE FUNCTION thread_type_for_post(int) RETURNS int AS $$ + SELECT thread.type + FROM + handmade_post AS post + JOIN handmade_thread AS thread ON post.thread_id = thread.id + WHERE post.id = $1 + $$ LANGUAGE SQL; + + CREATE OR REPLACE FUNCTION project_id_for_post(int) RETURNS int AS $$ + SELECT thread.project_id + FROM + handmade_post AS post + JOIN handmade_thread AS thread ON post.thread_id = thread.id + WHERE post.id = $1 + $$ LANGUAGE SQL; + `) + if err != nil { + return oops.New(err, "failed to add post constraints") + } + + return nil +}