66 lines
2.5 KiB
JavaScript
66 lines
2.5 KiB
JavaScript
/**
|
||
* calendar_migrate.js
|
||
* Führe einmalig aus: node db/calendar_migrate.js
|
||
*
|
||
* Erstellt die appointments-Tabelle für den Kalender.
|
||
* Ärzte werden aus der bestehenden `users`-Tabelle (role = 'arzt') gezogen.
|
||
*/
|
||
|
||
// ✅ MUSS als erstes stehen – lädt CONFIG_KEY bevor config-manager greift
|
||
require("dotenv").config();
|
||
|
||
const db = require("../db");
|
||
|
||
async function migrate() {
|
||
const conn = db.promise();
|
||
|
||
console.log("→ Erstelle Kalender-Tabellen …");
|
||
|
||
// ── Termine ──────────────────────────────────────────────────────────────
|
||
await conn.query(`
|
||
CREATE TABLE IF NOT EXISTS appointments (
|
||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
doctor_id INT NOT NULL COMMENT 'Referenz auf users.id (role=arzt)',
|
||
date DATE NOT NULL,
|
||
time TIME NOT NULL,
|
||
duration INT NOT NULL DEFAULT 15 COMMENT 'Minuten',
|
||
patient_name VARCHAR(150) NOT NULL,
|
||
notes TEXT DEFAULT NULL,
|
||
status ENUM('scheduled','completed','cancelled') DEFAULT 'scheduled',
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
INDEX idx_date (date),
|
||
INDEX idx_doctor (doctor_id),
|
||
INDEX idx_date_doc (date, doctor_id)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||
`);
|
||
|
||
console.log("✓ Tabelle `appointments` bereit");
|
||
|
||
// ── Farben für Ärzte ─────────────────────────────────────────────────────
|
||
// Falls die users-Tabelle noch keine doctor_color-Spalte hat, fügen wir sie hinzu.
|
||
// Fehler = Spalte existiert schon → ignorieren.
|
||
try {
|
||
await conn.query(`
|
||
ALTER TABLE users
|
||
ADD COLUMN doctor_color VARCHAR(20) DEFAULT '#3B82F6'
|
||
AFTER role;
|
||
`);
|
||
console.log("✓ Spalte `users.doctor_color` hinzugefügt");
|
||
} catch (e) {
|
||
if (e.code === "ER_DUP_FIELDNAME") {
|
||
console.log("ℹ️ Spalte `users.doctor_color` existiert bereits – übersprungen");
|
||
} else {
|
||
throw e;
|
||
}
|
||
}
|
||
|
||
console.log("\n✅ Kalender-Migration abgeschlossen.\n");
|
||
process.exit(0);
|
||
}
|
||
|
||
migrate().catch((err) => {
|
||
console.error("❌ Migration fehlgeschlagen:", err.message);
|
||
process.exit(1);
|
||
});
|