· 3 years ago · May 25, 2022, 08:50 PM
1-- Type: contact_type
2
3-- DROP TYPE IF EXISTS public.contact_type;
4
5CREATE TYPE public.contact_type AS ENUM
6 ('unknown', 'direction', 'commercial', 'production', 'support', 'external', '#err');
7
8ALTER TYPE public.contact_type
9 OWNER TO postgres;
10
11--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
12----//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
13--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
14
15-- FUNCTION: public.update_date_alter()
16
17-- DROP FUNCTION IF EXISTS public.update_date_alter();
18
19CREATE OR REPLACE FUNCTION public.update_date_alter()
20 RETURNS trigger
21 LANGUAGE 'plpgsql'
22 COST 100
23 VOLATILE NOT LEAKPROOF
24AS $BODY$
25 BEGIN
26 NEW.date_alter = NOW();
27 RETURN NEW;
28 END;
29$BODY$;
30
31ALTER FUNCTION public.update_date_alter()
32 OWNER TO postgres;
33
34
35--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
36----//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
37--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
38
39-- Table: public.company
40
41-- DROP TABLE IF EXISTS public.company;
42
43CREATE TABLE IF NOT EXISTS public.company
44(
45 comp_id bigserial NOT NULL,
46 comp_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
47 comp_active boolean NOT NULL DEFAULT true,
48 date_create timestamp with time zone DEFAULT now(),
49 date_alter timestamp with time zone DEFAULT now(),
50 CONSTRAINT "company_PRIMARY_KEY" PRIMARY KEY (comp_id)
51)
52
53TABLESPACE pg_default;
54
55ALTER TABLE IF EXISTS public.company
56 OWNER to postgres;
57-- Index: company_unique_name
58
59-- DROP INDEX IF EXISTS public.company_unique_name;
60
61CREATE UNIQUE INDEX IF NOT EXISTS company_unique_name
62 ON public.company USING btree
63 (comp_name COLLATE pg_catalog."default" ASC NULLS LAST)
64 TABLESPACE pg_default
65 WHERE comp_active;
66
67-- Trigger: comp_update_date_alter
68
69-- DROP TRIGGER IF EXISTS comp_update_date_alter ON public.company;
70
71CREATE TRIGGER comp_update_date_alter
72 BEFORE UPDATE
73 ON public.company
74 FOR EACH ROW
75 EXECUTE FUNCTION public.update_date_alter();
76--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
77----//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
78--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--
79
80-- Table: public.contact
81
82-- DROP TABLE IF EXISTS public.contact;
83
84CREATE TABLE IF NOT EXISTS public.contact
85(
86 con_id bigserial NOT NULL ,
87 con_lastname character varying(100) COLLATE pg_catalog."default",
88 con_firstname character varying(50) COLLATE pg_catalog."default",
89 con_mail character varying(255) COLLATE pg_catalog."default" NOT NULL DEFAULT 'unknown@example.com'::character varying,
90 con_active boolean NOT NULL DEFAULT true,
91 comp_id bigint NOT NULL DEFAULT 1,
92 con_comment text COLLATE pg_catalog."default",
93 con_status contact_type NOT NULL DEFAULT 'unknown'::contact_type,
94 con_date_first_interaction timestamp with time zone,
95 con_date_last_interaction timestamp with time zone,
96 date_create timestamp with time zone DEFAULT now(),
97 date_alter timestamp with time zone DEFAULT now(),
98 CONSTRAINT "contact_PRIMARY_KEY" PRIMARY KEY (con_id),
99 CONSTRAINT contact_has_company FOREIGN KEY (comp_id)
100 REFERENCES public.company (comp_id) MATCH SIMPLE
101 ON UPDATE CASCADE
102 ON DELETE SET DEFAULT
103 DEFERRABLE INITIALLY DEFERRED,
104 CONSTRAINT contact_first_interaction_cannot_be_in_future CHECK (con_date_first_interaction IS NULL OR con_date_first_interaction <= now()),
105 CONSTRAINT contact_first_interaction_imply_last_interaction CHECK (con_date_first_interaction IS NULL OR con_date_last_interaction IS NOT NULL),
106 CONSTRAINT contact_last_interaction_cannot_be_in_future CHECK (con_date_last_interaction IS NULL OR con_date_last_interaction <= now()),
107 CONSTRAINT contact_last_interaction_cannot_be_lower_than_first_one CHECK (con_date_last_interaction IS NULL OR con_date_last_interaction >= con_date_first_interaction),
108 CONSTRAINT contact_last_interaction_imply_first_interaction CHECK (con_date_last_interaction IS NULL OR con_date_first_interaction IS NOT NULL)
109)
110
111TABLESPACE pg_default;
112
113ALTER TABLE IF EXISTS public.contact
114 OWNER to postgres;
115-- Index: contact_comp_id_idx
116
117-- DROP INDEX IF EXISTS public.contact_comp_id_idx;
118
119CREATE INDEX IF NOT EXISTS contact_comp_id_idx
120 ON public.contact USING btree
121 (comp_id ASC NULLS LAST)
122 TABLESPACE pg_default;
123-- Index: contact_created_idx
124
125-- DROP INDEX IF EXISTS public.contact_created_idx;
126
127CREATE INDEX IF NOT EXISTS contact_created_idx
128 ON public.contact USING btree
129 ((timezone('Europe/Paris'::text, date_create)::date) DESC NULLS FIRST)
130 TABLESPACE pg_default;
131-- Index: contact_last_interaction_year_month_idx
132
133-- DROP INDEX IF EXISTS public.contact_last_interaction_year_month_idx;
134
135CREATE INDEX IF NOT EXISTS contact_last_interaction_year_month_idx
136 ON public.contact USING btree
137 (date_part('year'::text, timezone('Europe/Paris'::text, con_date_last_interaction)) DESC NULLS FIRST, date_part('month'::text, timezone('Europe/Paris'::text, con_date_last_interaction)) DESC NULLS FIRST)
138 TABLESPACE pg_default;
139-- Index: contact_unique_active_email
140
141-- DROP INDEX IF EXISTS public.contact_unique_active_email;
142
143CREATE UNIQUE INDEX IF NOT EXISTS contact_unique_active_email
144 ON public.contact USING btree
145 (con_mail COLLATE pg_catalog."default" ASC NULLS LAST)
146 TABLESPACE pg_default
147 WHERE con_active;
148-- Index: contact_upplastname_firstname
149
150-- DROP INDEX IF EXISTS public.contact_upplastname_firstname;
151
152CREATE INDEX IF NOT EXISTS contact_upplastname_firstname
153 ON public.contact USING btree
154 (upper(con_lastname::text) COLLATE pg_catalog."default" ASC NULLS LAST, con_firstname COLLATE pg_catalog."default" ASC NULLS LAST)
155 TABLESPACE pg_default
156 WHERE con_active;
157-- Index: contact_year_create_idx
158
159-- DROP INDEX IF EXISTS public.contact_year_create_idx;
160
161CREATE INDEX IF NOT EXISTS contact_year_create_idx
162 ON public.contact USING btree
163 (date_part('year'::text, timezone('Europe/Paris'::text, date_create)) ASC NULLS LAST)
164 TABLESPACE pg_default;
165
166-- Trigger: contact_update_date_alter
167
168-- DROP TRIGGER IF EXISTS contact_update_date_alter ON public.contact;
169
170CREATE TRIGGER contact_update_date_alter
171 BEFORE UPDATE
172 ON public.contact
173 FOR EACH ROW
174 EXECUTE FUNCTION public.update_date_alter();