2022-03-31 12:11:13 +00:00
|
|
|
exports.up = async (knex) => knex.raw(`
|
|
|
|
CREATE MATERIALIZED VIEW entities_stats
|
|
|
|
AS
|
2022-04-03 22:23:37 +00:00
|
|
|
WITH RECURSIVE relations AS (
|
|
|
|
SELECT entities.id, entities.parent_id, count(releases.id) AS releases_count, count(releases.id) AS total_count
|
|
|
|
FROM entities
|
|
|
|
LEFT JOIN releases ON releases.entity_id = entities.id
|
|
|
|
GROUP BY entities.id
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT entities.id AS entity_id, count(releases.id) AS releases_count, count(releases.id) + relations.total_count AS total_count
|
|
|
|
FROM entities
|
|
|
|
INNER JOIN relations ON relations.id = entities.parent_id
|
|
|
|
LEFT JOIN releases ON releases.entity_id = entities.id
|
|
|
|
GROUP BY entities.id
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT relations.id AS entity_id, relations.releases_count
|
|
|
|
FROM relations;
|
2022-03-31 12:11:13 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
exports.down = async (knex) => knex.raw(`
|
|
|
|
DROP MATERIALIZED VIEW entities_stats;
|
|
|
|
`);
|