1 CREATE EXTENSION
IF NOT EXISTS plpgsql
WITH SCHEMA pg_catalog
;
2 COMMENT ON EXTENSION plpgsql
IS 'PL/pgSQL procedural language';
3 -- needed for rights management
4 -- CREATE EXTENSION IF NOT EXISTS acl WITH SCHEMA public;
6 -- CREATE USER WITH ...
9 CREATE TABLE public.
auth_user (
11 password character varying(128) NOT NULL,
12 last_login
timestamp with time zone,
13 is_superuser
boolean NOT NULL,
14 username
character varying(150) NOT NULL,
15 first_name
character varying(30) NOT NULL,
16 last_name
character varying(30) NOT NULL,
17 email
character varying(254) NOT NULL,
18 is_staff
boolean NOT NULL,
19 is_active
boolean NOT NULL,
20 date_joined
timestamp with time zone DEFAULT now() NOT NULL,
24 ALTER TABLE public.auth_user
OWNER TO gargantua
;
26 -- TODO add publication_date
27 -- TODO typename -> type_id
28 CREATE TABLE public.
nodes (
30 typename
integer NOT NULL,
31 user_id
integer NOT NULL,
32 parent_id
integer REFERENCES public.
nodes(id) ON DELETE CASCADE ,
33 name character varying(255) DEFAULT ''::character varying NOT NULL,
34 date timestamp with time zone DEFAULT now() NOT NULL,
35 hyperdata jsonb
DEFAULT '{}'::jsonb NOT NULL,
38 FOREIGN KEY (user_id
) REFERENCES public.
auth_user(id) ON DELETE CASCADE
40 ALTER TABLE public.nodes
OWNER TO gargantua
;
44 CREATE TABLE public.
ngrams (
46 terms
character varying(255),
50 ALTER TABLE public.ngrams
OWNER TO gargantua
;
53 CREATE TABLE public.
nodes_ngrams (
55 node_id
integer NOT NULL,
56 ngram_id
integer NOT NULL,
57 weight
double precision,
59 FOREIGN KEY (node_id
) REFERENCES public.
nodes(id) ON DELETE CASCADE,
60 FOREIGN KEY (ngram_id
) REFERENCES public.
ngrams(id) ON DELETE CASCADE,
61 PRIMARY KEY (node_id
,ngram_id
)
63 ALTER TABLE public.nodes_ngrams
OWNER TO gargantua
;
66 -- Name: nodes_ngrams_ngrams; Type: TABLE; Schema: public; Owner: gargantua
69 CREATE TABLE public.
nodes_ngrams_ngrams (
70 node_id
integer NOT NULL REFERENCES public.
nodes(id) ON DELETE CASCADE,
71 ngram1_id
integer NOT NULL REFERENCES public.
ngrams(id) ON DELETE CASCADE,
72 ngram2_id
integer NOT NULL REFERENCES public.
ngrams(id) ON DELETE CASCADE,
73 weight
double precision,
74 PRIMARY KEY (node_id
,ngram1_id
,ngram2_id
)
77 ALTER TABLE public.nodes_ngrams_ngrams
OWNER TO gargantua
;
80 CREATE TABLE public.
nodes_nodes (
81 node1_id
integer NOT NULL,
82 node2_id
integer NOT NULL,
86 PRIMARY KEY (node1_id
, node2_id
)
88 ALTER TABLE public.nodes_nodes
OWNER TO gargantua
;
93 CREATE UNIQUE INDEX ON public.
auth_user(username
);
95 CREATE INDEX auth_user_username_like
ON public.auth_user
USING btree (username varchar_pattern_ops
);
97 --CREATE INDEX ix_nodes_typename ON public.nodes USING btree (typename);
98 --CREATE INDEX ngrams_n_idx ON public.ngrams USING btree (n);
99 CREATE INDEX nodes_hyperdata_idx
ON public.nodes
USING gin (hyperdata
);
100 CREATE UNIQUE INDEX nodes_expr_idx
ON public.nodes
USING btree (((hyperdata
->> 'uniqId'::text)));
102 CREATE UNIQUE INDEX nodes_expr_idx2
ON public.nodes
USING btree (((hyperdata
->> 'uniqIdBdd'::text)));
103 CREATE UNIQUE INDEX nodes_typename_parent_id_expr_idx
ON public.nodes
USING btree (typename
, parent_id
, ((hyperdata
->> 'uniqId'::text)));
104 CREATE INDEX nodes_user_id_typename_parent_id_idx
ON public.nodes
USING btree (user_id
, typename
, parent_id
);
107 CREATE UNIQUE INDEX ON public.
ngrams(terms
);
108 --CREATE UNIQUE INDEX ON public.ngrams(terms,n);
110 CREATE INDEX nodes_ngrams_ngram_id_idx
ON public.nodes_ngrams
USING btree (ngram_id
);
111 CREATE INDEX nodes_ngrams_ngrams_node_id_idx
ON public.nodes_ngrams_ngrams
USING btree (node_id
);
112 CREATE UNIQUE INDEX ON public.nodes_ngrams
USING btree (node_id
,ngram_id
);
113 CREATE INDEX nodes_nodes_delete
ON public.nodes_nodes
USING btree (node1_id
, node2_id
, delete);
114 CREATE UNIQUE INDEX nodes_nodes_node1_id_node2_id_idx
ON public.nodes_nodes
USING btree (node1_id
, node2_id
);
117 -- TODO user haskell-postgresql-simple to create this function
118 -- with rights typename
119 CREATE OR REPLACE FUNCTION public.
search_update()
120 RETURNS trigger AS $$
122 IF new.typename
= 4 AND new.hyperdata @
> '{"language_iso2":"EN"}' THEN
123 new.
search := to_tsvector( 'english' , (new.hyperdata
->> 'title') ||
' ' ||
(new.hyperdata
->> 'abstract'));
125 ELSIF
new.typename
= 4 AND new.hyperdata @
> '{"language_iso2":"FR"}' THEN
126 new.
search := to_tsvector( 'french' , (new.hyperdata
->> 'title') ||
' ' ||
(new.hyperdata
->> 'abstract'));
128 ELSIF
new.typename
= 41 THEN
129 new.
search := to_tsvector( 'french' , (new.hyperdata
->> 'prenom')
130 ||
' ' ||
(new.hyperdata
->> 'nom')
131 ||
' ' ||
(new.hyperdata
->> 'fonction')
134 new.
search := to_tsvector( 'english' , new.
name);
140 ALTER FUNCTION public.
search_update() OWNER TO gargantua
;
142 CREATE TRIGGER search_update_trigger
BEFORE INSERT OR UPDATE ON nodes
FOR EACH ROW EXECUTE PROCEDURE search_update();
145 -- Initialize index with already existing data
146 UPDATE nodes
SET hyperdata
= hyperdata
;