scripts/anonymise-database.ts (view raw)
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 |
import { db, ticketMessages, tickets } from "@/database";
import { eq } from "drizzle-orm";
console.log("starting anonymisation of ticket logs...");
const allTickets = db.select().from(tickets).all();
console.log(`found ${allTickets.length} tickets to process`);
let processedMessages = 0;
let updatedMessages = 0;
for (const ticket of allTickets) {
const messages = db
.select()
.from(ticketMessages)
.where(eq(ticketMessages.ticketId, ticket.id))
.all();
for (const message of messages) {
processedMessages++;
if (message.content.includes(ticket.authorId)) {
const anonymisedContent = message.content.replace(
new RegExp(ticket.authorId, "g"),
"-".repeat(ticket.authorId.length),
);
db.update(ticketMessages)
.set({ content: anonymisedContent })
.where(eq(ticketMessages.id, message.id))
.run();
updatedMessages++;
if (updatedMessages % 10 === 0) {
console.log(`updated ${updatedMessages} messages so far...`);
}
}
}
}
console.log(
`done! processed ${processedMessages} messages, updated ${updatedMessages}`,
);
|