]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Schema/schema.sql
[NGRAMS] Extract the listId parameter
[gargantext.git] / src / Gargantext / Database / Schema / schema.sql
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;
5
6 -- CREATE USER WITH ...
7 -- createdb "gargandb"
8
9 CREATE TABLE public.auth_user (
10 id SERIAL,
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,
21 PRIMARY KEY (id)
22 );
23
24 ALTER TABLE public.auth_user OWNER TO gargantua;
25
26 -- TODO add publication_date
27 -- TODO typename -> type_id
28 CREATE TABLE public.nodes (
29 id SERIAL,
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,
36 search tsvector,
37 PRIMARY KEY (id),
38 FOREIGN KEY (user_id) REFERENCES public.auth_user(id) ON DELETE CASCADE
39 );
40 ALTER TABLE public.nodes OWNER TO gargantua;
41
42
43
44 CREATE TABLE public.ngrams (
45 id SERIAL,
46 terms character varying(255),
47 n integer,
48 PRIMARY KEY (id)
49 );
50 ALTER TABLE public.ngrams OWNER TO gargantua;
51
52 -- TODO: delete ID
53 CREATE TABLE public.nodes_ngrams (
54 id SERIAL,
55 node_id integer NOT NULL,
56 ngrams_id integer NOT NULL,
57 ngrams_type integer,
58 list_type integer,
59 weight double precision,
60 FOREIGN KEY (node_id) REFERENCES public.nodes(id) ON DELETE CASCADE,
61 FOREIGN KEY (ngrams_id) REFERENCES public.ngrams(id) ON DELETE CASCADE,
62 PRIMARY KEY (node_id,ngrams_id)
63 );
64 ALTER TABLE public.nodes_ngrams OWNER TO gargantua;
65
66 --
67 -- Name: nodes_ngrams_ngrams; Type: TABLE; Schema: public; Owner: gargantua
68 --
69
70 CREATE TABLE public.nodes_ngrams_ngrams (
71 node_id integer NOT NULL REFERENCES public.nodes(id) ON DELETE CASCADE,
72 ngram1_id integer NOT NULL REFERENCES public.ngrams(id) ON DELETE CASCADE,
73 ngram2_id integer NOT NULL REFERENCES public.ngrams(id) ON DELETE CASCADE,
74 weight double precision,
75 PRIMARY KEY (node_id,ngram1_id,ngram2_id)
76 );
77
78 ALTER TABLE public.nodes_ngrams_ngrams OWNER TO gargantua;
79
80
81 CREATE TABLE public.nodes_nodes (
82 node1_id integer NOT NULL,
83 node2_id integer NOT NULL,
84 score real,
85 favorite boolean,
86 delete boolean,
87 PRIMARY KEY (node1_id, node2_id)
88 );
89 ALTER TABLE public.nodes_nodes OWNER TO gargantua;
90
91
92 -- INDEXES
93 CREATE UNIQUE INDEX ON public.auth_user(username);
94
95 CREATE INDEX auth_user_username_like ON public.auth_user USING btree (username varchar_pattern_ops);
96
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)));
101
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);
105
106
107 CREATE UNIQUE INDEX ON public.ngrams(terms);
108 --CREATE UNIQUE INDEX ON public.ngrams(terms,n);
109
110 CREATE INDEX nodes_ngrams_ngrams_id_idx ON public.nodes_ngrams USING btree (ngrams_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,ngrams_id,ngrams_type);
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);
115
116 -- TRIGGERS
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 $$
121 begin
122 IF new.typename = 4 AND new.hyperdata @> '{"language_iso2":"EN"}' THEN
123 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
124
125 ELSIF new.typename = 4 AND new.hyperdata @> '{"language_iso2":"FR"}' THEN
126 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
127
128 ELSIF new.typename = 41 THEN
129 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'prenom')
130 || ' ' || (new.hyperdata ->> 'nom')
131 || ' ' || (new.hyperdata ->> 'fonction')
132 );
133 ELSE
134 new.search := to_tsvector( 'english' , new.name);
135 END IF;
136 return new;
137 end
138 $$ LANGUAGE plpgsql;
139
140 ALTER FUNCTION public.search_update() OWNER TO gargantua;
141
142 CREATE TRIGGER search_update_trigger BEFORE INSERT OR UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE search_update();
143
144
145 -- Initialize index with already existing data
146 UPDATE nodes SET hyperdata = hyperdata;
147