Files
PackControl/pirp/tools/run_migration.php

76 lines
1.9 KiB
PHP

<?php
/**
* Einmalige Migration: Spalten hinzufügen und PDFs generieren
*/
if (PHP_SAPI !== 'cli') {
die("Nur CLI.\n");
}
require_once __DIR__ . '/../src/config.php';
require_once __DIR__ . '/../src/db.php';
$pdo = get_db();
echo "1. Prüfe/erstelle Spalten...\n";
// Spalten hinzufügen falls nicht vorhanden
$migrations = [
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS pdf_path TEXT",
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS pdf_hash VARCHAR(64)",
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS pdf_generated_at TIMESTAMPTZ",
];
foreach ($migrations as $sql) {
try {
$pdo->exec($sql);
echo " OK: $sql\n";
} catch (PDOException $e) {
echo " FEHLER: " . $e->getMessage() . "\n";
}
}
// Index erstellen
try {
$pdo->exec("CREATE INDEX IF NOT EXISTS idx_invoices_pdf_path ON invoices(pdf_path) WHERE pdf_path IS NULL");
echo " OK: Index erstellt\n";
} catch (PDOException $e) {
// Index existiert möglicherweise schon
}
echo "\n2. Generiere PDFs für alte Rechnungen...\n";
require_once __DIR__ . '/../src/pdf_functions.php';
// Verzeichnis erstellen
$uploadDir = __DIR__ . '/../public/uploads/invoices';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0775, true);
echo " Verzeichnis erstellt: $uploadDir\n";
}
$stmt = $pdo->query("SELECT id, invoice_number FROM invoices WHERE pdf_path IS NULL ORDER BY id");
$invoices = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo " Gefunden: " . count($invoices) . " Rechnungen ohne PDF\n\n";
$success = 0;
$failed = 0;
foreach ($invoices as $inv) {
echo " Verarbeite {$inv['invoice_number']}... ";
$result = archive_invoice_pdf($inv['id']);
if ($result) {
echo "OK -> $result\n";
$success++;
} else {
echo "FEHLER\n";
$failed++;
}
}
echo "\n=== Migration abgeschlossen ===\n";
echo "Erfolgreich: $success\n";
echo "Fehlgeschlagen: $failed\n";