From 0363357209d82855a3c45507bb0d35e66c4717a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 6 Apr 2022 22:15:50 +0200 Subject: [PATCH] Simplifying the categories migration (#2739) * Simplifying the categories migration * A bit more of simplification * Leaving the channel_id there because sqlite doesn't support drop columns * Update server/services/store/sqlstore/migrations/000018_populate_categories.up.sql * Update server/services/store/sqlstore/migrations/000019_populate_category_blocks.up.sql * Update server/services/store/sqlstore/migrations/000018_populate_categories.up.sql Co-authored-by: Doug Lauder --- .../000018_populate_categories.up.sql | 29 +++++++------------ .../000019_populate_category_blocks.up.sql | 27 ++++++----------- .../migrations/000021_fix_categories.down.sql | 18 ------------ .../migrations/000021_fix_categories.up.sql | 18 ------------ 4 files changed, 20 insertions(+), 72 deletions(-) delete mode 100644 server/services/store/sqlstore/migrations/000021_fix_categories.down.sql delete mode 100644 server/services/store/sqlstore/migrations/000021_fix_categories.up.sql diff --git a/server/services/store/sqlstore/migrations/000018_populate_categories.up.sql b/server/services/store/sqlstore/migrations/000018_populate_categories.up.sql index cc9ea3899..dc5ead83d 100644 --- a/server/services/store/sqlstore/migrations/000018_populate_categories.up.sql +++ b/server/services/store/sqlstore/migrations/000018_populate_categories.up.sql @@ -1,13 +1,9 @@ CREATE TABLE {{.prefix}}categories ( - {{if .mysql}}id INT NOT NULL UNIQUE AUTO_INCREMENT,{{end}} - {{if .postgres}}id SERIAL,{{end}} - {{if .sqlite}}id varchar(36),{{end}} + id varchar(36) NOT NULL, name varchar(100) NOT NULL, user_id varchar(32) NOT NULL, team_id varchar(32) NOT NULL, - {{if not .sqlite}} - channel_id varchar(32) NOT NULL, - {{end}} + channel_id varchar(32), create_at BIGINT, update_at BIGINT, delete_at BIGINT, @@ -16,22 +12,28 @@ CREATE TABLE {{.prefix}}categories ( {{if .plugin}} INSERT INTO {{.prefix}}categories( + id, name, user_id, team_id, - {{if not .sqlite}}channel_id,{{end}} + channel_id, create_at, update_at, delete_at ) SELECT + {{ if .postgres }} + REPLACE(uuid_in(md5(random()::text || clock_timestamp()::text)::cstring)::varchar, '-', ''), + {{ end }} + {{ if .mysql }} + REPLACE(UUID(), '-', ''), + {{ end }} COALESCE(nullif(c.DisplayName, ''), 'Direct Message') as category_name, cm.UserId, COALESCE(nullif(c.TeamId, ''), 'direct_message') as team_id, - {{if not .sqlite}}cm.ChannelId,{{end}} + cm.ChannelId, {{if .postgres}}(extract(epoch from now())*1000)::bigint,{{end}} {{if .mysql}}UNIX_TIMESTAMP() * 1000,{{end}} - {{if .sqlite}}CAST(strftime('%s', 'now') * 1000 as bigint),{{end}} 0, 0 FROM @@ -39,13 +41,4 @@ CREATE TABLE {{.prefix}}categories ( JOIN ChannelMembers cm on boards.channel_id = cm.ChannelId JOIN Channels c on cm.ChannelId = c.id GROUP BY cm.UserId, c.TeamId, cm.ChannelId, c.DisplayName; - - {{if .mysql}} - ALTER TABLE {{.prefix}}categories MODIFY id varchar(36); - {{end}} - - {{if .postgres}} - ALTER TABLE {{.prefix}}categories ALTER COLUMN id TYPE varchar(36); - ALTER TABLE {{.prefix}}categories ALTER COLUMN id DROP DEFAULT; - {{end}} {{end}} diff --git a/server/services/store/sqlstore/migrations/000019_populate_category_blocks.up.sql b/server/services/store/sqlstore/migrations/000019_populate_category_blocks.up.sql index 51b7c495b..709a9407e 100644 --- a/server/services/store/sqlstore/migrations/000019_populate_category_blocks.up.sql +++ b/server/services/store/sqlstore/migrations/000019_populate_category_blocks.up.sql @@ -1,7 +1,5 @@ CREATE TABLE {{.prefix}}category_blocks ( - {{if .mysql}}id INT AUTO_INCREMENT,{{end}} - {{if .postgres}}id SERIAL,{{end}} - {{if .sqlite}}id varchar(36),{{end}} + id varchar(36) NOT NULL, user_id varchar(32) NOT NULL, category_id varchar(36) NOT NULL, block_id VARCHAR(36) NOT NULL, @@ -12,30 +10,23 @@ CREATE TABLE {{.prefix}}category_blocks ( ) {{if .mysql}}DEFAULT CHARACTER SET utf8mb4{{end}}; {{if .plugin}} - INSERT INTO {{.prefix}}category_blocks(user_id, category_id, block_id, create_at, update_at, delete_at) + INSERT INTO {{.prefix}}category_blocks(id, user_id, category_id, block_id, create_at, update_at, delete_at) SELECT + {{ if .postgres }} + REPLACE(uuid_in(md5(random()::text || clock_timestamp()::text)::cstring)::varchar, '-', ''), + {{ end }} + {{ if .mysql }} + REPLACE(UUID(), '-', ''), + {{ end }} {{.prefix}}categories.user_id, {{.prefix}}categories.id, {{.prefix}}boards.id, {{if .postgres}}(extract(epoch from now())*1000)::bigint,{{end}} {{if .mysql}}UNIX_TIMESTAMP() * 1000,{{end}} - {{if .sqlite}}CAST(strftime('%s', 'now') * 1000 as bigint),{{end}} 0, 0 FROM {{.prefix}}categories JOIN {{.prefix}}boards ON {{.prefix}}categories.channel_id = {{.prefix}}boards.channel_id - AND {{.prefix}}boards.is_template = false -; - - ALTER TABLE {{.prefix}}categories DROP COLUMN channel_id; - - {{if .mysql}} - ALTER TABLE {{.prefix}}category_blocks MODIFY id varchar(36); - {{end}} - - {{if .postgres}} - ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id TYPE varchar(36); - ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id DROP DEFAULT; - {{end}} + AND {{.prefix}}boards.is_template = false; {{end}} diff --git a/server/services/store/sqlstore/migrations/000021_fix_categories.down.sql b/server/services/store/sqlstore/migrations/000021_fix_categories.down.sql deleted file mode 100644 index 8d49a03a6..000000000 --- a/server/services/store/sqlstore/migrations/000021_fix_categories.down.sql +++ /dev/null @@ -1,18 +0,0 @@ -{{if .sqlite}} -ALTER TABLE {{.prefix}}categories DROP COLUMN channel_id VARCHAR(36); -{{end}} - -{{if .mysql}} -ALTER TABLE {{.prefix}}categories MODIFY COLUMN channel_id VARCHAR(36) NOT NULL; -ALTER TABLE {{.prefix}}categories MODIFY COLUMN id INT NOT NULL UNIQUE AUTO_INCREMENT; -ALTER TABLE {{.prefix}}category_blocks MODIFY COLUMN id INT NOT NULL UNIQUE AUTO_INCREMENT; -{{end}} - -{{if .postgres}} -ALTER TABLE {{.prefix}}categories ALTER COLUMN id DROP NOT NULL; -ALTER TABLE {{.prefix}}categories ALTER COLUMN id TYPE SERIAL; -ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id DROP NOT NULL; -ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id TYPE SERIAL; -ALTER TABLE {{.prefix}}categories ALTER COLUMN channel_id TYPE VARCHAR(32); -ALTER TABLE {{.prefix}}categories ALTER COLUMN channel_id SET NOT NULL; -{{end}} diff --git a/server/services/store/sqlstore/migrations/000021_fix_categories.up.sql b/server/services/store/sqlstore/migrations/000021_fix_categories.up.sql deleted file mode 100644 index 67e967b0a..000000000 --- a/server/services/store/sqlstore/migrations/000021_fix_categories.up.sql +++ /dev/null @@ -1,18 +0,0 @@ -{{if .sqlite}} -ALTER TABLE {{.prefix}}categories ADD COLUMN channel_id VARCHAR(36); -{{end}} - -{{if .mysql}} -ALTER TABLE {{.prefix}}categories MODIFY COLUMN id VARCHAR(36) NOT NULL; -ALTER TABLE {{.prefix}}category_blocks MODIFY COLUMN id VARCHAR(36) NOT NULL; -ALTER TABLE {{.prefix}}categories MODIFY COLUMN channel_id VARCHAR(36); -{{end}} - -{{if .postgres}} -ALTER TABLE {{.prefix}}categories ALTER COLUMN id TYPE VARCHAR(36); -ALTER TABLE {{.prefix}}categories ALTER COLUMN id SET NOT NULL; -ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id TYPE VARCHAR(36); -ALTER TABLE {{.prefix}}category_blocks ALTER COLUMN id SET NOT NULL; -ALTER TABLE {{.prefix}}categories ALTER COLUMN channel_id TYPE VARCHAR(36); -ALTER TABLE {{.prefix}}categories ALTER COLUMN channel_id DROP NOT NULL; -{{end}}