From e7751fa279f958833bd3b2424ce5661e8da9831c Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Mon, 21 Feb 2022 18:04:37 +0100 Subject: [PATCH] Backfilled the members `last_seen_at` column refs https://github.com/TryGhost/Team/issues/1305 - Skipped sqlite3 compatibility due to the lack of join support in update - Uses a raw migration to improve performance --- ...53-backfill-members-last-seen-at-column.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 core/server/data/migrations/versions/4.37/2022-02-21-09-53-backfill-members-last-seen-at-column.js diff --git a/core/server/data/migrations/versions/4.37/2022-02-21-09-53-backfill-members-last-seen-at-column.js b/core/server/data/migrations/versions/4.37/2022-02-21-09-53-backfill-members-last-seen-at-column.js new file mode 100644 index 0000000000..d7d8cad15f --- /dev/null +++ b/core/server/data/migrations/versions/4.37/2022-02-21-09-53-backfill-members-last-seen-at-column.js @@ -0,0 +1,31 @@ +const logging = require('@tryghost/logging'); +const {createTransactionalMigration} = require('../../utils'); + +module.exports = createTransactionalMigration( + async function up(knex) { + if (knex.client.config.client === 'sqlite3') { + logging.warn('Skipping migration for SQLite3'); + return; + } + logging.info('Backfilling the members.last_seen_at column from members_login_events.'); + await knex.raw(` + UPDATE members + INNER JOIN (SELECT member_id as id, MAX(created_at) as last_seen_at + FROM members_login_events + GROUP BY member_id) as logins ON logins.id = members.id + SET + members.last_seen_at = logins.last_seen_at + WHERE + members.last_seen_at IS NULL + OR members.last_seen_at < logins.last_seen_at + `); + }, + async function down(knex) { + if (knex.client.config.client === 'sqlite3') { + logging.warn('Skipping migration for SQLite3'); + return; + } + logging.info('Rolling back the backfilling of the members.last_seen_at column from members_login_events.'); + await knex('members').update({last_seen_at: null}); + } +);