Added last login column to users table.

This commit is contained in:
DebaucheryLibrarian 2022-04-04 00:23:37 +02:00
parent a586413240
commit 153f28c494
4 changed files with 28 additions and 29 deletions

View File

@ -1,25 +0,0 @@
exports.up = async (knex) => knex.raw(`
CREATE MATERIALIZED VIEW entities_stats
AS
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;
`);
exports.down = async (knex) => knex.raw(`
DROP MATERIALIZED VIEW entities_stats;
`);

View File

@ -0,0 +1,7 @@
exports.up = async (knex) => knex.schema.alterTable('users', (table) => {
table.datetime('last_login');
});
exports.down = async (knex) => knex.schema.alterTable('users', (table) => {
table.dropColumn('last_login');
});

View File

@ -1,10 +1,23 @@
exports.up = async (knex) => knex.raw(` exports.up = async (knex) => knex.raw(`
CREATE MATERIALIZED VIEW entities_stats CREATE MATERIALIZED VIEW entities_stats
AS AS
SELECT entities.id AS entity_id, count(releases.id) AS releases_count WITH RECURSIVE relations AS (
FROM entities SELECT entities.id, entities.parent_id, count(releases.id) AS releases_count, count(releases.id) AS total_count
LEFT JOIN releases ON releases.entity_id = entities.id FROM entities
GROUP BY entities.id; 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;
`); `);
exports.down = async (knex) => knex.raw(` exports.down = async (knex) => knex.raw(`

View File

@ -34,6 +34,10 @@ async function login(credentials) {
await verifyPassword(credentials.password, user.password); await verifyPassword(credentials.password, user.password);
await knex('users')
.update('last_login', 'NOW()')
.where('id', user.id);
return curateUser(user); return curateUser(user);
} }