Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add debug script to fix journals consistency #4650

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions core/api/src/debug/populate-journal-transactions.ts
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))
Loading