Migrations Skripte

This commit is contained in:
Carl Mahnke
2025-04-12 16:40:21 +02:00
parent aa84dd967e
commit 48f72a2f0f
4 changed files with 122 additions and 25 deletions

View File

@@ -1,39 +1,52 @@
import moment from "moment";
import { Enums, prisma } from "#lib/server/prisma.js";
import Papa from "papaparse"
import * as fs from "fs";
import { fileURLToPath } from "url";
import { hashPassword } from "#lib/password.js";
import Papa from "papaparse";
import { generatePrefixedId } from "#lib/db.js";
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
import { tryCatch } from "#lib/tryCatch.js";
import { createBrotliDecompress } from "zlib";
const path = fileURLToPath(new URL("./ausweise.csv.br", import.meta.url)); // .br for Brotli file
const path = fileURLToPath(new URL("./rechnungen.csv", import.meta.url));
if (!fs.existsSync(path)) {
throw new Error(`${path} existiert nicht.`);
throw new Error(`${path} existiert nicht.`)
}
let i = 0;
const brotliStream = fs.createReadStream(path).pipe(createBrotliDecompress());
const file = fs.createReadStream(path, "utf8");
Papa.parse(file, {
header: true,
async complete(results, file) {
let i = 0;
for (const rechnung of results.data as any) {
i++
if (i % 1000 === 0) {
console.log(`Processed ${i} of ${results.data.length}, ${Math.round(i / results.data.length * 100)}%`)
}
const existing_rechnung = await prisma.rechnung.findFirst({
where: {
alte_id: parseInt(rechnung.id)
}
})
Papa.parse(brotliStream, {
header: true,
async complete(results, file) {
for (const dataset of results.data as any) {
const existing = await prisma.verbrauchsausweisWohnen.findFirst({
where: {
alte_ausweis_id: parseInt(dataset.id)
if (existing_rechnung) {
const existing_bedarfsausweiswohnen = await prisma.bedarfsausweisWohnen.findFirst({
where: {
alte_ausweis_id: parseInt(rechnung.ausweis_id)
}
})
if (existing_bedarfsausweiswohnen){
if (existing_bedarfsausweiswohnen.rechnung_id != existing_rechnung.id){
console.log('Rechnungsnummer weicht ab. Alte Ausweis Id:'+ rechnung.ausweis_id + ': ' + existing_bedarfsausweiswohnen.rechnung_id + ' vs ' + existing_rechnung.id);
//Todo: Rechnungsid updaten
} else {
//console.log('Rechnungsnummer Abgleich ok. Alte Ausweis Id:'+ rechnung.ausweis_id + ': ' + existing_bedarfsausweiswohnen.rechnung_id + ' vs ' + existing_rechnung.id);
}
}
});
if (existing) {
continue;
} else {
console.log('Rechnung existiert nicht: '+ parseInt(rechnung.id));
}
console.log(dataset.id);
// do something with dataset...
//if (i > 10000) break;
}
},
}
});