diff --git a/server/channels/db/migrations/migrations.list b/server/channels/db/migrations/migrations.list index 04a6cc41a6..e7dc749918 100644 --- a/server/channels/db/migrations/migrations.list +++ b/server/channels/db/migrations/migrations.list @@ -269,6 +269,8 @@ channels/db/migrations/mysql/000135_sidebarchannels_categoryid.down.sql channels/db/migrations/mysql/000135_sidebarchannels_categoryid.up.sql channels/db/migrations/mysql/000136_create_attribute_view.down.sql channels/db/migrations/mysql/000136_create_attribute_view.up.sql +channels/db/migrations/mysql/000137_update_attribute_view.down.sql +channels/db/migrations/mysql/000137_update_attribute_view.up.sql channels/db/migrations/postgres/000001_create_teams.down.sql channels/db/migrations/postgres/000001_create_teams.up.sql channels/db/migrations/postgres/000002_create_team_members.down.sql @@ -539,3 +541,5 @@ channels/db/migrations/postgres/000135_sidebarchannels_categoryid.down.sql channels/db/migrations/postgres/000135_sidebarchannels_categoryid.up.sql channels/db/migrations/postgres/000136_create_attribute_view.down.sql channels/db/migrations/postgres/000136_create_attribute_view.up.sql +channels/db/migrations/postgres/000137_update_attribute_view.down.sql +channels/db/migrations/postgres/000137_update_attribute_view.up.sql diff --git a/server/channels/db/migrations/mysql/000137_update_attribute_view.down.sql b/server/channels/db/migrations/mysql/000137_update_attribute_view.down.sql new file mode 100644 index 0000000000..edb1f7ee2a --- /dev/null +++ b/server/channels/db/migrations/mysql/000137_update_attribute_view.down.sql @@ -0,0 +1 @@ +-- This is no-op migration for MySQL. diff --git a/server/channels/db/migrations/mysql/000137_update_attribute_view.up.sql b/server/channels/db/migrations/mysql/000137_update_attribute_view.up.sql new file mode 100644 index 0000000000..edb1f7ee2a --- /dev/null +++ b/server/channels/db/migrations/mysql/000137_update_attribute_view.up.sql @@ -0,0 +1 @@ +-- This is no-op migration for MySQL. diff --git a/server/channels/db/migrations/postgres/000136_create_attribute_view.up.sql b/server/channels/db/migrations/postgres/000136_create_attribute_view.up.sql index 06a4ba2954..a1aa30154c 100644 --- a/server/channels/db/migrations/postgres/000136_create_attribute_view.up.sql +++ b/server/channels/db/migrations/postgres/000136_create_attribute_view.up.sql @@ -1,37 +1,2 @@ -CREATE OR REPLACE PROCEDURE create_attribute_view() -LANGUAGE plpgsql -AS $$ -BEGIN - EXECUTE ' - CREATE MATERIALIZED VIEW IF NOT EXISTS AttributeView AS - SELECT - pv.GroupID, - pv.TargetID, - pv.TargetType, - jsonb_object_agg( - pf.Name, - CASE - WHEN pf.Type = ''select'' THEN ( - SELECT to_jsonb(options.name) - FROM jsonb_to_recordset(pf.Attrs->''options'') AS options(id text, name text) - WHERE options.id = pv.Value #>> ''{}'' - LIMIT 1 - ) - WHEN pf.Type = ''multiselect'' THEN ( - SELECT jsonb_agg(option_names.name) - FROM jsonb_array_elements_text(pv.Value) AS option_id - JOIN jsonb_to_recordset(pf.Attrs->''options'') AS option_names(id text, name text) - ON option_id = option_names.id - ) - ELSE pv.Value - END - ) AS Attributes FROM PropertyValues pv - LEFT JOIN PropertyFields pf ON pf.ID = pv.FieldID - WHERE pv.DeleteAt = 0 OR pv.DeleteAt IS NULL - GROUP BY pv.GroupID, pv.TargetID, pv.TargetType - '; -END; -$$; - -call create_attribute_view(); -DROP PROCEDURE create_attribute_view(); +-- This migration is converted to be no-op due to an error in the original migration. +-- The new migration for this change is 000137_update_attribute_view.up.sql. diff --git a/server/channels/db/migrations/postgres/000137_update_attribute_view.down.sql b/server/channels/db/migrations/postgres/000137_update_attribute_view.down.sql new file mode 100644 index 0000000000..ce537c5b70 --- /dev/null +++ b/server/channels/db/migrations/postgres/000137_update_attribute_view.down.sql @@ -0,0 +1 @@ +DROP MATERIALIZED VIEW IF EXISTS AttributeView; diff --git a/server/channels/db/migrations/postgres/000137_update_attribute_view.up.sql b/server/channels/db/migrations/postgres/000137_update_attribute_view.up.sql new file mode 100644 index 0000000000..be9771573c --- /dev/null +++ b/server/channels/db/migrations/postgres/000137_update_attribute_view.up.sql @@ -0,0 +1,39 @@ +DROP MATERIALIZED VIEW IF EXISTS AttributeView; + +CREATE OR REPLACE PROCEDURE create_attribute_view() +LANGUAGE plpgsql +AS $$ +BEGIN + EXECUTE ' + CREATE MATERIALIZED VIEW IF NOT EXISTS AttributeView AS + SELECT + pv.GroupID, + pv.TargetID, + pv.TargetType, + jsonb_object_agg( + pf.Name, + CASE + WHEN pf.Type = ''select'' THEN ( + SELECT to_jsonb(options.name) + FROM jsonb_to_recordset(pf.Attrs->''options'') AS options(id text, name text) + WHERE options.id = pv.Value #>> ''{}'' + LIMIT 1 + ) + WHEN pf.Type = ''multiselect'' AND jsonb_typeof(pv.Value) = ''array'' THEN ( + SELECT jsonb_agg(option_names.name) + FROM jsonb_array_elements_text(pv.Value) AS option_id + JOIN jsonb_to_recordset(pf.Attrs->''options'') AS option_names(id text, name text) + ON option_id = option_names.id + ) + ELSE pv.Value + END + ) AS Attributes FROM PropertyValues pv + LEFT JOIN PropertyFields pf ON pf.ID = pv.FieldID + WHERE (pv.DeleteAt = 0 OR pv.DeleteAt IS NULL) AND (pf.DeleteAt = 0 OR pf.DeleteAt IS NULL) + GROUP BY pv.GroupID, pv.TargetID, pv.TargetType + '; +END; +$$; + +call create_attribute_view(); +DROP PROCEDURE create_attribute_view();