forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-credit-migration.ts
More file actions
64 lines (52 loc) · 1.82 KB
/
Copy pathapply-credit-migration.ts
File metadata and controls
64 lines (52 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from 'fs'
import db from '../common/src/db/index'
import * as schema from '../common/src/db/schema'
async function applyCreditMigration() {
try {
const migrationData: any[] = JSON.parse(
fs.readFileSync('credit-migration-data.json', 'utf-8'),
)
console.log(
`Starting credit migration for ${migrationData.length} users...`,
)
// Progress‑tracking file — just an array of operation_ids
const progressPath = 'credit-migration-progress.json'
let processedIds: Set<string> = new Set()
if (fs.existsSync(progressPath)) {
processedIds = new Set(JSON.parse(fs.readFileSync(progressPath, 'utf-8')))
}
for (const userData of migrationData) {
const { userId, entries } = userData
let newIdsWritten = false
for (const entry of entries) {
if (processedIds.has(entry.operation_id)) continue // already done
await db.insert(schema.creditLedger).values({
operation_id: entry.operation_id,
user_id: entry.user_id,
principal: entry.principal,
balance: entry.balance,
type: entry.type,
description: entry.description,
priority: entry.priority,
expires_at: new Date(entry.expires_at),
})
processedIds.add(entry.operation_id)
newIdsWritten = true
}
if (newIdsWritten) {
fs.writeFileSync(
progressPath,
JSON.stringify(Array.from(processedIds), null, 2),
)
console.log(`Processed credits for user ${userId}`)
} else {
console.log(`Skipped user ${userId} — all entries already migrated`)
}
}
console.log('Credit migration completed successfully!')
} catch (error) {
console.error('Error applying credit migration:', error)
throw error
}
}
applyCreditMigration()