0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 18:18:24 -05:00

Improve history handling on backend.

This commit is contained in:
Andrey Antukh 2017-03-25 19:35:28 +01:00
parent 0cd3442d86
commit 691c359985
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 51 additions and 4 deletions

View file

@ -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();

View file

@ -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
[]

View file

@ -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