Files
mostlymatter/db/migrations/postgres/000066_upgrade_posts_v6.0.up.sql
Ibrahim Serdar Acikgoz 56c7da5817 [MM-40952] migrations: Add if statements for some queries (#19359)
* migrations: add if statements for some queries

* add better checks to upgrade posts v6 migration

* use "is null" instead of "= null"

* fix down script for users v6

* reflect review comments
2022-01-19 10:57:28 +03:00

36 строки
1.2 KiB
SQL

DO $$
<<migrate_root_id>>
DECLARE
parentid_exist boolean := false;
alter_fileids boolean := false;
alter_props boolean := false;
BEGIN
SELECT count(*) != 0 INTO parentid_exist
FROM information_schema.columns
WHERE table_name = 'posts'
AND column_name = 'parentid';
SELECT count(*) != 0 INTO alter_fileids
FROM information_schema.columns
WHERE table_name = 'posts'
AND column_name = 'fileids'
AND data_type = 'character varying'
AND character_maximum_length != 300;
SELECT count(*) != 0 INTO alter_props
FROM information_schema.columns
WHERE table_name = 'posts'
AND column_name = 'props'
AND data_type != 'jsonb';
IF alter_fileids OR alter_props THEN
IF parentid_exist THEN
UPDATE posts SET rootid = parentid WHERE rootid = '' AND rootid != parentid;
ALTER TABLE posts ALTER COLUMN fileids TYPE varchar(300), ALTER COLUMN props TYPE jsonb USING props::jsonb, DROP COLUMN ParentId;
ELSE
ALTER TABLE posts ALTER COLUMN fileids TYPE varchar(300), ALTER COLUMN props TYPE jsonb USING props::jsonb;
END IF;
END IF;
END migrate_root_id $$;
CREATE INDEX IF NOT EXISTS idx_posts_root_id_delete_at ON posts(rootid, deleteat);
DROP INDEX IF EXISTS idx_posts_root_id;