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 <wiggin77@warpmail.net>
This commit is contained in:
Jesús Espino 2022-04-06 22:15:50 +02:00 committed by GitHub
parent 6fef5f0c92
commit 0363357209
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 72 deletions

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}