From 7fca20a217dd9104c2f186f395e01986a050a55b Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 29 Dec 2024 16:58:27 -0800 Subject: [PATCH] Fixups --- api/db/migration-state-queries.ts | 2 +- api/routes/update.ts | 2 +- api/stately/init/migrate-users.ts | 27 +++++++++++++++++++-------- api/stately/loadouts-queries.ts | 4 ++-- api/stately/settings-queries.ts | 8 ++++---- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/api/db/migration-state-queries.ts b/api/db/migration-state-queries.ts index 3863423..191ae83 100644 --- a/api/db/migration-state-queries.ts +++ b/api/db/migration-state-queries.ts @@ -30,7 +30,7 @@ interface MigrationStateRow { export async function getUsersToMigrate(client: ClientBase): Promise { const results = await client.query({ name: 'get_users_to_migrate', - text: 'select membership_id from settings where membership_id not in (select membership_id from migration_state) limit 1000', + text: 'select distinct(s.membership_id) as membership_id from loadouts as s left join migration_state as m on s.membership_id = m.membership_id where m.membership_id is null limit 1000', }); return results.rows.map((row) => row.membership_id); } diff --git a/api/routes/update.ts b/api/routes/update.ts index 911b574..3bbe8a0 100644 --- a/api/routes/update.ts +++ b/api/routes/update.ts @@ -154,7 +154,7 @@ export const updateHandler = asyncHandler(async (req, res) => { triumphs, searches, itemHashTags, - !migrationState.lastError, + migrationState.attemptCount > 0 || Boolean(migrationState.lastError), ); }; diff --git a/api/stately/init/migrate-users.ts b/api/stately/init/migrate-users.ts index e6cf248..e4b0337 100644 --- a/api/stately/init/migrate-users.ts +++ b/api/stately/init/migrate-users.ts @@ -1,3 +1,4 @@ +import { chunk } from 'es-toolkit'; import { readTransaction } from '../../db/index.js'; import { getUsersToMigrate } from '../../db/migration-state-queries.js'; import { delay } from '../../utils.js'; @@ -6,16 +7,26 @@ import { migrateUser } from '../migrator/user.js'; while (true) { try { const bungieMembershipIds = await readTransaction(async (client) => getUsersToMigrate(client)); - for (const bungieMembershipId of bungieMembershipIds) { - try { - await migrateUser(bungieMembershipId); - console.log(`Migrated user ${bungieMembershipId}`); - } catch (e) { - console.error(`Error migrating user ${bungieMembershipId}: ${e}`); - } + if (bungieMembershipIds.length === 0) { + console.log('No users to migrate'); + break; + } + for (const idChunk of chunk(bungieMembershipIds, 10)) { + await Promise.all( + idChunk.map(async (bungieMembershipId) => { + try { + await migrateUser(bungieMembershipId); + console.log(`Migrated user ${bungieMembershipId}`); + } catch (e) { + console.error(`Error migrating user ${bungieMembershipId}: ${e}`); + } + }), + ); } } catch (e) { - console.error(`Error getting users to migrate: ${e}`); + if (e instanceof Error) { + console.error(`Error getting users to migrate: ${e}`); + } await delay(1000); } } diff --git a/api/stately/loadouts-queries.ts b/api/stately/loadouts-queries.ts index 6628d17..2cbc1a0 100644 --- a/api/stately/loadouts-queries.ts +++ b/api/stately/loadouts-queries.ts @@ -273,8 +273,8 @@ export function convertLoadoutCommonFieldsToStately( unequipped: (loadout.unequipped || []).map(convertLoadoutItemToStately), notes: loadout.notes, parameters: convertLoadoutParametersToStately(loadout.parameters), - createdAt: BigInt(loadout.createdAt ?? 0n), - lastUpdatedAt: BigInt(loadout.lastUpdatedAt ?? 0n), + createdAt: BigInt(loadout.createdAt ? new Date(loadout.createdAt).getTime() : 0n), + lastUpdatedAt: BigInt(loadout.lastUpdatedAt ? new Date(loadout.lastUpdatedAt).getTime() : 0n), }; } diff --git a/api/stately/settings-queries.ts b/api/stately/settings-queries.ts index 16e6999..c4efb59 100644 --- a/api/stately/settings-queries.ts +++ b/api/stately/settings-queries.ts @@ -214,12 +214,12 @@ export function convertToStatelyItem( }); }); - const customTotalStatsList = Object.entries(customTotalStatsByClass).map( - ([classType, customStats]) => ({ + const customTotalStatsList = Object.entries(customTotalStatsByClass) + .map(([classType, customStats]) => ({ classType: Number(classType), customStats, - }), - ); + })) + .filter((c) => c.customStats.length > 0); return client.create('Settings', { ...rest,