Files
KeepNote/database/migrations/002_shared_auth.sql
T

20 lines
745 B
SQL

\c dcntrlized;
-- Plain-UUID user_id: drop the legacy FK (D5).
ALTER TABLE IF EXISTS keepnote.notes DROP CONSTRAINT IF EXISTS notes_user_id_fkey;
-- Re-point existing rows to auth.users by username, then drop the old table.
-- No-op on fresh DBs (the old users table never existed).
DO $$
BEGIN
IF EXISTS (SELECT FROM pg_catalog.pg_tables WHERE schemaname='keepnote' AND tablename='users')
AND EXISTS (SELECT FROM pg_catalog.pg_tables WHERE schemaname='auth' AND tablename='users') THEN
UPDATE keepnote.notes n
SET user_id = a.id
FROM keepnote.users old
JOIN auth.users a ON a.username = old.username
WHERE n.user_id = old.id;
DROP TABLE keepnote.users;
END IF;
END $$;