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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. do_notify boolean DEFAULT FALSE NOT NULL,
  53. CONSTRAINT pk__pod_user_workspace__user_id__workspace_id PRIMARY KEY (user_id , workspace_id ),
  54. CONSTRAINT fk__pod_user_workspace__user_id FOREIGN KEY (user_id)
  55. REFERENCES pod_user (user_id) MATCH SIMPLE
  56. ON UPDATE CASCADE ON DELETE CASCADE,
  57. CONSTRAINT fk__pod_user_workspace__workspace_id FOREIGN KEY (workspace_id)
  58. REFERENCES pod_workspaces (workspace_id) MATCH SIMPLE
  59. ON UPDATE CASCADE ON DELETE CASCADE
  60. )
  61. WITH (
  62. OIDS=FALSE
  63. );
  64. ALTER TABLE pod_user_workspace
  65. OWNER TO poduser;
  66. INSERT INTO pod_user_workspace(user_id, workspace_id, role) SELECT user_id, 1, 8 FROM pod_user;
  67. -- ADD Workspace id to all nodes
  68. ALTER TABLE pod_nodes_history ADD COLUMN workspace_id integer;
  69. ALTER TABLE pod_nodes_history ADD COLUMN is_deleted boolean;
  70. UPDATE pod_nodes_history SET is_deleted=false;
  71. ALTER TABLE pod_nodes_history ALTER COLUMN is_deleted SET NOT NULL;
  72. ALTER TABLE pod_nodes_history ALTER COLUMN is_deleted SET DEFAULT false;
  73. ALTER TABLE pod_nodes_history ADD COLUMN is_archived boolean;
  74. UPDATE pod_nodes_history SET is_archived=false;
  75. ALTER TABLE pod_nodes_history ALTER COLUMN is_archived SET NOT NULL;
  76. ALTER TABLE pod_nodes_history ALTER COLUMN is_archived SET DEFAULT false;
  77. -- Trigger: pod_update_node_tg on pod_nodes
  78. DROP TRIGGER pod_update_node_tg ON pod_nodes;
  79. CREATE TRIGGER pod_update_node_tg
  80. INSTEAD OF UPDATE
  81. ON pod_nodes
  82. FOR EACH ROW
  83. EXECUTE PROCEDURE pod_update_node();
  84. -- View: pod_nodes
  85. -- DROP VIEW pod_nodes;
  86. CREATE OR REPLACE VIEW pod_nodes AS
  87. 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
  88. FROM pod_nodes_history
  89. ORDER BY pod_nodes_history.node_id, pod_nodes_history.updated_at DESC, pod_nodes_history.created_at DESC;
  90. ALTER TABLE pod_nodes
  91. OWNER TO poduser;
  92. -- Rule: pod_insert_new_node ON pod_nodes
  93. -- DROP RULE pod_insert_new_node ON pod_nodes;
  94. CREATE OR REPLACE RULE pod_insert_new_node AS
  95. 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)
  96. 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)
  97. 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;
  98. -- Trigger: pod_update_node_tg on pod_nodes
  99. -- DROP TRIGGER pod_update_node_tg ON pod_nodes;
  100. DROP TRIGGER pod_update_node_tg
  101. CREATE TRIGGER pod_update_node_tg
  102. INSTEAD OF UPDATE
  103. ON pod_nodes
  104. FOR EACH ROW
  105. EXECUTE PROCEDURE pod_update_node();
  106. CREATE OR REPLACE FUNCTION pod_update_node()
  107. RETURNS trigger AS
  108. $BODY$
  109. BEGIN
  110. INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
  111. data_label, data_content, data_datetime, node_status, data_reminder_datetime,
  112. data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
  113. 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);
  114. return new;
  115. END;
  116. $BODY$
  117. LANGUAGE plpgsql VOLATILE
  118. COST 100;
  119. ALTER FUNCTION pod_update_node()
  120. OWNER TO poduser;
  121. UPDATE pod_nodes_history SET workspace_id=1;
  122. -- alter data type to folder for each data containing children
  123. UPDATE pod_nodes_history SET node_type='folder' WHERE node_id IN (
  124. SELECT parent_id FROM pod_nodes WHERE node_type='data' GROUP BY parent_id ORDER BY parent_id)
  125. UPDATE pod_nodes_history SET node_type='folder' WHERE parent_id IS NULL;
  126. -- alter default data nodes to "page nodes"
  127. update pod_nodes_history set node_type='page' where node_type='data'
  128. update pod_nodes_history set node_type='page' where node_type='contact'
  129. update pod_nodes_history set node_type='comment' where node_type='event'
  130. update pod_nodes_history set node_status='open' where node_status in ('new', 'inprogress', 'standby')
  131. update pod_nodes_history set node_status='closed-validated' where node_status in ('done', 'information')
  132. update pod_nodes_history set node_status='closed-validated', is_deleted=true where node_status in ('deleted')
  133. update pod_nodes_history set node_status='closed-validated', is_archived=true where node_status in ('closed')
  134. -- Add column "properties"
  135. -- ALTER TABLE pod_nodes_history DROP COLUMN properties;
  136. ALTER TABLE pod_nodes_history ADD COLUMN properties text;
  137. 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")';
  138. ALTER TABLE pod_nodes_history ADD COLUMN last_action character varying(32);
  139. -- Add the properties column to pod_nodes view
  140. -- DROP VIEW pod_nodes;
  141. CREATE OR REPLACE VIEW pod_nodes AS
  142. 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
  143. FROM pod_nodes_history
  144. ORDER BY pod_nodes_history.node_id, pod_nodes_history.updated_at DESC, pod_nodes_history.created_at DESC;
  145. CREATE OR REPLACE RULE pod_insert_new_node AS
  146. 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)
  147. 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)
  148. 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;
  149. DROP TRIGGER pod_update_node_tg ON pod_nodes;
  150. CREATE TRIGGER pod_update_node_tg
  151. INSTEAD OF UPDATE
  152. ON pod_nodes
  153. FOR EACH ROW
  154. EXECUTE PROCEDURE pod_update_node();
  155. -- DROP FUNCTION pod_update_node();
  156. CREATE OR REPLACE FUNCTION pod_update_node()
  157. RETURNS trigger AS
  158. $BODY$
  159. BEGIN
  160. INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
  161. data_label, data_content, data_datetime, node_status, data_reminder_datetime,
  162. data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
  163. 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);
  164. return new;
  165. END;
  166. $BODY$
  167. LANGUAGE plpgsql VOLATILE
  168. COST 100;
  169. ALTER TABLE pod_group DROP COLUMN personnal_group;
  170. DELETE FROM pod_user_group;
  171. DELETE FROM pod_group;
  172. INSERT INTO pod_group(group_id, group_name, display_name, created) VALUES
  173. (1, 'users', 'Users', NOW()),
  174. (2, 'managers', 'Global Managers', NOW()),
  175. (3, 'administrators', 'Administrators', NOW());
  176. -- Add all users in all group
  177. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 1 FROM pod_user;
  178. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 2 FROM pod_user;
  179. INSERT INTO pod_user_group(user_id, group_id) SELECT user_id, 3 FROM pod_user;