mirror of
https://github.com/StanGirard/quivr.git
synced 2024-11-30 21:28:56 +03:00
34 lines
1.2 KiB
PL/PgSQL
34 lines
1.2 KiB
PL/PgSQL
BEGIN;
|
|
|
|
-- Function to check if column exists in a table
|
|
DO $$
|
|
BEGIN
|
|
-- Check if email column doesn't exist, then add it
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'email') THEN
|
|
ALTER TABLE users ADD COLUMN email TEXT;
|
|
END IF;
|
|
|
|
-- Copy user_id to email column only if user_id column exists
|
|
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'user_id') THEN
|
|
UPDATE users SET email = user_id;
|
|
END IF;
|
|
|
|
-- Check if user_id column exists, then drop it
|
|
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'user_id') THEN
|
|
ALTER TABLE users DROP COLUMN user_id;
|
|
END IF;
|
|
|
|
-- Check if new user_id column doesn't exist, then add it
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'user_id') THEN
|
|
ALTER TABLE users ADD COLUMN user_id UUID DEFAULT gen_random_uuid();
|
|
ALTER TABLE users ADD PRIMARY KEY (user_id);
|
|
END IF;
|
|
|
|
EXCEPTION WHEN others THEN
|
|
-- Exception block to catch errors
|
|
RAISE NOTICE 'An error occurred during migration.';
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
COMMIT;
|