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

@@ -0,0 +1,37 @@
import { Enums, prisma } from "#lib/server/prisma.js";
let start = 1;
let limit = 1;
const existing_bedarfsausweiswohnen_list = await prisma.bedarfsausweisWohnen.findMany({
where: {
rechnung_id: null
}
});
for (const ausweis of existing_bedarfsausweiswohnen_list) {
if (ausweis.bestellt != false && ausweis.registriernummer == ""){
console.log(
'ID:' + ausweis.id +
' Reg.Nr:' + ausweis.registriernummer +
' Rechnung_id: ' + ausweis.rechnung_id +
' Bestellt: ' + ausweis.bestellt +
' Ausgestellt: ' + ausweis.ausgestellt +
' Ausgestellt am: ' + ausweis.ausstellungsdatum
);
await prisma.bedarfsausweisWohnen.update({
where: {
id: ausweis.id
},
data: {
bestellt: false,
ausgestellt: false,
ausstellungsdatum: null
}
});
// if (start >= limit) break;
start++;
}
}

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;
}
},
}
});

View File

@@ -0,0 +1,46 @@
import { Enums, prisma } from "#lib/server/prisma.js";
import Papa from "papaparse"
import * as fs from "fs";
import { fileURLToPath } from "url";
import { generatePrefixedId } from "#lib/db.js";
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
const path = fileURLToPath(new URL("./users.csv", import.meta.url));
if (!fs.existsSync(path)) {
throw new Error(`${path} existiert nicht.`)
}
const file = fs.createReadStream(path, "utf8");
Papa.parse(file, {
header: true,
async complete(results, file) {
let i = 0;
for (const user of results.data as any) {
i++
if (i % 100 === 0) {
//console.log(`Processed ${i} of ${results.data.length}, ${Math.round(i / results.data.length * 100)}%`)
}
const existing = await prisma.benutzer.findFirst({
where: {
email: user.email
}
})
if (existing) {
if (existing.alte_id == null){
console.log('User: ' + user.email + ' fehlt alte id ('+user.id+')');
await prisma.benutzer.update({
where: {
email: user.email
},
data: {
alte_id: parseInt(user.id)
}
});
}
}
}
}
});

View File

@@ -19,7 +19,7 @@ Papa.parse(file, {
let i = 0;
for (const rechnung of results.data as any) {
i++
if (i % 50 === 0) {
if (i % 500 === 0) {
console.log(`Processed ${i} of ${results.data.length}, ${Math.round(i / results.data.length * 100)}%`)
}
const existing = await prisma.rechnung.findFirst({
@@ -29,7 +29,7 @@ Papa.parse(file, {
})
if (existing) {
console.log(`Rechnung für ${rechnung.id} existiert bereits.`);
//console.log(`Rechnung für ${rechnung.id} existiert bereits.`);
continue;
}
@@ -166,6 +166,7 @@ Papa.parse(file, {
await prisma.rechnung.create({
data
});
console.log('User: ' + rechnung.user_id + ' Rechnung:' + rechnung.id);
} catch (e) {
console.log(e);
continue;