chore(api): add data migrations
This commit is contained in:
1 parent
b5aa58dde8
commit
9c05850cba
6 files changed
+1438
No files matched your search
@@ -0,0 +1,160 @@
|
||||
import { migrateTeamIndex } from './20260502-team-index';
|
||||
|
||||
function createDb(params: {
|
||||
auctions?: Array<Record<string, unknown>>;
|
||||
matches?: Array<Record<string, unknown>>;
|
||||
}) {
|
||||
const collections = {
|
||||
auction: params.auctions ?? [],
|
||||
match: params.matches ?? [],
|
||||
};
|
||||
|
||||
return {
|
||||
collections,
|
||||
db: {
|
||||
collection(name: string) {
|
||||
const key = name === 'auction' ? 'auction' : 'match';
|
||||
const docs = collections[key];
|
||||
return {
|
||||
find() {
|
||||
return {
|
||||
limit() {
|
||||
return {
|
||||
async *[Symbol.asyncIterator]() {
|
||||
for (const doc of docs) yield doc;
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
async bulkWrite(writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: {
|
||||
$set: Record<string, unknown>;
|
||||
$unset: Record<string, ''>;
|
||||
};
|
||||
};
|
||||
}>) {
|
||||
for (const write of writes) {
|
||||
const doc = docs.find((item) => item._id === write.updateOne.filter._id);
|
||||
if (!doc) continue;
|
||||
Object.assign(doc, write.updateOne.update.$set);
|
||||
for (const key of Object.keys(write.updateOne.update.$unset)) {
|
||||
delete doc[key];
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('migrateTeamIndex', () => {
|
||||
it('migrates auction and match team identity fields', async () => {
|
||||
const { db, collections } = createDb({
|
||||
auctions: [
|
||||
{
|
||||
_id: 'auction-1',
|
||||
teams: [{ side: 'blue', groupIndex: 1 }, { side: 'red', groupIndex: 1 }],
|
||||
bids: [{ side: 'red', groupIndex: 1, amount: 100 }],
|
||||
currentBidder: 'red',
|
||||
currentBidderGroupIndex: 1,
|
||||
},
|
||||
],
|
||||
matches: [
|
||||
{
|
||||
_id: 'match-1',
|
||||
teams: [{ side: 'blue', groupIndex: 1 }, { side: 'red', groupIndex: 1 }],
|
||||
result: {
|
||||
winnerSide: 'blue',
|
||||
blueScore: 1,
|
||||
redScore: 0,
|
||||
games: [
|
||||
{
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_1',
|
||||
winnerSide: 'blue',
|
||||
blueGroupIndex: 1,
|
||||
redGroupIndex: 1,
|
||||
participants: [
|
||||
{ userId: 'u1', side: 'blue' },
|
||||
{ userId: 'u2', side: 'red' },
|
||||
],
|
||||
},
|
||||
],
|
||||
seriesResults: [
|
||||
{
|
||||
blueGroupIndex: 1,
|
||||
redGroupIndex: 1,
|
||||
blueScore: 1,
|
||||
redScore: 0,
|
||||
winnerSide: 'blue',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const stats = await migrateTeamIndex(db, { dryRun: false });
|
||||
|
||||
expect(stats).toEqual({
|
||||
scannedAuctions: 1,
|
||||
changedAuctions: 1,
|
||||
scannedMatches: 1,
|
||||
changedMatches: 1,
|
||||
});
|
||||
expect(collections.auction[0]).toMatchObject({
|
||||
teams: [{ teamIndex: 1 }, { teamIndex: 2 }],
|
||||
bids: [{ teamIndex: 2, amount: 100 }],
|
||||
currentBidderTeamIndex: 2,
|
||||
});
|
||||
expect(collections.match[0]).toMatchObject({
|
||||
teams: [{ teamIndex: 1, name: '1팀' }, { teamIndex: 2, name: '2팀' }],
|
||||
result: {
|
||||
winnerTeamIndex: 1,
|
||||
games: [
|
||||
{
|
||||
blueTeamIndex: 1,
|
||||
redTeamIndex: 2,
|
||||
winnerTeamIndex: 1,
|
||||
participants: [
|
||||
{ teamIndex: 1, side: 'blue' },
|
||||
{ teamIndex: 2, side: 'red' },
|
||||
],
|
||||
},
|
||||
],
|
||||
seriesResults: [
|
||||
{
|
||||
teamAIndex: 1,
|
||||
teamBIndex: 2,
|
||||
teamAWins: 1,
|
||||
teamBWins: 0,
|
||||
winnerTeamIndex: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(collections.auction[0]).not.toHaveProperty('currentBidder');
|
||||
expect(collections.match[0].result).not.toHaveProperty('blueScore');
|
||||
});
|
||||
|
||||
it('is idempotent after applying updates', async () => {
|
||||
const { db } = createDb({
|
||||
matches: [
|
||||
{
|
||||
_id: 'match-1',
|
||||
teams: [{ side: 'blue', groupIndex: 1 }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await migrateTeamIndex(db, { dryRun: false });
|
||||
const stats = await migrateTeamIndex(db, { dryRun: false });
|
||||
|
||||
expect(stats.changedMatches).toBe(0);
|
||||
expect(stats.changedAuctions).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user