Improved showcase view.

This commit is contained in:
DebaucheryLibrarian
2023-01-14 23:44:32 +01:00
parent 2e2d8a15ac
commit b0692d78ac
4 changed files with 41 additions and 56 deletions

View File

@@ -1,20 +1,20 @@
exports.up = async (knex) => Promise.resolve()
.then(() => knex.raw(`
CREATE FUNCTION releases_is_showcased(release releases) RETURNS BOOLEAN AS $$
SELECT COALESCE(entities.showcased, false) OR COALESCE(studios.showcased, false) FROM releases
LEFT JOIN entities ON entities.id = releases.entity_id
LEFT JOIN entities AS studios ON studios.id = releases.studio_id
WHERE releases.id = release.id
$$ LANGUAGE SQL STABLE;
`)).then(() => knex.schema.alterTable('releases', (table) => {
table.index('entity_id');
table.index('studio_id');
}));
const config = require('config');
exports.down = async (knex) => Promise.resolve()
.then(() => knex.schema.alterTable('releases', (table) => {
table.dropIndex('entity_id');
table.dropIndex('studio_id');
})).then(() => knex.raw(`
DROP FUNCTION IF EXISTS releases_is_showcased;
`));
exports.up = async (knex) => knex.raw(`
CREATE VIEW releases_showcased AS (
SELECT releases.id AS release_id FROM releases
LEFT JOIN entities ON entities.id = releases.entity_id
LEFT JOIN entities AS studios ON studios.id = releases.studio_id
WHERE entities.showcased
OR entities.showcased
);
COMMENT ON VIEW releases_showcased IS E'@foreignKey (release_id) references releases (id)';
GRANT SELECT ON releases_showcased TO :visitor;
`, {
visitor: knex.raw(config.database.query.user),
});
exports.down = async (knex) => knex.raw(`
DROP VIEW IF EXISTS releases_showcased;
`);