Skip to content

Commit

Permalink
Add secret
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Jan 8, 2024
1 parent a8d38f8 commit 7ff621d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"pg": "^8.11.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"typeorm": "^0.3.17"
"typeorm": "^0.3.17",
"uuid": "^9.0.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
4 changes: 4 additions & 0 deletions api/src/accounts/account.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class Account {
@Column({ type: 'jsonb', default: '{"equipped": 1, "owned": [1]}' })
skins: { equipped: number; owned: number[] };

@Column({ type: 'uuid', unique: true })
@Generated('uuid')
secret: string;

constructor(data: Partial<Account> = {}) {
Object.assign(this, data);
}
Expand Down
5 changes: 5 additions & 0 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5449,6 +5449,11 @@ uuid@^9.0.0:
resolved "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

uuid@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz"
Expand Down
89 changes: 89 additions & 0 deletions scripts/dbmigrator/migratesecrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// script to migrate legacy sb database to v2 database
// note: doesnt support games table since its so different basically pointless for a leaderboard
// u can prob add it pretty easily

import postgres from 'postgres'
import cosmetics from '../../cosmetics.json' assert { type: "json" };
import {config} from 'dotenv';
import fs from 'fs';
config();
console.log('Swordbattle.io secret* database migrator');
let lastLog = 0;

const ignoreNewDb = false;
const useStatsCached = false;
// set stopAt to integer to limit to N users migrated
let stopAt = false;

if(!process.env.OLD_DB) throw new Error('No old db url provided');
if(!process.env.NEW_DB && !ignoreNewDb) throw new Error('No new db url provided');
// connect to the old db
console.log('Connecting to old db...');
const sql = postgres(process.env.OLD_DB, {
max: 3
});

const sql2 = !ignoreNewDb ? postgres(process.env.NEW_DB, {
}) : () => {};

if(!ignoreNewDb) {
// check if te new db works
(async () => {
try {
await sql2`SELECT * FROM accounts limit 1`;
} catch (error) {
console.error('Failed to connect to new db', error);
throw error;
}
})();
}

// get all the users in the old db
const users = await sql`SELECT * FROM accounts`;
console.log('Found', users.length, 'users in old db');

// get all the users in the new db
let newUsers = !ignoreNewDb ? await sql2`SELECT * FROM accounts where is_v1 = true` : [];
console.log('Found', newUsers.length, 'users in new db');

let success = 0;
let failed = 0;
let alreadyDone = 0;
let remaining = users.length;
let noUserfound = 0;

for(const oldUser of users) {
// find new user with same username
const newUserIndex = newUsers.findIndex(u => u.username === oldUser.username);
const newUser = newUserIndex !== -1 ? newUsers[newUserIndex] : false;
remaining--;
console.clear();
console.log('Migrating secrets...');
console.log('Success:', success);
console.log('Errored:', failed);
console.log('No user found:', noUserfound);
console.log('Already done:', alreadyDone);
console.log('Remaining:', remaining);

if(!newUser) {
console.log('No new user found for', oldUser.username);
noUserfound++;
continue;
}
// update new user secret
// console.log('Migrating', oldUser.username);
if(newUser.secret === oldUser.secret) {
alreadyDone++;
continue;
}
newUser.secret = oldUser.secret;
// save
try {
await sql2`UPDATE accounts SET secret = ${newUser.secret} WHERE id = ${newUser.id}`;
success++;
} catch (error) {
failed++;
}
// remove from new users
newUsers.splice(newUserIndex, 1);
}

0 comments on commit 7ff621d

Please sign in to comment.