mirror of
https://gitlab.com/davical-project/davical.git
synced 2026-05-20 01:44:15 +00:00
Move patches into primary database definitions.
This commit is contained in:
parent
d70fd0fbd2
commit
8ee00fb91c
110
dba/davical.sql
110
dba/davical.sql
@ -1,6 +1,46 @@
|
|||||||
-- Really Simple CalDAV Store - Database Schema
|
-- DAViCal CalDAV Server - Database Schema
|
||||||
--
|
--
|
||||||
|
|
||||||
|
|
||||||
|
-- Given a verbose DAV: or CalDAV: privilege name return the bitmask
|
||||||
|
CREATE or REPLACE FUNCTION privilege_to_bits( TEXT ) RETURNS BIT(24) AS $$
|
||||||
|
DECLARE
|
||||||
|
raw_priv ALIAS FOR $1;
|
||||||
|
in_priv TEXT;
|
||||||
|
BEGIN
|
||||||
|
in_priv := trim(lower(regexp_replace(raw_priv, '^.*:', '')));
|
||||||
|
IF in_priv = 'all' THEN
|
||||||
|
RETURN ~ 0::BIT(24);
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
RETURN (CASE
|
||||||
|
WHEN in_priv = 'read' THEN 4609 -- 1 + 512 + 4096
|
||||||
|
WHEN in_priv = 'write' THEN 198 -- 2 + 4 + 64 + 128
|
||||||
|
WHEN in_priv = 'write-properties' THEN 2
|
||||||
|
WHEN in_priv = 'write-content' THEN 4
|
||||||
|
WHEN in_priv = 'unlock' THEN 8
|
||||||
|
WHEN in_priv = 'read-acl' THEN 16
|
||||||
|
WHEN in_priv = 'read-current-user-privilege-set' THEN 32
|
||||||
|
WHEN in_priv = 'bind' THEN 64
|
||||||
|
WHEN in_priv = 'unbind' THEN 128
|
||||||
|
WHEN in_priv = 'write-acl' THEN 256
|
||||||
|
WHEN in_priv = 'read-free-busy' THEN 4608 -- 512 + 4096
|
||||||
|
WHEN in_priv = 'schedule-deliver' THEN 7168 -- 1024 + 2048 + 4096
|
||||||
|
WHEN in_priv = 'schedule-deliver-invite' THEN 1024
|
||||||
|
WHEN in_priv = 'schedule-deliver-reply' THEN 2048
|
||||||
|
WHEN in_priv = 'schedule-query-freebusy' THEN 4096
|
||||||
|
WHEN in_priv = 'schedule-send' THEN 57344 -- 8192 + 16384 + 32768
|
||||||
|
WHEN in_priv = 'schedule-send-invite' THEN 8192
|
||||||
|
WHEN in_priv = 'schedule-send-reply' THEN 16384
|
||||||
|
WHEN in_priv = 'schedule-send-freebusy' THEN 32768
|
||||||
|
ELSE 0 END)::BIT(24);
|
||||||
|
END
|
||||||
|
$$
|
||||||
|
LANGUAGE 'PlPgSQL' IMMUTABLE STRICT;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- This sequence is used in a number of places so that any DAV resource will have a unique ID
|
||||||
CREATE SEQUENCE dav_id_seq;
|
CREATE SEQUENCE dav_id_seq;
|
||||||
|
|
||||||
-- Something that can look like a filesystem hierarchy where we store stuff
|
-- Something that can look like a filesystem hierarchy where we store stuff
|
||||||
@ -16,9 +56,14 @@ CREATE TABLE collection (
|
|||||||
public_events_only BOOLEAN NOT NULL DEFAULT FALSE,
|
public_events_only BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
publicly_readable BOOLEAN NOT NULL DEFAULT FALSE,
|
publicly_readable BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
collection_id INT8 PRIMARY KEY DEFAULT nextval('dav_id_seq'),
|
collection_id INT8 PRIMARY KEY DEFAULT nextval('dav_id_seq'),
|
||||||
|
default_privileges BIT(24) DEFAULT privilege_to_bits('caldav:read-free-busy'),
|
||||||
|
is_addressbook BOOLEAN DEFAULT FALSE,
|
||||||
|
resourcetypes TEXT DEFAULT '<DAV::collection/>',
|
||||||
UNIQUE(user_no,dav_name)
|
UNIQUE(user_no,dav_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
UPDATE collection SET resourcetypes = '<DAV::collection/><urn:ietf:params:xml:ns:caldav:calendar/>' WHERE is_calendar;
|
||||||
|
|
||||||
|
|
||||||
-- The main event. Where we store the things the calendar throws at us.
|
-- The main event. Where we store the things the calendar throws at us.
|
||||||
CREATE TABLE caldav_data (
|
CREATE TABLE caldav_data (
|
||||||
@ -85,6 +130,7 @@ CREATE TABLE calendar_item (
|
|||||||
CREATE INDEX calendar_item_collection_id_fkey ON calendar_item(collection_id);
|
CREATE INDEX calendar_item_collection_id_fkey ON calendar_item(collection_id);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Each user can be related to each other user. This mechanism can also
|
-- Each user can be related to each other user. This mechanism can also
|
||||||
-- be used to define groups of users, since some relationships are transitive.
|
-- be used to define groups of users, since some relationships are transitive.
|
||||||
CREATE TABLE relationship_type (
|
CREATE TABLE relationship_type (
|
||||||
@ -92,7 +138,8 @@ CREATE TABLE relationship_type (
|
|||||||
rt_name TEXT,
|
rt_name TEXT,
|
||||||
rt_togroup BOOLEAN,
|
rt_togroup BOOLEAN,
|
||||||
confers TEXT DEFAULT 'RW',
|
confers TEXT DEFAULT 'RW',
|
||||||
rt_fromgroup BOOLEAN
|
rt_fromgroup BOOLEAN,
|
||||||
|
bit_confers BIT(24) DEFAULT legacy_privilege_to_bits('RW')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@ -100,6 +147,7 @@ CREATE TABLE relationship (
|
|||||||
from_user INT REFERENCES usr (user_no) ON UPDATE CASCADE ON DELETE CASCADE,
|
from_user INT REFERENCES usr (user_no) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||||
to_user INT REFERENCES usr (user_no) ON UPDATE CASCADE ON DELETE CASCADE,
|
to_user INT REFERENCES usr (user_no) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||||
rt_id INT REFERENCES relationship_type (rt_id) ON UPDATE CASCADE ON DELETE CASCADE,
|
rt_id INT REFERENCES relationship_type (rt_id) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||||
|
bit_confers BIT(24) DEFAULT legacy_privilege_to_bits('RW'),
|
||||||
|
|
||||||
PRIMARY KEY ( from_user, to_user, rt_id )
|
PRIMARY KEY ( from_user, to_user, rt_id )
|
||||||
);
|
);
|
||||||
@ -182,15 +230,18 @@ CREATE TABLE principal_type (
|
|||||||
|
|
||||||
|
|
||||||
-- web needs SELECT,INSERT,UPDATE,DELETE
|
-- web needs SELECT,INSERT,UPDATE,DELETE
|
||||||
|
DROP TABLE principal CASCADE;
|
||||||
CREATE TABLE principal (
|
CREATE TABLE principal (
|
||||||
principal_id SERIAL PRIMARY KEY,
|
principal_id DEFAULT nextval('dav_id_seq') PRIMARY KEY,
|
||||||
type_id INT8 NOT NULL REFERENCES principal_type(principal_type_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
type_id INT8 NOT NULL REFERENCES principal_type(principal_type_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
||||||
user_no INT8 NULL REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
user_no INT8 NULL REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
||||||
displayname TEXT,
|
displayname TEXT,
|
||||||
active BOOLEAN
|
active BOOLEAN,
|
||||||
|
default_privileges BIT(24)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Allowing identification of group members.
|
-- Allowing identification of group members.
|
||||||
CREATE TABLE group_member (
|
CREATE TABLE group_member (
|
||||||
group_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
group_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
||||||
@ -200,39 +251,30 @@ CREATE UNIQUE INDEX group_member_pk ON group_member(group_id,member_id);
|
|||||||
CREATE INDEX group_member_sk ON group_member(member_id);
|
CREATE INDEX group_member_sk ON group_member(member_id);
|
||||||
|
|
||||||
|
|
||||||
-- Only needs SELECT access by website. dav_resource_type will be 'principal', 'collection', 'CalDAV:calendar' and so forth.
|
CREATE TABLE grants (
|
||||||
CREATE TABLE dav_resource_type (
|
by_principal INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
||||||
resource_type_id SERIAL PRIMARY KEY,
|
|
||||||
dav_resource_type TEXT,
|
|
||||||
resource_type_desc TEXT
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE dav_resource (
|
|
||||||
dav_id INT8 PRIMARY KEY DEFAULT nextval('dav_id_seq'),
|
|
||||||
dav_name TEXT,
|
dav_name TEXT,
|
||||||
resource_type_id INT8 REFERENCES dav_resource_type(resource_type_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
to_principal INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
||||||
owner_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
|
privileges BIT(24),
|
||||||
|
is_group BOOLEAN,
|
||||||
|
PRIMARY KEY (dav_name, to_principal)
|
||||||
|
) WITHOUT OIDS;
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE sync_tokens (
|
||||||
|
sync_token SERIAL PRIMARY KEY,
|
||||||
|
collection_id INT8 REFERENCES collection(collection_id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||||
|
modification_time TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE sync_changes (
|
||||||
CREATE TABLE privilege (
|
sync_time TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp,
|
||||||
granted_to_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
collection_id INT8 REFERENCES collection(collection_id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||||
resource_id INT8 REFERENCES dav_resource(dav_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
sync_status INT,
|
||||||
granted_by_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
dav_id INT8, -- can't REFERENCES calendar_item(dav_id) ON DELETE SET NULL ON UPDATE RESTRICT
|
||||||
can_read BOOLEAN,
|
dav_name TEXT
|
||||||
can_write BOOLEAN,
|
|
||||||
can_write_properties BOOLEAN,
|
|
||||||
can_write_content BOOLEAN,
|
|
||||||
can_unlock BOOLEAN,
|
|
||||||
can_read_acl BOOLEAN,
|
|
||||||
can_read_current_user_privilege_set BOOLEAN,
|
|
||||||
can_write_acl BOOLEAN,
|
|
||||||
can_bind BOOLEAN,
|
|
||||||
can_unbind BOOLEAN,
|
|
||||||
can_read_free_busy BOOLEAN,
|
|
||||||
PRIMARY KEY (granted_to_id, resource_id)
|
|
||||||
);
|
);
|
||||||
|
CREATE INDEX sync_processing_index ON sync_changes( collection_id, dav_id, sync_time );
|
||||||
|
|
||||||
|
|
||||||
SELECT new_db_revision(1,2,5, 'Mai' );
|
SELECT new_db_revision(1,2,7, 'Juillet' );
|
||||||
|
|||||||
@ -494,13 +494,13 @@ $$
|
|||||||
LANGUAGE 'PlPgSQL' IMMUTABLE STRICT;
|
LANGUAGE 'PlPgSQL' IMMUTABLE STRICT;
|
||||||
|
|
||||||
|
|
||||||
ALTER TABLE relationship_type ADD COLUMN bit_confers BIT(24) DEFAULT legacy_privilege_to_bits('RW');
|
ALTER TABLE relationship_type ADD COLUMN bit_confers BIT(24) DEFAULT privilege_to_bits(ARRAY['DAV::read','DAV::write']);
|
||||||
UPDATE relationship_type SET bit_confers = legacy_privilege_to_bits(confers);
|
UPDATE relationship_type SET bit_confers = legacy_privilege_to_bits(confers);
|
||||||
|
|
||||||
ALTER TABLE relationship ADD COLUMN confers BIT(24) DEFAULT legacy_privilege_to_bits('F');
|
ALTER TABLE relationship ADD COLUMN confers BIT(24) DEFAULT privilege_to_bits('caldav:read-free-busy');
|
||||||
UPDATE relationship r SET confers = bit_confers FROM relationship_type rt WHERE rt.rt_id=r.rt_id;
|
UPDATE relationship r SET confers = bit_confers FROM relationship_type rt WHERE rt.rt_id=r.rt_id;
|
||||||
|
|
||||||
ALTER TABLE collection ADD COLUMN default_privileges BIT(24) DEFAULT legacy_privilege_to_bits('F');
|
ALTER TABLE collection ADD COLUMN default_privileges BIT(24) DEFAULT privilege_to_bits('caldav:read-free-busy');
|
||||||
|
|
||||||
INSERT INTO principal_type (principal_type_id, principal_type_desc) VALUES( 1, 'Person' );
|
INSERT INTO principal_type (principal_type_id, principal_type_desc) VALUES( 1, 'Person' );
|
||||||
INSERT INTO principal_type (principal_type_id, principal_type_desc) VALUES( 2, 'Resource' );
|
INSERT INTO principal_type (principal_type_id, principal_type_desc) VALUES( 2, 'Resource' );
|
||||||
@ -509,7 +509,7 @@ INSERT INTO principal_type (principal_type_id, principal_type_desc) VALUES( 3, '
|
|||||||
-- web needs SELECT,INSERT,UPDATE,DELETE
|
-- web needs SELECT,INSERT,UPDATE,DELETE
|
||||||
DROP TABLE principal CASCADE;
|
DROP TABLE principal CASCADE;
|
||||||
CREATE TABLE principal (
|
CREATE TABLE principal (
|
||||||
principal_id SERIAL PRIMARY KEY,
|
principal_id DEFAULT nextval('dav_id_seq') PRIMARY KEY,
|
||||||
type_id INT8 NOT NULL REFERENCES principal_type(principal_type_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
type_id INT8 NOT NULL REFERENCES principal_type(principal_type_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE,
|
||||||
user_no INT8 NULL REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
user_no INT8 NULL REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
||||||
displayname TEXT,
|
displayname TEXT,
|
||||||
@ -535,14 +535,6 @@ UPDATE collection SET default_privileges = CASE
|
|||||||
ELSE (SELECT default_privileges FROM principal WHERE principal.user_no = collection.user_no)
|
ELSE (SELECT default_privileges FROM principal WHERE principal.user_no = collection.user_no)
|
||||||
END;
|
END;
|
||||||
|
|
||||||
-- Allowing identification of group members.
|
|
||||||
DROP TABLE group_member CASCADE;
|
|
||||||
CREATE TABLE group_member (
|
|
||||||
group_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
|
|
||||||
member_id INT8 REFERENCES principal(principal_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
|
|
||||||
);
|
|
||||||
CREATE UNIQUE INDEX group_member_pk ON group_member(group_id,member_id);
|
|
||||||
CREATE INDEX group_member_sk ON group_member(member_id);
|
|
||||||
INSERT INTO group_member ( group_id, member_id)
|
INSERT INTO group_member ( group_id, member_id)
|
||||||
SELECT g.principal_id, m.principal_id
|
SELECT g.principal_id, m.principal_id
|
||||||
FROM relationship JOIN principal g ON(to_user=g.user_no AND g.type_id = 3) -- Group
|
FROM relationship JOIN principal g ON(to_user=g.user_no AND g.type_id = 3) -- Group
|
||||||
|
|||||||
@ -26,7 +26,7 @@ ALTER TABLE collection ADD COLUMN resourcetypes TEXT DEFAULT '<DAV::collection/>
|
|||||||
|
|
||||||
UPDATE collection SET resourcetypes = '<DAV::collection/><urn:ietf:params:xml:ns:caldav:calendar/>' WHERE is_calendar;
|
UPDATE collection SET resourcetypes = '<DAV::collection/><urn:ietf:params:xml:ns:caldav:calendar/>' WHERE is_calendar;
|
||||||
|
|
||||||
SELECT new_db_revision(1,2,7, 'Juli' );
|
SELECT new_db_revision(1,2,7, 'Juillet' );
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
ROLLBACK;
|
ROLLBACK;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user