pod-migrate-to-first-stable-release.sql 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ALTER TABLE pod_user ADD COLUMN is_active boolean;
  2. UPDATE pod_user SET is_active=true;
  3. ALTER TABLE pod_user ALTER COLUMN is_active SET NOT NULL;
  4. ALTER TABLE pod_user ALTER COLUMN is_active SET DEFAULT true;
  5. -- Table: pod_workspaces
  6. -- DROP TABLE pod_workspaces;
  7. CREATE TABLE pod_workspaces
  8. (
  9. workspace_id integer NOT NULL,
  10. data_label character varying(1024),
  11. data_comment text,
  12. created_at timestamp without time zone,
  13. updated_at timestamp without time zone,
  14. is_deleted boolean NOT NULL DEFAULT false,
  15. CONSTRAINT pk__workspace__workspace_id PRIMARY KEY (workspace_id )
  16. )
  17. WITH (
  18. OIDS=FALSE
  19. );
  20. ALTER TABLE pod_workspaces
  21. OWNER TO poduser;
  22. -- Trigger: pod_workspaces__on_insert_set_created_at on pod_workspaces
  23. -- DROP TRIGGER pod_workspaces__on_insert_set_created_at ON pod_workspaces;
  24. CREATE TRIGGER pod_workspaces__on_insert_set_created_at
  25. BEFORE INSERT
  26. ON pod_workspaces
  27. FOR EACH ROW
  28. EXECUTE PROCEDURE set_created_at();
  29. -- Trigger: pod_workspaces__on_update_set_updated_at on pod_workspaces
  30. -- DROP TRIGGER pod_workspaces__on_update_set_updated_at ON pod_workspaces;
  31. CREATE TRIGGER pod_workspaces__on_update_set_updated_at
  32. BEFORE UPDATE
  33. ON pod_workspaces
  34. FOR EACH ROW
  35. EXECUTE PROCEDURE set_updated_at();
  36. CREATE SEQUENCE pod_workspaces__workspace_id__sequence
  37. INCREMENT 1
  38. MINVALUE 1
  39. MAXVALUE 9223372036854775807
  40. START 11
  41. CACHE 1;
  42. ALTER TABLE pod_workspaces__workspace_id__sequence
  43. OWNER TO poduser;
  44. INSERT INTO pod_workspaces(workspace_id, data_label, data_comment) VALUES (1, 'All (old) content', 'This workspace contain all content which have been created before adding workspace features');
  45. -- Table: pod_user_workspace
  46. -- DROP TABLE pod_user_workspace;
  47. CREATE TABLE pod_user_workspace
  48. (
  49. user_id integer NOT NULL,
  50. workspace_id integer NOT NULL,
  51. role integer,
  52. CONSTRAINT pk__pod_user_workspace__user_id__workspace_id PRIMARY KEY (user_id , workspace_id ),
  53. CONSTRAINT fk__pod_user_workspace__user_id FOREIGN KEY (user_id)
  54. REFERENCES pod_user (user_id) MATCH SIMPLE
  55. ON UPDATE CASCADE ON DELETE CASCADE,
  56. CONSTRAINT fk__pod_user_workspace__workspace_id FOREIGN KEY (workspace_id)
  57. REFERENCES pod_workspaces (workspace_id) MATCH SIMPLE
  58. ON UPDATE CASCADE ON DELETE CASCADE
  59. )
  60. WITH (
  61. OIDS=FALSE
  62. );
  63. ALTER TABLE pod_user_workspace
  64. OWNER TO poduser;
  65. INSERT INTO pod_user_workspace(user_id, workspace_id, role) SELECT user_id, 1, 8 FROM pod_user;
  66. -- ADD Workspace id to all nodes
  67. ALTER TABLE pod_nodes_history ADD COLUMN workspace_id integer;
  68. ALTER TABLE pod_nodes_history ADD COLUMN is_deleted boolean;
  69. UPDATE pod_nodes_history SET is_deleted=false;
  70. ALTER TABLE pod_nodes_history ALTER COLUMN is_deleted SET NOT NULL;
  71. ALTER TABLE pod_nodes_history ALTER COLUMN is_deleted SET DEFAULT false;
  72. ALTER TABLE pod_nodes_history ADD COLUMN is_archived boolean;
  73. UPDATE pod_nodes_history SET is_archived=false;
  74. ALTER TABLE pod_nodes_history ALTER COLUMN is_archived SET NOT NULL;
  75. ALTER TABLE pod_nodes_history ALTER COLUMN is_archived SET DEFAULT false;
  76. -- Trigger: pod_update_node_tg on pod_nodes
  77. DROP TRIGGER pod_update_node_tg ON pod_nodes;
  78. CREATE TRIGGER pod_update_node_tg
  79. INSTEAD OF UPDATE
  80. ON pod_nodes
  81. FOR EACH ROW
  82. EXECUTE PROCEDURE pod_update_node();
  83. -- View: pod_nodes
  84. -- DROP VIEW pod_nodes;
  85. CREATE OR REPLACE VIEW pod_nodes AS
  86. SELECT DISTINCT ON (pod_nodes_history.node_id) pod_nodes_history.node_id, pod_nodes_history.parent_id, pod_nodes_history.node_order, pod_nodes_history.node_type, pod_nodes_history.created_at, pod_nodes_history.updated_at, pod_nodes_history.data_label, pod_nodes_history.data_content, pod_nodes_history.data_datetime, pod_nodes_history.node_status, pod_nodes_history.data_reminder_datetime, pod_nodes_history.data_file_name, pod_nodes_history.data_file_content, pod_nodes_history.data_file_mime_type, pod_nodes_history.parent_tree_path, pod_nodes_history.node_depth, pod_nodes_history.owner_id, pod_nodes_history.is_shared, pod_nodes_history.is_public, pod_nodes_history.public_url_key, pod_nodes_history.workspace_id, pod_nodes_history.is_deleted, pod_nodes_history.is_archived
  87. FROM pod_nodes_history
  88. ORDER BY pod_nodes_history.node_id, pod_nodes_history.updated_at DESC, pod_nodes_history.created_at DESC;
  89. ALTER TABLE pod_nodes
  90. OWNER TO poduser;
  91. -- Rule: pod_insert_new_node ON pod_nodes
  92. -- DROP RULE pod_insert_new_node ON pod_nodes;
  93. CREATE OR REPLACE RULE pod_insert_new_node AS
  94. ON INSERT TO pod_nodes DO INSTEAD INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at, data_label, data_content, data_datetime, node_status, data_reminder_datetime, data_file_name, data_file_content, data_file_mime_type, parent_tree_path, node_depth, owner_id, version_id, is_shared, is_public, public_url_key, workspace_id, is_deleted, is_archived)
  95. VALUES (nextval('pod_nodes__node_id__sequence'::regclass), new.parent_id, new.node_order, new.node_type, new.created_at, new.updated_at, new.data_label, new.data_content, new.data_datetime, new.node_status, new.data_reminder_datetime, new.data_file_name, new.data_file_content, new.data_file_mime_type, new.parent_tree_path, new.node_depth, new.owner_id, nextval('pod_nodes_version_id_sequence'::regclass), new.is_shared, new.is_public, new.public_url_key, new.workspace_id, new.is_deleted, new.is_archived)
  96. RETURNING pod_nodes_history.node_id, pod_nodes_history.parent_id, pod_nodes_history.node_order, pod_nodes_history.node_type, pod_nodes_history.created_at, pod_nodes_history.updated_at, pod_nodes_history.data_label, pod_nodes_history.data_content, pod_nodes_history.data_datetime, pod_nodes_history.node_status, pod_nodes_history.data_reminder_datetime, pod_nodes_history.data_file_name, pod_nodes_history.data_file_content, pod_nodes_history.data_file_mime_type, pod_nodes_history.parent_tree_path, pod_nodes_history.node_depth, pod_nodes_history.owner_id, pod_nodes_history.is_shared, pod_nodes_history.is_public, pod_nodes_history.public_url_key, pod_nodes_history.workspace_id, pod_nodes_history.is_deleted, pod_nodes_history.is_archived;
  97. -- Trigger: pod_update_node_tg on pod_nodes
  98. -- DROP TRIGGER pod_update_node_tg ON pod_nodes;
  99. DROP TRIGGER pod_update_node_tg
  100. CREATE TRIGGER pod_update_node_tg
  101. INSTEAD OF UPDATE
  102. ON pod_nodes
  103. FOR EACH ROW
  104. EXECUTE PROCEDURE pod_update_node();
  105. CREATE OR REPLACE FUNCTION pod_update_node()
  106. RETURNS trigger AS
  107. $BODY$
  108. BEGIN
  109. INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
  110. data_label, data_content, data_datetime, node_status, data_reminder_datetime,
  111. data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
  112. node_depth, owner_id, version_id, is_shared, is_public, public_url_key, workspace_id, is_deleted, is_archived) VALUES (NEW.node_id, NEW.parent_id, NEW.node_order, NEW.node_type, NEW.created_at, NEW.updated_at, NEW.data_label, NEW.data_content, NEW.data_datetime, NEW.node_status, NEW.data_reminder_datetime, NEW.data_file_name, NEW.data_file_content, NEW.data_file_mime_type, NEW.parent_tree_path, NEW.node_depth, NEW.owner_id, nextval('pod_nodes_version_id_sequence'), NEW.is_shared, NEW.is_public, NEW.public_url_key, NEW.workspace_id, NEW.is_deleted, NEW.is_archived);
  113. return new;
  114. END;
  115. $BODY$
  116. LANGUAGE plpgsql VOLATILE
  117. COST 100;
  118. ALTER FUNCTION pod_update_node()
  119. OWNER TO poduser;
  120. UPDATE pod_nodes_history SET workspace_id=1;
  121. -- alter data type to folder for each data containing children
  122. UPDATE pod_nodes_history SET node_type='folder' WHERE node_id IN (
  123. SELECT parent_id FROM pod_nodes WHERE node_type='data' GROUP BY parent_id ORDER BY parent_id)
  124. UPDATE pod_nodes_history SET node_type='folder' WHERE parent_id IS NULL;
  125. -- alter default data nodes to "page nodes"
  126. update pod_nodes_history set node_type='page' where node_type='data'
  127. update pod_nodes_history set node_type='page' where node_type='contact'
  128. update pod_nodes_history set node_type='comment' where node_type='event'
  129. update pod_nodes_history set node_status='open' where node_status in ('new', 'inprogress', 'standby')
  130. update pod_nodes_history set node_status='closed-validated' where node_status in ('done', 'information')
  131. update pod_nodes_history set node_status='closed-validated', is_deleted=true where node_status in ('deleted')
  132. update pod_nodes_history set node_status='closed-validated', is_archived=true where node_status in ('closed')
  133. -- Add column "properties"
  134. -- ALTER TABLE pod_nodes_history DROP COLUMN properties;
  135. ALTER TABLE pod_nodes_history ADD COLUMN properties text;
  136. COMMENT ON COLUMN pod_nodes_history.properties IS 'This column contain properties specific to a given node_type. these properties are json encoded (so there is no structure "a priori")';
  137. ALTER TABLE pod_nodes_history ADD COLUMN last_action character varying(32);
  138. -- Add the properties column to pod_nodes view
  139. -- DROP VIEW pod_nodes;
  140. CREATE OR REPLACE VIEW pod_nodes AS
  141. SELECT DISTINCT ON (pod_nodes_history.node_id) pod_nodes_history.node_id, pod_nodes_history.parent_id, pod_nodes_history.node_order, pod_nodes_history.node_type, pod_nodes_history.created_at, pod_nodes_history.updated_at, pod_nodes_history.data_label, pod_nodes_history.data_content, pod_nodes_history.data_datetime, pod_nodes_history.node_status, pod_nodes_history.data_reminder_datetime, pod_nodes_history.data_file_name, pod_nodes_history.data_file_content, pod_nodes_history.data_file_mime_type, pod_nodes_history.parent_tree_path, pod_nodes_history.node_depth, pod_nodes_history.owner_id, pod_nodes_history.is_shared, pod_nodes_history.is_public, pod_nodes_history.public_url_key, pod_nodes_history.workspace_id, pod_nodes_history.is_deleted, pod_nodes_history.is_archived, pod_nodes_history.properties, pod_nodes_history.last_action
  142. FROM pod_nodes_history
  143. ORDER BY pod_nodes_history.node_id, pod_nodes_history.updated_at DESC, pod_nodes_history.created_at DESC;
  144. CREATE OR REPLACE RULE pod_insert_new_node AS
  145. ON INSERT TO pod_nodes DO INSTEAD INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at, data_label, data_content, data_datetime, node_status, data_reminder_datetime, data_file_name, data_file_content, data_file_mime_type, parent_tree_path, node_depth, owner_id, version_id, is_shared, is_public, public_url_key, workspace_id, is_deleted, is_archived, properties, last_action)
  146. VALUES (nextval('pod_nodes__node_id__sequence'::regclass), new.parent_id, new.node_order, new.node_type, new.created_at, new.updated_at, new.data_label, new.data_content, new.data_datetime, new.node_status, new.data_reminder_datetime, new.data_file_name, new.data_file_content, new.data_file_mime_type, new.parent_tree_path, new.node_depth, new.owner_id, nextval('pod_nodes_version_id_sequence'::regclass), new.is_shared, new.is_public, new.public_url_key, new.workspace_id, new.is_deleted, new.is_archived, new.properties, new.last_action)
  147. RETURNING pod_nodes_history.node_id, pod_nodes_history.parent_id, pod_nodes_history.node_order, pod_nodes_history.node_type, pod_nodes_history.created_at, pod_nodes_history.updated_at, pod_nodes_history.data_label, pod_nodes_history.data_content, pod_nodes_history.data_datetime, pod_nodes_history.node_status, pod_nodes_history.data_reminder_datetime, pod_nodes_history.data_file_name, pod_nodes_history.data_file_content, pod_nodes_history.data_file_mime_type, pod_nodes_history.parent_tree_path, pod_nodes_history.node_depth, pod_nodes_history.owner_id, pod_nodes_history.is_shared, pod_nodes_history.is_public, pod_nodes_history.public_url_key, pod_nodes_history.workspace_id, pod_nodes_history.is_deleted, pod_nodes_history.is_archived, pod_nodes_history.properties, pod_nodes_history.last_action;
  148. -- DROP TRIGGER pod_update_node_tg ON pod_nodes;
  149. CREATE TRIGGER pod_update_node_tg
  150. INSTEAD OF UPDATE
  151. ON pod_nodes
  152. FOR EACH ROW
  153. EXECUTE PROCEDURE pod_update_node();
  154. -- DROP FUNCTION pod_update_node();
  155. CREATE OR REPLACE FUNCTION pod_update_node()
  156. RETURNS trigger AS
  157. $BODY$
  158. BEGIN
  159. INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
  160. data_label, data_content, data_datetime, node_status, data_reminder_datetime,
  161. data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
  162. node_depth, owner_id, version_id, is_shared, is_public, public_url_key, workspace_id, is_deleted, is_archived, properties, last_action) VALUES (NEW.node_id, NEW.parent_id, NEW.node_order, NEW.node_type, NEW.created_at, NEW.updated_at, NEW.data_label, NEW.data_content, NEW.data_datetime, NEW.node_status, NEW.data_reminder_datetime, NEW.data_file_name, NEW.data_file_content, NEW.data_file_mime_type, NEW.parent_tree_path, NEW.node_depth, NEW.owner_id, nextval('pod_nodes_version_id_sequence'), NEW.is_shared, NEW.is_public, NEW.public_url_key, NEW.workspace_id, NEW.is_deleted, NEW.is_archived, NEW.properties, NEW.last_action);
  163. return new;
  164. END;
  165. $BODY$
  166. LANGUAGE plpgsql VOLATILE
  167. COST 100;
  168. ALTER TABLE pod_group DROP COLUMN personnal_group;
  169. DELETE FROM pod_user_group;
  170. DELETE FROM pod_group;
  171. INSERT INTO pod_group(group_id, group_name, display_name, created) VALUES
  172. (1, 'users', 'Users', NOW()),
  173. (2, 'managers', 'Global Managers', NOW()),
  174. (3, 'administrators', 'Administrators', NOW());
  175. -- Add all users in all group
  176. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 1 FROM pod_user;
  177. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 2 FROM pod_user;
  178. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 3 FROM pod_user;