-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterialize-setup.sql
More file actions
56 lines (52 loc) · 1.72 KB
/
materialize-setup.sql
File metadata and controls
56 lines (52 loc) · 1.72 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
-- Materialize setup script
-- Run this after both containers are up
-- Create secret for the password (drop if exists)
DROP SECRET IF EXISTS postgres_password CASCADE;
CREATE SECRET postgres_password AS 'postgres';
-- Create a connection to PostgreSQL (drop if exists)
DROP CONNECTION IF EXISTS postgres_conn CASCADE;
CREATE CONNECTION postgres_conn TO POSTGRES (
HOST 'postgres',
PORT 5432,
USER 'postgres',
PASSWORD SECRET postgres_password,
DATABASE 'product_catalog'
);
-- Create source from PostgreSQL using PUBLICATION
DROP SOURCE IF EXISTS postgres_source CASCADE;
CREATE SOURCE postgres_source
FROM POSTGRES CONNECTION postgres_conn (PUBLICATION 'mz_source')
FOR ALL TABLES;
-- Create the materialized view with product translations
DROP MATERIALIZED VIEW IF EXISTS materialized_product CASCADE;
CREATE MATERIALIZED VIEW materialized_product AS
SELECT
p.id,
p.sku,
p.price,
p.created_at,
p.updated_at,
p.name_id,
ls.code as name_code,
COALESCE(
jsonb_agg(
CASE
WHEN t.id IS NOT NULL THEN
jsonb_build_object(
'language', t.language,
'translation', t.translation,
'approved_by', a.approved_by,
'approved_at', a.approved_at
)
ELSE NULL
END
) FILTER (WHERE t.id IS NOT NULL),
'[]'::jsonb
) as localized_names
FROM
product p
INNER JOIN localized_string ls ON p.name_id = ls.id
LEFT JOIN translation t ON ls.id = t.localized_string_id
LEFT JOIN approval a ON t.id = a.translation_id
GROUP BY
p.id, p.sku, p.price, p.created_at, p.updated_at, p.name_id, ls.code;