Skip to content

Commit

Permalink
Merge pull request #258 from DestinyItemManager/loadout-share-migrate
Browse files Browse the repository at this point in the history
Migrate loadout shares
  • Loading branch information
bhollis authored Dec 21, 2024
2 parents a58cab7 + 210ef5c commit 5f8d56c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 210 deletions.
98 changes: 0 additions & 98 deletions api/db/loadout-share-queries.test.ts

This file was deleted.

84 changes: 0 additions & 84 deletions api/db/loadout-share-queries.ts

This file was deleted.

27 changes: 5 additions & 22 deletions api/routes/loadout-share.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import crypto from 'crypto';
import asyncHandler from 'express-async-handler';
import base32 from 'hi-base32';
import { transaction } from '../db/index.js';
import { getLoadoutShare, recordAccess } from '../db/loadout-share-queries.js';
import { metrics } from '../metrics/index.js';
import { ApiApp } from '../shapes/app.js';
import {
Expand Down Expand Up @@ -117,25 +115,10 @@ export const getLoadoutShareHandler = asyncHandler(async (req, res) => {
});

export async function loadLoadoutShare(shareId: string) {
// First look in Stately
try {
const loadout = await getLoadoutShareStately(shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccessStately(shareId);
return loadout;
}
} catch (e) {
console.error('Failed to load loadout share from Stately', e);
}

// Fall back to Postgres
return transaction(async (client) => {
const loadout = await getLoadoutShare(client, shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccess(client, shareId);
}
const loadout = await getLoadoutShareStately(shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccessStately(shareId);
return loadout;
});
}
}
4 changes: 4 additions & 0 deletions api/shapes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface ProfileResponse {
/** Hashes of tracked triumphs */
triumphs?: number[];
searches?: Search[];

syncTokens?: {
[key: string]: string;
};
}

/**
Expand Down
6 changes: 6 additions & 0 deletions api/stately/init/migrate-loadout-shares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import { migrateLoadoutShareChunk } from '../migrator/loadout-shares.js';

// while (true) {
// await migrateLoadoutShareChunk();
// console.log('Migrated loadout shares');
// }
24 changes: 23 additions & 1 deletion api/stately/loadout-share-queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { keyPath, StatelyError } from '@stately-cloud/client';
import { keyPath, StatelyError, WithPutOptions } from '@stately-cloud/client';
import { Loadout } from '../shapes/loadouts.js';
import { client } from './client.js';
import { LoadoutShare as StatelyLoadoutShare } from './generated/index.js';
Expand Down Expand Up @@ -56,6 +56,28 @@ export async function addLoadoutShare(
}
}

/**
* Put loadout shares - this is meant for migrations.
*/
export async function addLoadoutSharesForMigration(
shares: {
platformMembershipId: string;
shareId: string;
loadout: Loadout;
}[],
): Promise<void> {
const statelyShares = shares.map(
({ platformMembershipId, shareId, loadout }): WithPutOptions<StatelyLoadoutShare> => ({
item: convertLoadoutShareToStately(loadout, platformMembershipId, shareId),
// Preserve the original timestamps
overwriteMetadataTimestamps: true,
}),
);

// We overwrite here - shares are immutable, so this is fine.
await client.putBatch(...statelyShares);
}

/**
* Touch the last_accessed_at and visits fields to keep track of access.
*/
Expand Down
2 changes: 2 additions & 0 deletions api/stately/loadouts-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +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),
};
}

Expand Down
15 changes: 15 additions & 0 deletions api/stately/migrator/loadout-shares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// import { transaction } from '../../db/index.js';
// import { deleteLoadoutShares, getLoadoutShares } from '../../db/loadout-share-queries.js';
// import { addLoadoutSharesForMigration } from '../loadout-share-queries.js';

// export async function migrateLoadoutShareChunk() {
// await transaction(async (db) => {
// const loadouts = await getLoadoutShares(db, 50);
// await Promise.all(loadouts.map((loadout) => addLoadoutSharesForMigration([loadout])));
// console.log('Added to stately');
// await deleteLoadoutShares(
// db,
// loadouts.map((loadout) => loadout.shareId),
// );
// });
// }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@google-cloud/profiler": "^6.0.2",
"@sentry/node": "^7.119.2",
"@sentry/tracing": "^7.114.0",
"@stately-cloud/client": "^0.17.1",
"@stately-cloud/client": "^0.19.0",
"bungie-api-ts": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f8d56c

Please sign in to comment.