forked from transistorsoft/background-geolocation-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.sql
165 lines (122 loc) · 4.36 KB
/
create.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- please create DB
--
-- CREATE DATABASE geolocation;
-- \connect geolocation
CREATE TABLE if not exists public.companies (
id integer NOT NULL,
company_token text,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE SEQUENCE if not exists public.companies_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.companies_id_seq OWNED BY public.companies.id;
CREATE TABLE if not exists public.devices (
id integer NOT NULL,
company_id integer,
company_token text,
device_id text,
device_model text,
created_at timestamp with time zone,
framework text,
version text,
updated_at timestamp with time zone
);
CREATE SEQUENCE if not exists public.devices_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.devices_id_seq OWNED BY public.devices.id;
CREATE SEQUENCE if not exists public.locations_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE TABLE if not exists public.locations (
id integer DEFAULT nextval('public.locations_id_seq'::regclass) NOT NULL,
latitude double precision,
longitude double precision,
recorded_at timestamp with time zone,
created_at timestamp with time zone,
company_id integer,
device_id integer,
data jsonb,
uuid text
);
ALTER TABLE ONLY public.companies ALTER COLUMN id SET DEFAULT nextval('public.companies_id_seq'::regclass);
ALTER TABLE ONLY public.devices ALTER COLUMN id SET DEFAULT nextval('public.devices_id_seq'::regclass);
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.companies ADD CONSTRAINT companies_pkey PRIMARY KEY (id);
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table constraint public.companies already exists';
END;
END $$ LANGUAGE plpgsql;
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.devices ADD CONSTRAINT devices_pkey PRIMARY KEY (id);
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table constraint public.devices already exists';
END;
END $$ LANGUAGE plpgsql;
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.locations ADD CONSTRAINT locations_pkey PRIMARY KEY (id);
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table constraint public.locations already exists';
END;
END $$ LANGUAGE plpgsql;
CREATE INDEX if not exists devices_company_id ON public.devices USING btree (company_id);
CREATE INDEX if not exists devices_company_token ON public.devices USING btree (company_token);
CREATE INDEX if not exists devices_device_id ON public.devices USING btree (device_id);
CREATE INDEX if not exists locations_company_id_device_id_recorded_at ON public.locations USING btree (company_id, device_id, recorded_at);
CREATE INDEX if not exists locations_company_id_device_ref_id_recorded_at ON public.locations USING btree (company_id, device_id, recorded_at);
CREATE INDEX if not exists locations_device_id ON public.locations USING btree (device_id);
CREATE INDEX if not exists locations_recorded_at ON public.locations USING btree (recorded_at);
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.devices
ADD CONSTRAINT devices_company_id_fkey FOREIGN KEY (company_id) REFERENCES public.companies(id) ON UPDATE CASCADE ON DELETE CASCADE;
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table fk constraint public.devices already exists';
END;
END $$ LANGUAGE plpgsql;
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.locations
ADD CONSTRAINT locations_company_id_fkey FOREIGN KEY (company_id) REFERENCES public.companies(id) ON UPDATE CASCADE ON DELETE CASCADE;
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table fk constraint public.locations:company already exists';
END;
END $$ LANGUAGE plpgsql;
DO $$
BEGIN
BEGIN
ALTER TABLE ONLY public.locations
ADD CONSTRAINT locations_device_id_fkey FOREIGN KEY (device_id) REFERENCES public.devices(id) ON UPDATE CASCADE ON DELETE CASCADE;
EXCEPTION
WHEN others THEN RAISE NOTICE 'Table fk constraint public.locations:device already exists';
END;
END $$ LANGUAGE plpgsql;
DO $$
BEGIN
BEGIN
GRANT SELECT,USAGE ON SEQUENCE public.companies_id_seq TO main;
GRANT SELECT,USAGE ON SEQUENCE public.devices_id_seq TO main;
GRANT SELECT,USAGE ON SEQUENCE public.locations_id_seq TO main;
EXCEPTION
WHEN others THEN RAISE NOTICE 'role main does not exists';
END;
END $$ LANGUAGE plpgsql;