changeset 177:4e2451d561b1

Make schema for templates more realistic A print template should be stored with name and timestamp and the template itself will be any data to be interpreted as a template file by a templating engine. Delete relations in user_templates with their parents using CASCADE, because user_templates is only an m:n-table.
author Tom Gottfried <tom@intevation.de>
date Tue, 17 Jul 2018 16:50:16 +0200
parents 0c0826063561
children 2111ad473961
files schema/auth.sql schema/gemma.sql schema/tap_tests.sql schema/tap_tests_data.sql
diffstat 4 files changed, 20 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/schema/auth.sql	Tue Jul 17 12:01:14 2018 +0200
+++ b/schema/auth.sql	Tue Jul 17 16:50:16 2018 +0200
@@ -57,7 +57,7 @@
 ALTER TABLE user_profiles ENABLE ROW LEVEL SECURITY;
 
 CREATE POLICY own_templates ON templates FOR SELECT TO waterway_user
-       USING (id IN(SELECT template_id FROM user_templates
+       USING (template_name IN(SELECT template_name FROM user_templates
                     WHERE username = current_user));
 ALTER TABLE templates ENABLE ROW LEVEL SECURITY;
 
@@ -87,7 +87,7 @@
        USING (country = current_user_country());
 
 CREATE POLICY manage_templates ON templates FOR ALL TO waterway_admin
-       USING (id IN(SELECT template_id FROM user_templates ut
+       USING (template_name IN(SELECT template_name FROM user_templates ut
                     JOIN user_profiles p ON ut.username = p.username
                     WHERE p.country = current_user_country()));
 
--- a/schema/gemma.sql	Tue Jul 17 12:01:14 2018 +0200
+++ b/schema/gemma.sql	Tue Jul 17 16:50:16 2018 +0200
@@ -76,16 +76,17 @@
        );
 
 CREATE TABLE gemma.templates (
-       id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
-       dummy_attrib varchar,
-       "..." varchar
-       -- TODO: template attributes tbd.
+       template_name varchar PRIMARY KEY,
+       template_data bytea NOT NULL,
+       date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
        );
+CREATE TRIGGER templates_date_info BEFORE UPDATE ON templates
+       FOR EACH ROW EXECUTE PROCEDURE update_date_info();
 
 CREATE TABLE gemma.user_templates (
-       username varchar NOT NULL REFERENCES user_profiles,
-       template_id int NOT NULL REFERENCES templates,
-       PRIMARY KEY (username, template_id)
+       username varchar NOT NULL REFERENCES user_profiles ON DELETE CASCADE,
+       template_name varchar NOT NULL REFERENCES templates ON DELETE CASCADE,
+       PRIMARY KEY (username, template_name)
        );
 
 --
--- a/schema/tap_tests.sql	Tue Jul 17 12:01:14 2018 +0200
+++ b/schema/tap_tests.sql	Tue Jul 17 16:50:16 2018 +0200
@@ -32,8 +32,8 @@
 
 SELECT isnt_empty('SELECT * FROM templates',
                   'User should see templates associated to him');
-SELECT is_empty('SELECT * FROM templates t
-                 JOIN user_templates ut ON t.id = template_id
+SELECT is_empty('SELECT * FROM templates
+                 JOIN user_templates USING (template_name)
                  WHERE username <> current_user',
                 'User should only see templates associated to him');
 
@@ -66,8 +66,9 @@
                       4326))', 42501, NULL,
                  'Waterway admin cannot insert data outside his region');
 
-SELECT isnt_empty('SELECT * FROM templates t
-                   JOIN user_templates ut ON t.id = template_id
+-- template management
+SELECT isnt_empty('SELECT * FROM templates
+                   JOIN user_templates USING (template_name)
                    WHERE username <> current_user',
                   'Waterway admin should see templates of users in country');
 
--- a/schema/tap_tests_data.sql	Tue Jul 17 12:01:14 2018 +0200
+++ b/schema/tap_tests_data.sql	Tue Jul 17 16:50:16 2018 +0200
@@ -1,6 +1,6 @@
 SET search_path TO public, gemma, gemma_waterway, gemma_fairway;
 
-INSERT INTO countries VALUES ('AT');
+INSERT INTO countries VALUES ('AT'), ('RO');
 
 INSERT INTO responsibility_areas VALUES
        ('AT', ST_geomfromtext('MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 4326));
@@ -8,6 +8,7 @@
 INSERT INTO user_profiles (username, country, email_adress)
        VALUES
        ('waterway_user', 'AT', 'xxx'),
+       ('waterway_user2', 'RO', 'xxy'),
        ('waterway_admin', 'AT', 'yyy'),
        ('sys_admin', 'AT', 'zzz');
 
@@ -42,8 +43,7 @@
         'AT', 'AT', 'AT',
         1, 'depth', 'testorganization', true);
 
-INSERT INTO templates (dummy_attrib)
-       VALUES ('usertemplate'), ('othertemplate');
+INSERT INTO templates (template_name, template_data)
+       VALUES ('AT', '\x'), ('RO', '\x');
 INSERT INTO user_templates
-       SELECT 'waterway_user', id FROM templates
-              WHERE dummy_attrib = 'usertemplate';
+       VALUES ('waterway_user', 'AT'), ('waterway_user2', 'RO');