-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add debug script to fix journals consistency (#4650)
* chore: add debug script to fix journals consistency * fix: lint issues
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* how to run: | ||
* pnpm tsx src/debug/populate-journal-transactions.ts <starting_journal_id> | ||
*/ | ||
|
||
import mongoose from "mongoose" | ||
|
||
import { setupMongoConnection } from "@/services/mongodb" | ||
import { Transaction } from "@/services/ledger/schema" | ||
|
||
const Journal = mongoose.connection.models["Medici_Journal"] | ||
|
||
const populateJournalTransactions = async (startingJournalId: string) => { | ||
console.log("Starting from journal ID:", startingJournalId) | ||
|
||
const journals = Journal.find({ | ||
_id: { $gte: new mongoose.Types.ObjectId(startingJournalId) }, | ||
$or: [{ _transactions: { $size: 0 } }, { _transactions: { $exists: false } }], | ||
}).cursor() | ||
|
||
let processedCount = 0 | ||
let updatedCount = 0 | ||
const startTime = Date.now() | ||
|
||
for await (const journal of journals) { | ||
processedCount++ | ||
|
||
// Find transactions for this journal | ||
const transactions = await Transaction.find({ _journal: journal._id }, { _id: 1 }) | ||
|
||
if (transactions.length <= 0) { | ||
console.error(`No transactions found for journal ${journal._id}`) | ||
continue | ||
} | ||
|
||
// Update journal | ||
const transactionIds = transactions.map((tx) => tx._id) | ||
await Journal.updateOne( | ||
{ _id: journal._id }, | ||
{ $set: { _transactions: transactionIds } }, | ||
) | ||
updatedCount++ | ||
|
||
// Log progress every 100 journals | ||
if (processedCount % 100 === 0) { | ||
const timeElapsed = (Date.now() - startTime) / 1000 | ||
console.log(` | ||
Processed: ${processedCount} journals | ||
Updated: ${updatedCount} journals | ||
Time elapsed: ${timeElapsed.toFixed(2)}s | ||
Current journal ID: ${journal._id} | ||
`) | ||
} | ||
} | ||
|
||
const totalTimeElapsed = (Date.now() - startTime) / 1000 | ||
console.log(` | ||
Finished! | ||
Total processed: ${processedCount} | ||
Total updated: ${updatedCount} | ||
Total time: ${totalTimeElapsed.toFixed(2)}s | ||
`) | ||
|
||
return { processedCount, updatedCount } | ||
} | ||
|
||
const main = async () => { | ||
const args = process.argv.slice(-1) | ||
const startingJournalId = args[0] | ||
|
||
if (!startingJournalId) { | ||
console.error("Please provide a starting journal ID") | ||
process.exit(1) | ||
} | ||
|
||
try { | ||
const result = await populateJournalTransactions(startingJournalId) | ||
console.log("Result:", result) | ||
} catch (err) { | ||
console.error("Error:", err) | ||
} | ||
} | ||
|
||
setupMongoConnection() | ||
.then(async (mongoose) => { | ||
await main() | ||
if (mongoose) await mongoose.connection.close() | ||
}) | ||
.catch((err) => console.log(err)) |