From 153f28c49482c54028c2007329790ed342a9b7d3 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Mon, 4 Apr 2022 00:23:37 +0200 Subject: [PATCH] Added last login column to users table. --- migrations/20220330230122_stats.js | 25 ------------------------- migrations/20220403235645_last_login.js | 7 +++++++ migrations/_20220330230122_stats.js | 21 +++++++++++++++++---- src/auth.js | 4 ++++ 4 files changed, 28 insertions(+), 29 deletions(-) delete mode 100644 migrations/20220330230122_stats.js create mode 100644 migrations/20220403235645_last_login.js diff --git a/migrations/20220330230122_stats.js b/migrations/20220330230122_stats.js deleted file mode 100644 index fde6a0cd..00000000 --- a/migrations/20220330230122_stats.js +++ /dev/null @@ -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; -`); diff --git a/migrations/20220403235645_last_login.js b/migrations/20220403235645_last_login.js new file mode 100644 index 00000000..34cc2ad1 --- /dev/null +++ b/migrations/20220403235645_last_login.js @@ -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'); +}); diff --git a/migrations/_20220330230122_stats.js b/migrations/_20220330230122_stats.js index 02b81a71..fde6a0cd 100644 --- a/migrations/_20220330230122_stats.js +++ b/migrations/_20220330230122_stats.js @@ -1,10 +1,23 @@ exports.up = async (knex) => knex.raw(` CREATE MATERIALIZED VIEW entities_stats AS - SELECT entities.id AS entity_id, count(releases.id) AS releases_count - FROM entities - LEFT JOIN releases ON releases.entity_id = entities.id - GROUP BY entities.id; + 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(` diff --git a/src/auth.js b/src/auth.js index 48c49192..6220b180 100644 --- a/src/auth.js +++ b/src/auth.js @@ -34,6 +34,10 @@ async function login(credentials) { await verifyPassword(credentials.password, user.password); + await knex('users') + .update('last_login', 'NOW()') + .where('id', user.id); + return curateUser(user); }