Prefix things with "public." inside Postgres functions

Boy is this a stupid behavior of Postgres, and one that only really
manifests when restoring data from a backup.
This commit is contained in:
Ben Visness 2021-09-05 17:42:39 -05:00
parent 0f19cc612b
commit 59f5243839
2 changed files with 92 additions and 4 deletions

View File

@ -9,8 +9,8 @@ set -eou pipefail
# TODO(opensource): We should adapt Asaf's seedfile command and then delete this. # TODO(opensource): We should adapt Asaf's seedfile command and then delete this.
THIS_PATH=$(pwd) THIS_PATH=$(pwd)
#BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta' BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta'
BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta' # BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta'
cd $BETA_PATH cd $BETA_PATH
docker-compose down -v docker-compose down -v
@ -22,5 +22,5 @@ cd $THIS_PATH
go run src/main.go migrate 2021-03-10T05:16:21Z go run src/main.go migrate 2021-03-10T05:16:21Z
cd $BETA_PATH 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-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-25

View File

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