Files
Jellystat/backend/migrations/057_jf_library_count_view.js
Thegan Govender c284d59454 Fix for incorrect stat count
Forgot to add archived items exclusion when tallying stats
This feature has been extended to seasons and seasons/episodes as some episodes may be archived but not the entire show
2023-11-26 20:06:33 +02:00

46 lines
1.5 KiB
JavaScript

exports.up = async function(knex) {
try
{
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.jf_library_count_view
AS
SELECT l."Id",
l."Name",
l."CollectionType",
count(DISTINCT i."Id") AS "Library_Count",
count(DISTINCT s."Id") AS "Season_Count",
count(DISTINCT e."Id") AS "Episode_Count"
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id" AND i.archived=false
LEFT JOIN jf_library_seasons s ON s."SeriesId" = i."Id" AND s.archived=false
LEFT JOIN jf_library_episodes e ON e."SeasonId" = s."Id" AND e.archived=false
GROUP BY l."Id", l."Name"
ORDER BY (count(DISTINCT i."Id")) DESC;`);
}catch (error) {
console.error(error);
}
};
exports.down = async function(knex) {
try {
await knex.schema.raw(`
CREATE OR REPLACE VIEW public.jf_library_count_view
AS
SELECT l."Id",
l."Name",
l."CollectionType",
count(DISTINCT i."Id") AS "Library_Count",
count(DISTINCT s."Id") AS "Season_Count",
count(DISTINCT e."Id") AS "Episode_Count"
FROM jf_libraries l
JOIN jf_library_items i ON i."ParentId" = l."Id"
LEFT JOIN jf_library_seasons s ON s."SeriesId" = i."Id"
LEFT JOIN jf_library_episodes e ON e."SeasonId" = s."Id"
GROUP BY l."Id", l."Name"
ORDER BY (count(DISTINCT i."Id")) DESC;`);
} catch (error) {
console.error(error);
}
};