From 691c3599854f4db7b7c3331f9a38aeaed3b7a128 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 25 Mar 2017 19:35:28 +0100 Subject: [PATCH] Improve history handling on backend. --- .../0009.history.improvements.up.sql | 42 +++++++++++++++++++ backend/src/uxbox/migrations.clj | 7 +++- backend/test/uxbox/tests/test_pages.clj | 6 +-- 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 backend/resources/migrations/0009.history.improvements.up.sql diff --git a/backend/resources/migrations/0009.history.improvements.up.sql b/backend/resources/migrations/0009.history.improvements.up.sql new file mode 100644 index 000000000..47ccd23ed --- /dev/null +++ b/backend/resources/migrations/0009.history.improvements.up.sql @@ -0,0 +1,42 @@ +DROP TRIGGER page_on_update_tgr ON pages; + +CREATE OR REPLACE FUNCTION handle_page_update() + RETURNS TRIGGER AS $pagechange$ + BEGIN + --- Update projects modified_at attribute when a + --- page of that project is modified. + UPDATE projects SET modified_at = clock_timestamp() + WHERE id = OLD.project; + + RETURN NEW; + END; +$pagechange$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION handle_page_history() + RETURNS TRIGGER AS $pagehistory$ + BEGIN + INSERT INTO pages_history (page, "user", created_at, + modified_at, data, version) + VALUES (NEW.id, NEW."user", NEW.modified_at, + NEW.modified_at, NEW.data, NEW.version); + + RETURN NEW; + END; +$pagehistory$ LANGUAGE plpgsql; + + +CREATE TRIGGER page_on_insert_tgr + AFTER INSERT ON pages + FOR EACH ROW + EXECUTE PROCEDURE handle_page_history(); + +CREATE TRIGGER page_on_update_tgr + AFTER UPDATE ON pages + FOR EACH ROW + EXECUTE PROCEDURE handle_page_update(); + +CREATE TRIGGER page_on_update_history_tgr + AFTER UPDATE ON pages + FOR EACH ROW + WHEN (OLD.data IS DISTINCT FROM NEW.data) + EXECUTE PROCEDURE handle_page_history(); diff --git a/backend/src/uxbox/migrations.clj b/backend/src/uxbox/migrations.clj index 93ed725e8..d07cf1fc1 100644 --- a/backend/src/uxbox/migrations.clj +++ b/backend/src/uxbox/migrations.clj @@ -51,6 +51,10 @@ "Create initial tables for image collections." :up (mg/resource "migrations/0008.icons.up.sql")) +(defmigration history-0009 + "Add improvements on how history is managed for pages." + :up (mg/resource "migrations/0009.history.improvements.up.sql")) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Entry point ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -65,7 +69,8 @@ [:0005 kvstore-0005] [:0006 emails-queue-0006] [:0007 images-0007] - [:0008 icons-0008]]}) + [:0008 icons-0008] + [:0009 history-0009]]}) (defn- migrate [] diff --git a/backend/test/uxbox/tests/test_pages.clj b/backend/test/uxbox/tests/test_pages.clj index 98964b9f3..70c7335c4 100644 --- a/backend/test/uxbox/tests/test_pages.clj +++ b/backend/test/uxbox/tests/test_pages.clj @@ -160,7 +160,7 @@ ;; Check inserted history (let [sqlv ["SELECT * FROM pages_history WHERE page=?" (:id data)] result (sc/fetch conn sqlv)] - (t/is (= (count result) 100))) + (t/is (= (count result) 101))) ;; Check retrieve all items (with-server {:handler (uft/routes)} @@ -169,7 +169,7 @@ ;; (println "RESPONSE:" status result) (t/is (= (count result) 10)) (t/is (= 200 status)) - (t/is (= 99 (:version (first result)))) + (t/is (= 100 (:version (first result)))) (let [params {:query {:since (:version (last result)) :max 20}} @@ -177,7 +177,7 @@ ;; (println "RESPONSE:" status result) (t/is (= (count result) 20)) (t/is (= 200 status)) - (t/is (= 89 (:version (first result)))))) + (t/is (= 90 (:version (first result)))))) )))) (t/deftest test-http-page-history-update