traxxx/migrations/20240104234936_actors_metad...

35 lines
927 B
JavaScript
Raw Normal View History

exports.up = async function up(knex) {
await knex.raw(`
CREATE MATERIALIZED VIEW actors_meta AS (
SELECT
actors.*,
COUNT(DISTINCT stashes_actors) as stashed,
COUNT(DISTINCT releases_actors) as scenes,
row_to_json(avatars) as avatar
FROM actors
LEFT JOIN stashes_actors ON stashes_actors.actor_id = actors.id
LEFT JOIN releases_actors ON releases_actors.actor_id = actors.id
LEFT JOIN media AS avatars ON avatars.id = actors.avatar_media_id
GROUP BY
actors.id,
avatars.id
);
CREATE MATERIALIZED VIEW scenes_meta AS (
SELECT
releases.*,
COUNT(DISTINCT stashes_scenes) as stashed
FROM releases
LEFT JOIN stashes_scenes ON stashes_scenes.scene_id = releases.id
GROUP BY releases.id
);
`);
};
exports.down = async function down(knex) {
await knex.raw(`
DROP MATERIALIZED VIEW IF EXISTS actors_meta;
DROP MATERIALIZED VIEW IF EXISTS scenes_meta;
`);
};