|
@@ -0,0 +1,42 @@
|
|
1
|
+-- move table
|
|
2
|
+ALTER TABLE pod_nodes RENAME TO pod_nodes_history;
|
|
3
|
+
|
|
4
|
+-- add uuid index
|
|
5
|
+-- drop primary key
|
|
6
|
+-- new primary key
|
|
7
|
+CREATE SEQUENCE pod_nodes_version_id_sequence;
|
|
8
|
+ALTER TABLE pod_nodes_history
|
|
9
|
+ DROP CONSTRAINT pod_nodes_pk CASCADE,
|
|
10
|
+ ADD COLUMN version_id INTEGER PRIMARY KEY DEFAULT nextval('pod_nodes_version_id_sequence');
|
|
11
|
+
|
|
12
|
+-- create view
|
|
13
|
+CREATE OR REPLACE VIEW pod_nodes AS
|
|
14
|
+ SELECT DISTINCT ON (node_id) node_id, parent_id, node_order, node_type, created_at, updated_at,
|
|
15
|
+ data_label, data_content, data_datetime, node_status, data_reminder_datetime,
|
|
16
|
+ data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
|
|
17
|
+ node_depth, owner_id
|
|
18
|
+ FROM pod_nodes_history
|
|
19
|
+ ORDER BY node_id, updated_at DESC;
|
|
20
|
+
|
|
21
|
+CREATE OR REPLACE RULE pod_insert_new_node AS ON INSERT
|
|
22
|
+TO pod_nodes
|
|
23
|
+DO INSTEAD INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
|
|
24
|
+ data_label, data_content, data_datetime, node_status, data_reminder_datetime,
|
|
25
|
+ data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
|
|
26
|
+ node_depth, owner_id, version_id) VALUES (nextval('pod_nodes__node_id__sequence'), 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'))
|
|
27
|
+RETURNING node_id, parent_id, node_order, node_type, created_at, updated_at,
|
|
28
|
+ data_label, data_content, data_datetime, node_status, data_reminder_datetime,
|
|
29
|
+ data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
|
|
30
|
+ node_depth, owner_id;
|
|
31
|
+
|
|
32
|
+CREATE OR REPLACE FUNCTION pod_update_node() RETURNS trigger AS $$
|
|
33
|
+BEGIN
|
|
34
|
+INSERT INTO pod_nodes_history (node_id, parent_id, node_order, node_type, created_at, updated_at,
|
|
35
|
+ data_label, data_content, data_datetime, node_status, data_reminder_datetime,
|
|
36
|
+ data_file_name, data_file_content, data_file_mime_type, parent_tree_path,
|
|
37
|
+ node_depth, owner_id, version_id) 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'));
|
|
38
|
+return new;
|
|
39
|
+END;
|
|
40
|
+$$ LANGUAGE plpgsql;
|
|
41
|
+
|
|
42
|
+CREATE TRIGGER pod_update_node_tg INSTEAD OF UPDATE ON pod_nodes FOR EACH ROW EXECUTE PROCEDURE pod_update_node();
|