Repository: supabase/supabase

Issue #1416: Improve migration between projects to include super user role update
Created: 2021-05-04T14:00:50Z
Improve documentation
Link
https://supabase.io/docs/guides/database#migrating-between-projects
Describe the problem
It took me quite a while to understand it but when I was doing a migration from a project to another one replication disappeared and even though my data was correctly imported into the new project, all foreign keys weirdly brought bugs.
After a couple of tests, I found out that the problem was that the user postgres was not a super user.
I fixed the issue by temporarily set the postgres user as super user with ALTER USER postgres WITH SUPERUSER;
I could now see way less errors and the import worked correctly.
I switch the user postgres back to not being a super user with ALTER USER postgres WITH NOSUPERUSER;
Describe the improvement
I'm not 100% sure this is the right approach but it worked for me. If it is then perhaps this could be added to the doc. In the doc, you're saying "You may see some errors". But in my case, I saw a lot of errors due to lack of permissions and I thought it was ok.

Duplicate issue #2200: After migration to a new project, querying relationships return error: "Could not find foreign keys between these entities. No relationship found between X and Y."
Created: 2021-07-03T01:57:14Z
Bug report
Describe the bug
Scenario: Using two projects (say: dev and staging). Both hosted by supabase. I'm migrating the schema like the recomendation, realtime disabled.
The issue: on dev environment, where I do everything by sql statements or interface I'm able to query relationships defined by foreign key. E.g.:
const { data, error } = await supabase
    .from<IYEntity>("X")
    .select(`name, uuid, Y (key, is_valid)`)
    .eq("uuid", id)
    .limit(1);
When using the staging project, I get "Could not find foreign keys between these entities. No relationship found between X and Y." while running the same query. I reviewed both DBs and they appear to be identical. I dropped all tables on the staging environment and deleted on procedures. The migration runs, everything appears to be fine, but the query still throws the same error.
To Reproduce

Define a database with foreign key relationships. E.g.:

Migrate the DB to a new project.
Try a query like the one shown.

System information

OS: Linux.
Version of supabase-js: 1.15.1
Version of Node.js: v15.11.0

Additional context
The only think I can think is of restarting the postgrest container, I'd love to be able to do that from the supabase interface or any API. Is there such an option?

Closing comment (2021-07-04T02:40:45Z): Hey @EdgarBarrantes,
Try running this SQL and retry the postgrest-js request:
-- manually refresh postgrest foreign key metadata
NOTIFY ddl_command_end;
It's likely that when you did the migration, the event trigger that automatically reloaded postgrest metadata was lost. This can be checked with:
select evtname, evtevent from pg_event_trigger;
-- should show
-- api_restart ddl_command_end
If the event trigger is not there, you can create it with:
CREATE OR REPLACE FUNCTION extensions.notify_api_restart()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
BEGIN
    NOTIFY ddl_command_end;
END;
$$;
CREATE EVENT TRIGGER api_restart ON ddl_command_end
EXECUTE PROCEDURE extensions.notify_api_restart();
COMMENT ON FUNCTION extensions.notify_api_restart IS 'Sends a notification to the API to restart. If your database schema has changed, this is required so that Supabase can rebuild the relationships.';

Related to #1416 (comment)