Skip to content

Commit

Permalink
Fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Dec 30, 2024
1 parent 9b3268a commit 7fca20a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/db/migration-state-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface MigrationStateRow {
export async function getUsersToMigrate(client: ClientBase): Promise<number[]> {
const results = await client.query<MigrationStateRow>({
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);
}
Expand Down
2 changes: 1 addition & 1 deletion api/routes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const updateHandler = asyncHandler(async (req, res) => {
triumphs,
searches,
itemHashTags,
!migrationState.lastError,
migrationState.attemptCount > 0 || Boolean(migrationState.lastError),
);
};

Expand Down
27 changes: 19 additions & 8 deletions api/stately/init/migrate-users.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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}`);

Check failure on line 21 in api/stately/init/migrate-users.ts

View workflow job for this annotation

GitHub Actions / build

Invalid type "unknown" of template literal expression
}
}),
);
}
} 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);
}
}
4 changes: 2 additions & 2 deletions api/stately/loadouts-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand Down
8 changes: 4 additions & 4 deletions api/stately/settings-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7fca20a

Please sign in to comment.