-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathschema.sql
More file actions
161 lines (150 loc) · 3.14 KB
/
schema.sql
File metadata and controls
161 lines (150 loc) · 3.14 KB
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
drop table if exists public.owner;
create table public.owner(
id text,
name text
);
insert into public.owner (id, name)
values ('1', 'Andrey'), ('2', 'Kaarel');
drop table if exists public.shop;
create table public.shop(
id text,
name text,
visible boolean,
owner_id text,
di_pickup text,
di_worldwide text,
dis_special text
);
INSERT INTO public.shop (
id,
name,
visible,
owner_id,
di_pickup,
di_worldwide,
dis_special
) VALUES (
'1',
'One',
true,
'1',
'1',
'2',
'3'
),
(
'2',
'Two',
true,
'2',
'1',
'2',
'3'
),
(
'3',
'Three',
false,
'1',
'1',
'2',
'3'
),
(
'4',
'Four',
true,
'2',
'1',
'2',
'3'
);
drop table if exists public.pet;
create table public.pet(
id text,
name text,
type text,
shop_id text
);
insert into public.pet (id, name, type, shop_id)
values ('1', 'Nikki', 'cat', '1'),
('2', 'Brown', 'dog', '2');
drop view if exists public.v_metadata;
drop table if exists public.metadata;
create table public.metadata(
id text,
type text,
title text
);
INSERT INTO public.metadata (id, type, title)
VALUES ('1', 'pickup', '10% off on all pickups'),
('2', 'donate', 'We accept your valuable donations via cards'),
('3', 'worldwide', 'We delivery anywhere with care');
drop table if exists public.metadata_enriched;
create table public.metadata_enriched(
id text,
type text,
title text,
enriched_text text
);
INSERT INTO public.metadata_enriched (id, type, title, enriched_text)
VALUES ('1', 'pickup', '10% off on all pickups', 'pickup enriched'),
('2', 'donate', 'We accept your valuable donations via cards', 'donate enriched'),
('3', 'worldwide', 'We delivery anywhere with care', 'worldwide enriched');
drop table if exists public.app_config CASCADE;
create table public.app_config
(
id TEXT PRIMARY KEY,
feed_date TIMESTAMPTZ NOT NULL DEFAULT NOW(),
name TEXT NOT NULL default 'Fake name',
data jsonb
);
insert into public.app_config (id, data)
values ('seo', '{ "some": "data", "other": "more data"}');
drop view if exists public.v_metadata;
CREATE VIEW public.v_metadata AS
SELECT
m.id,
m.type,
m.title,
me.enriched_text
FROM public.metadata m
LEFT JOIN public.metadata_enriched me ON m.id = me.id
LEFT JOIN LATERAL (
SELECT data as config
FROM public.app_config
WHERE id = 'seo'
LIMIT 1
) sc ON TRUE;
drop table if exists public.image;
create table public.image(
parent_type text,
parent_id text,
type text,
original text,
dim64x64 text,
dim128x128 text,
primary key (parent_type, parent_id, type)
);
insert into public.image (parent_type, parent_id, type, original, dim64x64, dim128x128)
values ('shop', '1', 'front', 'http://fake-original', 'http://fake-64', null),
('shop', '2', 'front', 'http://fake-original', 'http://fake-64', 'http://fake-128');
CREATE OR REPLACE FUNCTION public.get_shop_info(id_param text)
RETURNS json AS $$
DECLARE
result_row json;
BEGIN
SELECT json_build_object(
'assetId', s.id,
'petId', p.id,
'petType', p.type,
'text', s.name || ' - ' || p.name
)
INTO result_row
FROM public.shop s
LEFT JOIN public.pet p
ON s.id = p.shop_id
WHERE s.id = id_param;
RETURN result_row;
END;
$$ LANGUAGE plpgsql STABLE;