1 CREATE EXTENSION
IF NOT EXISTS plpgsql
WITH SCHEMA pg_catalog
;
2 COMMENT ON EXTENSION plpgsql
IS 'PL/pgSQL procedural language';
4 -- CREATE USER WITH ...
7 CREATE TABLE public.
auth_user (
9 password character varying(128) NOT NULL,
10 last_login
timestamp with time zone,
11 is_superuser
boolean NOT NULL,
12 username
character varying(150) NOT NULL,
13 first_name
character varying(30) NOT NULL,
14 last_name
character varying(30) NOT NULL,
15 email
character varying(254) NOT NULL,
16 is_staff
boolean NOT NULL,
17 is_active
boolean NOT NULL,
18 date_joined
timestamp with time zone DEFAULT now() NOT NULL,
22 ALTER TABLE public.auth_user
OWNER TO gargantua
;
25 -- TODO add publication_date
26 -- TODO typename -> type_id
27 CREATE TABLE public.
nodes (
29 typename
integer NOT NULL,
30 user_id
integer NOT NULL,
31 parent_id
integer REFERENCES public.
nodes(id) ON DELETE CASCADE ,
32 name character varying(255) DEFAULT ''::character varying NOT NULL,
33 date timestamp with time zone DEFAULT now() NOT NULL,
34 hyperdata jsonb
DEFAULT '{}'::jsonb NOT NULL,
37 FOREIGN KEY (user_id
) REFERENCES public.
auth_user(id) ON DELETE CASCADE
39 ALTER TABLE public.nodes
OWNER TO gargantua
;
42 CREATE TABLE public.
ngrams (
44 terms
character varying(255),
48 ALTER TABLE public.ngrams
OWNER TO gargantua
;
50 --------------------------------------------------------------
51 --------------------------------------------------------------
52 -- TODO: delete delete this table
53 CREATE TABLE public.
nodes_ngrams (
55 node_id
integer NOT NULL,
56 ngrams_id
integer NOT NULL,
57 parent_id
integer REFERENCES public.
nodes_ngrams(id) ON DELETE SET NULL,
60 weight
double precision,
61 FOREIGN KEY (node_id
) REFERENCES public.
nodes(id) ON DELETE CASCADE,
62 FOREIGN KEY (ngrams_id
) REFERENCES public.
ngrams(id) ON DELETE CASCADE,
64 -- PRIMARY KEY (node_id,ngrams_id)
66 ALTER TABLE public.nodes_ngrams
OWNER TO gargantua
;
67 --------------------------------------------------------------
69 CREATE TABLE public.
nodes_ngrams_repo (
70 version integer NOT NULL,
71 patches jsonb
DEFAULT '{}'::jsonb NOT NULL,
74 ALTER TABLE public.nodes_ngrams_repo
OWNER TO gargantua
;
76 --------------------------------------------------------------
79 -- TODO: delete delete this table
80 CREATE TABLE public.
nodes_ngrams_ngrams (
81 node_id
integer NOT NULL REFERENCES public.
nodes(id) ON DELETE CASCADE,
82 ngram1_id
integer NOT NULL REFERENCES public.
ngrams(id) ON DELETE CASCADE,
83 ngram2_id
integer NOT NULL REFERENCES public.
ngrams(id) ON DELETE CASCADE,
84 weight
double precision,
85 PRIMARY KEY (node_id
,ngram1_id
,ngram2_id
)
88 ALTER TABLE public.nodes_ngrams_ngrams
OWNER TO gargantua
;
90 ---------------------------------------------------------
91 CREATE TABLE public.
nodes_nodes (
92 node1_id
integer NOT NULL,
93 node2_id
integer NOT NULL,
97 PRIMARY KEY (node1_id
, node2_id
)
99 ALTER TABLE public.nodes_nodes
OWNER TO gargantua
;
100 ---------------------------------------------------------
102 -- If needed for rights management at row level
103 -- CREATE EXTENSION IF NOT EXISTS acl WITH SCHEMA public;
105 CREATE TABLE public.
rights (
106 user_id
INTEGER NOT NULL REFERENCES public.
auth_user(id) ON DELETE CASCADE,
107 node_id
INTEGER NOT NULL REFERENCES public.
nodes(id) ON DELETE CASCADE,
108 rights
INTEGER NOT NULL,
109 PRIMARY KEY (user_id
, node_id
)
111 ALTER TABLE public.rights
OWNER TO gargantua
;
113 CREATE INDEX rights_userId_nodeId
ON public.rights
USING btree (user_id
,node_id
);
116 ------------------------------------------------------------
118 CREATE UNIQUE INDEX ON public.
auth_user(username
);
120 CREATE INDEX auth_user_username_like
ON public.auth_user
USING btree (username varchar_pattern_ops
);
122 --CREATE INDEX ix_nodes_typename ON public.nodes USING btree (typename);
123 --CREATE INDEX ngrams_n_idx ON public.ngrams USING btree (n);
124 CREATE INDEX nodes_hyperdata_idx
ON public.nodes
USING gin (hyperdata
);
125 CREATE UNIQUE INDEX nodes_expr_idx
ON public.nodes
USING btree (((hyperdata
->> 'uniqId'::text)));
127 CREATE UNIQUE INDEX nodes_expr_idx2
ON public.nodes
USING btree (((hyperdata
->> 'uniqIdBdd'::text)));
128 CREATE UNIQUE INDEX nodes_typename_parent_id_expr_idx
ON public.nodes
USING btree (typename
, parent_id
, ((hyperdata
->> 'uniqId'::text)));
129 CREATE INDEX nodes_user_id_typename_parent_id_idx
ON public.nodes
USING btree (user_id
, typename
, parent_id
);
131 CREATE UNIQUE INDEX ON public.
ngrams(terms
);
132 --CREATE UNIQUE INDEX ON public.ngrams(terms,n);
134 CREATE UNIQUE INDEX ON public.nodes_ngrams
USING btree (node_id
,ngrams_id
);
135 CREATE INDEX nodes_ngrams_ngrams_id_idx
ON public.nodes_ngrams
USING btree (ngrams_id
);
136 CREATE INDEX nodes_ngrams_ngrams_node_id_idx
ON public.nodes_ngrams_ngrams
USING btree (node_id
);
137 CREATE UNIQUE INDEX ON public.nodes_ngrams
USING btree (node_id
,ngrams_id
,ngrams_type
);
138 CREATE INDEX nodes_nodes_delete
ON public.nodes_nodes
USING btree (node1_id
, node2_id
, delete);
139 CREATE UNIQUE INDEX nodes_nodes_node1_id_node2_id_idx
ON public.nodes_nodes
USING btree (node1_id
, node2_id
);
142 -- TODO user haskell-postgresql-simple to create this function
143 -- with rights typename
144 CREATE OR REPLACE FUNCTION public.
search_update()
145 RETURNS trigger AS $$
147 IF new.typename
= 4 AND new.hyperdata @
> '{"language_iso2":"EN"}' THEN
148 new.
search := to_tsvector( 'english' , (new.hyperdata
->> 'title') ||
' ' ||
(new.hyperdata
->> 'abstract'));
150 ELSIF
new.typename
= 4 AND new.hyperdata @
> '{"language_iso2":"FR"}' THEN
151 new.
search := to_tsvector( 'french' , (new.hyperdata
->> 'title') ||
' ' ||
(new.hyperdata
->> 'abstract'));
153 ELSIF
new.typename
= 41 THEN
154 new.
search := to_tsvector( 'french' , (new.hyperdata
->> 'prenom')
155 ||
' ' ||
(new.hyperdata
->> 'nom')
156 ||
' ' ||
(new.hyperdata
->> 'fonction')
159 new.
search := to_tsvector( 'english' , new.
name);
165 ALTER FUNCTION public.
search_update() OWNER TO gargantua
;
167 CREATE TRIGGER search_update_trigger
BEFORE INSERT OR UPDATE ON nodes
FOR EACH ROW EXECUTE PROCEDURE search_update();
170 -- Initialize index with already existing data
171 UPDATE nodes
SET hyperdata
= hyperdata
;