Skip to content

Commit

Permalink
trying to fix sync
Browse files Browse the repository at this point in the history
  • Loading branch information
sebald committed Nov 10, 2023
1 parent 1d972b6 commit 4a36db9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/db/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { percentile } from '@/lib/utils';
import { toCompositionId, normalize } from '@/lib/xws';

import { addSquads } from './squads';
import { getLastSync, setLastSync } from './system';
import { setLastSync } from './system';
import { addTournaments } from './tournaments';

export const sync = async (date: Date) => {
Expand All @@ -20,11 +20,7 @@ export const sync = async (date: Date) => {
);

if (tournaments.length === 0) {
const a = await getLastSync();
console.log(a);
await setLastSync();
const b = await getLastSync();
console.log(b);
return {
name: 'Sync Complete!',
message: 'Already up to date!',
Expand Down
6 changes: 5 additions & 1 deletion lib/db/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { db } from './db';
// Last Sync
// ---------------
export const setLastSync = async () => {
await db
.deleteFrom('system')
.where('key', '=', 'last_sync')
.executeTakeFirstOrThrow();

await db
.insertInto('system')
.values({ key: 'last_sync', value: now() })
.onDuplicateKeyUpdate({ key: 'last_sync' })
.execute();
};

Expand Down
4 changes: 3 additions & 1 deletion scripts/setup-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const limit = pLimit(100);
// ---------------
void (async () => {
// Loading dynamically so env is correctly loaded.
const { initDatabase, teardownDatabase } = await import('@/lib/db/db');
const { db, initDatabase, teardownDatabase } = await import('@/lib/db/db');
const { addTournaments } = await import('@/lib/db/tournaments');
const { addSquads } = await import('@/lib/db/squads');
const { setLastSync } = await import('@/lib/db/system');
Expand Down Expand Up @@ -92,6 +92,8 @@ void (async () => {
console.log(
`🏁 Setup done! (${tournaments.length} Tournaments, ${squadCount} Squads)`
);

// await db.destroy();
} catch (err: any) {
console.log(chalk.red.bold(err?.body?.message || err.message || err));
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/sync-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dotenv.config({ path: '.env.local' });
// Script
// ---------------
void (async () => {
const { db } = await import('@/lib/db/db');
const { getLastSync } = await import('@/lib/db/system');
const { sync } = await import('@/lib/db/sync');

Expand All @@ -24,6 +25,8 @@ void (async () => {

const latestSync = await getLastSync();
console.log(`✅ Latest Sync: ${latestSync}`);

await db.destroy();
} catch (err: any) {
console.log(chalk.red.bold(err?.body?.message || err.message || err));
}
Expand Down

1 comment on commit 4a36db9

@vercel
Copy link

@vercel vercel bot commented on 4a36db9 Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.