drink-it-cold/sql/migrations/012_booking_documents.sql

29 lines
1.4 KiB
MySQL
Raw Permalink Normal View History

-- Buchungsunterlagen: Mietvertrag, Auftragsbestätigung und Rechnung (PDF).
--
-- Jedes Dokument liegt als Datei unter storage/documents/<booking_id>/ und wird
-- nur über eine Anmeldung ausgeliefert. Die Aufbewahrung ist bewusst kurz:
-- privacy_cleanup() löscht Unterlagen einen Monat nach dem Mietende
-- (app.document_retention_days).
CREATE TABLE IF NOT EXISTS booking_documents (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
booking_id BIGINT UNSIGNED NOT NULL,
kind ENUM('contract', 'confirmation', 'invoice') NOT NULL,
variant VARCHAR(20) NOT NULL DEFAULT '',
file_name VARCHAR(190) NOT NULL,
stored_file VARCHAR(190) NOT NULL,
mime_type VARCHAR(100) NOT NULL DEFAULT 'application/pdf',
size_bytes INT UNSIGNED NOT NULL DEFAULT 0,
signature_file VARCHAR(190) NULL DEFAULT NULL,
signed_at DATETIME NULL DEFAULT NULL,
mailed_at DATETIME NULL DEFAULT NULL,
source ENUM('generated', 'upload') NOT NULL DEFAULT 'generated',
created_at DATETIME NOT NULL,
created_by BIGINT UNSIGNED NULL,
PRIMARY KEY (id),
KEY idx_booking_documents_booking (booking_id, kind, created_at),
KEY idx_booking_documents_created (created_at),
CONSTRAINT fk_booking_documents_booking FOREIGN KEY (booking_id) REFERENCES bookings (id) ON DELETE CASCADE,
CONSTRAINT fk_booking_documents_user FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;