drink-it-cold/sql/schema.sql

280 lines
13 KiB
MySQL
Raw Normal View History

-- Drink it cold – Datenbankschema (MariaDB 10.11, utf8mb4)
--
-- Wird vom Installer (install.php) angewendet. Datumsangaben sind reine
-- Kalendertage; Zeitstempel werden in UTC gespeichert. Änderungen am Schema
-- bekommen eine neue Nummer in schema_migrations.
CREATE TABLE IF NOT EXISTS schema_migrations (
version INT UNSIGNED NOT NULL,
applied_at DATETIME NOT NULL,
PRIMARY KEY (version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS trailers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
label VARCHAR(40) NOT NULL,
color_name VARCHAR(40) NOT NULL,
color_hex CHAR(7) NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
active TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY uq_trailers_label (label),
KEY idx_trailers_sort (active, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS employees (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
active TINYINT(1) NOT NULL DEFAULT 1,
sort_order INT NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_employees_name (name),
KEY idx_employees_sort (active, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL,
email VARCHAR(190) NULL,
password_hash VARCHAR(255) NOT NULL,
role ENUM('admin', 'user') NOT NULL DEFAULT 'user',
status ENUM('active', 'disabled') NOT NULL DEFAULT 'active',
employee_id BIGINT UNSIGNED NULL,
must_change_password TINYINT(1) NOT NULL DEFAULT 1,
auth_epoch INT UNSIGNED NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
last_login_at DATETIME NULL,
password_changed_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_users_name (first_name, last_name),
UNIQUE KEY uq_users_email (email),
UNIQUE KEY uq_users_employee (employee_id),
CONSTRAINT fk_users_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS bookings (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
trailer_id BIGINT UNSIGNED NOT NULL,
employee_id BIGINT UNSIGNED NOT NULL,
return_employee_id BIGINT UNSIGNED NULL DEFAULT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
start_time TIME NULL DEFAULT NULL,
end_time TIME NULL DEFAULT NULL,
deposit_cents INT UNSIGNED NOT NULL DEFAULT 0,
rent_cents INT UNSIGNED NOT NULL DEFAULT 0,
delivery_cents INT UNSIGNED NOT NULL DEFAULT 0,
total_cents INT UNSIGNED NOT NULL DEFAULT 0,
invoice TINYINT(1) NOT NULL DEFAULT 0,
invoice_no VARCHAR(40) NULL DEFAULT NULL,
kind ENUM('pickup', 'delivery') NOT NULL DEFAULT 'pickup',
customer_name VARCHAR(190) NOT NULL,
street VARCHAR(190) NOT NULL DEFAULT '',
city VARCHAR(190) NOT NULL DEFAULT '',
mobile VARCHAR(60) NOT NULL DEFAULT '',
email VARCHAR(190) NOT NULL DEFAULT '',
note VARCHAR(500) NOT NULL DEFAULT '',
status ENUM('active', 'cancelled') NOT NULL DEFAULT 'active',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
created_by BIGINT UNSIGNED NULL,
updated_by BIGINT UNSIGNED NULL,
cancelled_at DATETIME NULL,
cancelled_by BIGINT UNSIGNED NULL,
PRIMARY KEY (id),
KEY idx_bookings_period (status, start_date, end_date),
KEY idx_bookings_trailer (trailer_id, status, start_date),
KEY idx_bookings_employee (employee_id, status, start_date),
KEY idx_bookings_return_employee (return_employee_id, status, start_date),
KEY idx_bookings_customer (customer_name),
CONSTRAINT fk_bookings_trailer FOREIGN KEY (trailer_id) REFERENCES trailers (id) ON DELETE RESTRICT,
CONSTRAINT fk_bookings_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE RESTRICT,
CONSTRAINT fk_bookings_return_employee FOREIGN KEY (return_employee_id) REFERENCES employees (id) ON DELETE SET NULL,
CONSTRAINT fk_bookings_created_by FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL,
CONSTRAINT fk_bookings_updated_by FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL,
CONSTRAINT fk_bookings_cancelled_by FOREIGN KEY (cancelled_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Abgehakte Erinnerungen. Der Schlüssel enthält das Fälligkeitsdatum, damit
-- eine Terminverschiebung die Erinnerung automatisch wieder öffnet.
CREATE TABLE IF NOT EXISTS reminder_done (
booking_id BIGINT UNSIGNED NOT NULL,
kind ENUM('start_8', 'start_1', 'end_1') NOT NULL,
due_date DATE NOT NULL,
done_by BIGINT UNSIGNED NULL,
done_at DATETIME NOT NULL,
PRIMARY KEY (booking_id, kind, due_date),
KEY idx_reminder_done_due (due_date),
CONSTRAINT fk_reminder_done_booking FOREIGN KEY (booking_id) REFERENCES bookings (id) ON DELETE CASCADE,
CONSTRAINT fk_reminder_done_user FOREIGN KEY (done_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Versandprotokoll für den täglichen E-Mail-Digest (verhindert Doppelmails).
CREATE TABLE IF NOT EXISTS reminder_mail_log (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
booking_id BIGINT UNSIGNED NOT NULL,
kind ENUM('start_8', 'start_1', 'end_1') NOT NULL,
due_date DATE NOT NULL,
employee_id BIGINT UNSIGNED NULL,
sent_to VARCHAR(190) NOT NULL,
sent_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_reminder_mail (booking_id, kind, due_date),
KEY idx_reminder_mail_employee (employee_id, sent_at),
CONSTRAINT fk_reminder_mail_booking FOREIGN KEY (booking_id) REFERENCES bookings (id) ON DELETE CASCADE,
CONSTRAINT fk_reminder_mail_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS auth_tokens (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
selector CHAR(24) NOT NULL,
validator_hash CHAR(64) NOT NULL,
created_at DATETIME NOT NULL,
expires_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_auth_tokens_selector (selector),
KEY idx_auth_tokens_user (user_id, expires_at),
CONSTRAINT fk_auth_tokens_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS user_invites (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
token_hash CHAR(64) NOT NULL,
created_by BIGINT UNSIGNED NULL,
created_at DATETIME NOT NULL,
expires_at DATETIME NOT NULL,
used_at DATETIME NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_user_invites_token (token_hash),
KEY idx_user_invites_user (user_id, expires_at),
CONSTRAINT fk_user_invites_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT fk_user_invites_creator FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS login_attempts (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
ip VARCHAR(45) NOT NULL,
name_key VARCHAR(170) NOT NULL,
attempted_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY idx_login_ip (ip, attempted_at),
KEY idx_login_name (name_key, attempted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Startausstattung: die in der Anforderung genannten Kühlanhänger
-- (weitere können im Admin angelegt werden) und das
-- bestehende Mitarbeiter-Team. Bestehende Zeilen bleiben bei erneutem
-- Installationslauf unverändert.
INSERT IGNORE INTO trailers (label, color_name, color_hex, sort_order) VALUES
('Nr. 1', 'Grün', '#2e7d32', 10),
('Nr. 2', 'Gelb', '#d8a412', 20),
('Nr. 3', 'Rot', '#c0392b', 30),
('Nr. 4', 'Blau', '#196abc', 40),
('Nr. 5', 'Lila', '#7b3fa0', 50),
('Nr. 6', 'Braun', '#795548', 60),
('Nr. 7', 'Hellgrau', '#9aa7b1', 70),
('Nr. 8', 'Schwarz', '#263238', 80);
INSERT IGNORE INTO employees (name, sort_order, created_at) VALUES
('M. Krücken', 10, UTC_TIMESTAMP()),
('N. Jorns', 20, UTC_TIMESTAMP()),
('M. Schmitz', 30, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (1, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (2, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (3, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (4, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (5, UTC_TIMESTAMP());
INSERT IGNORE INTO schema_migrations (version, applied_at) VALUES (6, UTC_TIMESTAMP());
-- Web-Push-Abos (VAPID) und Versandprotokoll für Erinnerungen.
CREATE TABLE IF NOT EXISTS push_subscriptions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
endpoint VARCHAR(1000) NOT NULL,
endpoint_hash CHAR(64) NOT NULL,
p256dh VARCHAR(120) NOT NULL,
auth VARCHAR(40) NOT NULL,
user_agent VARCHAR(255) NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
last_success_at DATETIME NULL,
last_error VARCHAR(255) NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_push_endpoint (endpoint_hash),
KEY idx_push_user (user_id, updated_at),
CONSTRAINT fk_push_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS reminder_push_log (
booking_id BIGINT UNSIGNED NOT NULL,
kind ENUM('start_8', 'start_1', 'end_1') NOT NULL,
due_date DATE NOT NULL,
employee_id BIGINT UNSIGNED NULL,
user_id BIGINT UNSIGNED NULL,
sent_at DATETIME NOT NULL,
PRIMARY KEY (booking_id, kind, due_date),
KEY idx_reminder_push_due (due_date),
CONSTRAINT fk_reminder_push_booking FOREIGN KEY (booking_id) REFERENCES bookings (id) ON DELETE CASCADE,
CONSTRAINT fk_reminder_push_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE SET NULL,
CONSTRAINT fk_reminder_push_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS absences (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
employee_id BIGINT UNSIGNED NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
note VARCHAR(160) NOT NULL DEFAULT '',
created_by BIGINT UNSIGNED NULL DEFAULT NULL,
updated_by BIGINT UNSIGNED NULL DEFAULT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY idx_absences_employee (employee_id, start_date, end_date),
KEY idx_absences_period (start_date, end_date),
CONSTRAINT fk_absences_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE CASCADE,
CONSTRAINT fk_absences_created_by FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL,
CONSTRAINT fk_absences_updated_by FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Kundenrundmail: Sperrliste für Abmeldungen und Versandprotokoll.
-- Empfänger werden je Kampagne aus bookings.email gezogen (app/mailing.php).
CREATE TABLE IF NOT EXISTS mail_optouts (
email VARCHAR(190) NOT NULL,
source VARCHAR(40) NOT NULL DEFAULT '',
created_at DATETIME NOT NULL,
PRIMARY KEY (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS mailings (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
subject VARCHAR(190) NOT NULL,
body TEXT NOT NULL,
recipient_count INT UNSIGNED NOT NULL DEFAULT 0,
sent_count INT UNSIGNED NOT NULL DEFAULT 0,
failed_count INT UNSIGNED NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
created_by BIGINT UNSIGNED NULL DEFAULT NULL,
finished_at DATETIME NULL DEFAULT NULL,
PRIMARY KEY (id),
KEY idx_mailings_created (created_at),
CONSTRAINT fk_mailings_created_by FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS mailing_recipients (
mailing_id BIGINT UNSIGNED NOT NULL,
email VARCHAR(190) NOT NULL,
status ENUM('pending', 'sent', 'failed') NOT NULL DEFAULT 'pending',
error VARCHAR(190) NOT NULL DEFAULT '',
sent_at DATETIME NULL DEFAULT NULL,
PRIMARY KEY (mailing_id, email),
KEY idx_mailing_recipients_status (mailing_id, status),
CONSTRAINT fk_mailing_recipients_mailing FOREIGN KEY (mailing_id) REFERENCES mailings (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;