From 722455555dc52b1c0c54eddbdf8897b8e8ede949 Mon Sep 17 00:00:00 2001 From: EricaWind Date: Tue, 4 Apr 2017 16:21:58 +0300 Subject: [PATCH] PW-789 Add migration for v_esales_categorytree_i18n --- migrations/000.032.sql | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 migrations/000.032.sql diff --git a/migrations/000.032.sql b/migrations/000.032.sql new file mode 100644 index 0000000..c1b718f --- /dev/null +++ b/migrations/000.032.sql @@ -0,0 +1,86 @@ +-- Materialized View: v_esales_categorytree_i18n + +DROP MATERIALIZED VIEW IF EXISTS v_esales_categorytree_i18n; + +CREATE MATERIALIZED VIEW v_esales_categorytree_i18n AS +SELECT main.id, + main.parent_id, + main.locale_id, + main.locale, + main.name, + main.path, + main.is_leaf, + main.depth, + main.child_count, + main.lft, + main.rgt, + regexp_replace(unaccent(replace(replace(main.path, ' '::text, '_'::text), '&'::text, 'and'::text)), '[^\w/\-]'::text, ''::text, 'g'::text) AS path_translated + FROM ( + SELECT c.id, + ( + SELECT s.id + FROM categories s + WHERE s.lft < c.lft AND s.rgt > c.rgt + ORDER BY s.rgt - c.rgt + LIMIT 1 + ) AS parent_id, + c.locale_id, + c.locale, + c.name, + ( + SELECT lower(array_to_string(array_agg(main_1.parent_name), '/'::text)) AS path + FROM ( + SELECT node.id, + ( + SELECT category_texts.name + FROM category_texts + WHERE category_texts.locale_id = c.locale_id + AND category_texts.category_id = parent.id + ) AS parent_name + FROM categories node, + categories parent + WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id + ORDER BY parent.lft + ) main_1 + GROUP BY main_1.id + ) AS path, + CASE c.rgt - c.lft + WHEN 1 THEN 1 + ELSE 0 + END AS is_leaf, + ( + SELECT count(*) - 1 + FROM categories node, + categories parent + WHERE node.lft >= parent.lft + AND node.lft <= parent.rgt + AND node.id = c.id + ) AS depth, + ( + SELECT count(*) - 1 + FROM categories c1 + WHERE c1.lft >= c.lft AND c1.lft <= c.rgt + ) AS child_count, + c.lft, + c.rgt + FROM ( + SELECT c_1.id, + s.id AS locale_id, + s.value AS locale, + lower(s_t.name::text) AS name, + c_1.rgt, + c_1.lft + FROM locales s + JOIN category_texts s_t ON s.id = s_t.locale_id + JOIN categories c_1 ON s_t.category_id = c_1.id + ORDER BY s.id, c_1.lft + ) c + ) main +WITH DATA; + +-- Index: v_esales_categorytree_i18n_id_locale_id +-- DROP INDEX v_esales_categorytree_i18n_id_locale_id; +CREATE UNIQUE INDEX v_esales_categorytree_i18n_id_locale_id + ON v_esales_categorytree_i18n + USING btree + (id, locale_id);