[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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
509f49c813
Коммит
56c7da5817
@@ -14,18 +14,21 @@ CREATE TABLE IF NOT EXISTS roles (
|
||||
ALTER TABLE roles ADD COLUMN IF NOT EXISTS builtin boolean;
|
||||
|
||||
DO $$
|
||||
<< migrate_roles >>
|
||||
<< migrate_if_version_below_500 >>
|
||||
DECLARE
|
||||
current_db_version VARCHAR(100) := '';
|
||||
BEGIN
|
||||
IF((
|
||||
SELECT
|
||||
value
|
||||
FROM systems
|
||||
SELECT
|
||||
value INTO current_db_version
|
||||
FROM
|
||||
systems
|
||||
WHERE
|
||||
Name = 'Version') = '4.10.0') THEN
|
||||
UPDATE Roles SET SchemeManaged = false
|
||||
name = 'Version';
|
||||
IF (string_to_array(current_db_version, '.') < string_to_array('5.0.0', '.')) THEN
|
||||
UPDATE Roles SET SchemeManaged = false
|
||||
WHERE Name NOT IN ('system_user', 'system_admin', 'team_user', 'team_admin', 'channel_user', 'channel_admin');
|
||||
END IF;
|
||||
END migrate_roles
|
||||
END migrate_if_version_below_500
|
||||
$$;
|
||||
|
||||
ALTER TABLE roles ALTER COLUMN permissions TYPE TEXT;
|
||||
|
||||
@@ -19,4 +19,19 @@ CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions (expiresat);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_create_at ON sessions (createat);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_last_activity_at ON sessions (lastactivityat);
|
||||
|
||||
DELETE FROM sessions where expiresat > 3000000000000;
|
||||
DO $$
|
||||
<< migrate_if_version_below_5120 >>
|
||||
DECLARE
|
||||
current_db_version VARCHAR(100) := '';
|
||||
BEGIN
|
||||
SELECT
|
||||
value INTO current_db_version
|
||||
FROM
|
||||
systems
|
||||
WHERE
|
||||
name = 'Version';
|
||||
IF (string_to_array(current_db_version, '.') < string_to_array('5.12.0', '.')) THEN
|
||||
DELETE FROM sessions where expiresat > 3000000000000;
|
||||
END IF;
|
||||
END migrate_if_version_below_5120
|
||||
$$;
|
||||
|
||||
@@ -18,15 +18,18 @@ ALTER TABLE uploadsessions ADD COLUMN IF NOT EXISTS remoteid VARCHAR(26);
|
||||
ALTER TABLE uploadsessions ADD COLUMN IF NOT EXISTS reqfileid VARCHAR(26);
|
||||
|
||||
DO $$
|
||||
<< shared_channel_support >>
|
||||
<< migrate_if_version_below_5350 >>
|
||||
DECLARE
|
||||
current_db_version VARCHAR(100) := '';
|
||||
BEGIN
|
||||
IF((
|
||||
SELECT
|
||||
value
|
||||
FROM systems
|
||||
SELECT
|
||||
value INTO current_db_version
|
||||
FROM
|
||||
systems
|
||||
WHERE
|
||||
Name = 'Version') = '5.34.0') THEN
|
||||
UPDATE UploadSessions SET RemoteId='', ReqFileId='' WHERE RemoteId IS NULL;
|
||||
name = 'Version';
|
||||
IF (string_to_array(current_db_version, '.') < string_to_array('5.35.0', '.')) THEN
|
||||
UPDATE UploadSessions SET RemoteId='', ReqFileId='' WHERE RemoteId IS NULL;
|
||||
END IF;
|
||||
END shared_channel_support
|
||||
END migrate_if_version_below_5350
|
||||
$$;
|
||||
|
||||
@@ -9,8 +9,27 @@ ALTER TABLE threads ADD COLUMN IF NOT EXISTS channelid VARCHAR(26);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_threads_channel_id ON threads (channelid);
|
||||
|
||||
UPDATE threads
|
||||
SET channelId=posts.channelid
|
||||
FROM posts
|
||||
WHERE posts.id=threads.postid
|
||||
AND threads.channelid IS NULL;
|
||||
DO $$
|
||||
<< migrate_empty_threads >>
|
||||
DECLARE
|
||||
empty_threads_exist boolean := FALSE;
|
||||
BEGIN
|
||||
SELECT
|
||||
count(*) != 0 INTO empty_threads_exist
|
||||
FROM
|
||||
threads
|
||||
WHERE
|
||||
channelid IS NULL;
|
||||
IF empty_threads_exist THEN
|
||||
UPDATE
|
||||
threads
|
||||
SET
|
||||
channelId = posts.channelid
|
||||
FROM
|
||||
posts
|
||||
WHERE
|
||||
posts.id = threads.postid
|
||||
AND threads.channelid IS NULL;
|
||||
END IF;
|
||||
END migrate_empty_threads
|
||||
$$;
|
||||
|
||||
@@ -29,8 +29,37 @@ ALTER TABLE users DROP COLUMN IF EXISTS lastpingat;
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS position VARCHAR(128);
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS timezone VARCHAR(256) DEFAULT '{"automaticTimezone":"","manualTimezone":"","useAutomaticTimezone":"true"}';
|
||||
ALTER TABLE users ALTER COLUMN position TYPE VARCHAR(128);
|
||||
UPDATE Users SET AuthData=LOWER(AuthData) WHERE AuthService = 'saml';
|
||||
ALTER TABLE users ALTER COLUMN roles TYPE VARCHAR(256);
|
||||
|
||||
DO $$
|
||||
<< migrate_if_version_below_4100 >>
|
||||
DECLARE
|
||||
current_db_version VARCHAR(100) := '';
|
||||
BEGIN
|
||||
SELECT
|
||||
value INTO current_db_version
|
||||
FROM
|
||||
systems
|
||||
WHERE
|
||||
name = 'Version';
|
||||
IF (string_to_array(current_db_version, '.') < string_to_array('4.10.0', '.')) THEN
|
||||
UPDATE Users SET AuthData=LOWER(AuthData) WHERE AuthService = 'saml';
|
||||
END IF;
|
||||
END migrate_if_version_below_4100
|
||||
$$;
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
column_exist boolean := false;
|
||||
BEGIN
|
||||
SELECT count(*) != 0 INTO column_exist
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'users'
|
||||
AND column_name = 'roles'
|
||||
AND NOT data_type = 'varchar(256)';
|
||||
IF column_exist THEN
|
||||
ALTER TABLE users ALTER COLUMN roles TYPE varchar(256);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_update_at ON users (updateat);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_create_at ON users (createat);
|
||||
|
||||
@@ -18,7 +18,7 @@ SELECT count(*) != 0 INTO msg_count_root_exist
|
||||
AND column_name = 'mentioncountroot';
|
||||
|
||||
IF mention_count_root_exist THEN
|
||||
tmp_count_root := (SELECT count(*) FROM channelmembers WHERE msgcountroot = NULL OR mentioncountroot = NULL);
|
||||
tmp_count_root := (SELECT count(*) FROM channelmembers WHERE msgcountroot IS NULL OR mentioncountroot IS NULL);
|
||||
END IF;
|
||||
|
||||
ALTER TABLE channelmembers ADD COLUMN IF NOT EXISTS mentioncountroot bigint;
|
||||
|
||||
@@ -2,17 +2,31 @@ 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';
|
||||
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;
|
||||
|
||||
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 $$;
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user