# TimeClock v3 - SQL-Scripts Ausführungsreihenfolge

Diese Anleitung zeigt, welche SQL-Scripts in welcher Reihenfolge ausgeführt werden müssen.

## Auf dem Server ausführen:

```bash
cd /var/www/timeclock/backend

# DB-Credentials aus .env
DB_HOST=$(grep DB_HOST .env | cut -d= -f2)
DB_USER=$(grep DB_USER .env | cut -d= -f2)
DB_PASSWORD=$(grep DB_PASSWORD .env | cut -d= -f2)
DB_NAME=$(grep DB_NAME .env | cut -d= -f2)

# Alias für einfachere Befehle
alias dbexec="mysql -h $DB_HOST -u $DB_USER -p'$DB_PASSWORD' $DB_NAME"
```

## 1. Basis-Schema (WICHTIG - zuerst!)

```bash
# Erstellt alle Haupt-Tabellen
dbexec < database-schema.sql

# Zeige erstellte Tabellen
dbexec -e "SHOW TABLES;"
```

## 2. Zusätzliche Tabellen

```bash
# Invitation Table
dbexec < create-invitation-table.sql

# Watcher Table  
dbexec < create-watcher-table.sql
```

## 3. Indices für Performance

```bash
# Sick Index
dbexec < add-sick-index.sql

# Vacation Index
dbexec < add-vacation-index.sql
```

## 4. Timewish Setup (korrigierte Version!)

```bash
# Timewish mit Zeiträumen
dbexec < setup-timewish-complete.sql

# ODER falls noch Fehler: Nur die Spalten hinzufügen
dbexec << 'EOF'
-- Prüfe ob start_date existiert
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS 
                   WHERE TABLE_SCHEMA = DATABASE() 
                   AND TABLE_NAME = 'timewish' 
                   AND COLUMN_NAME = 'start_date');

SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE timewish ADD COLUMN start_date DATE DEFAULT ''2023-01-01'' AFTER end_time;',
    'SELECT ''start_date existiert bereits'';'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- end_date
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS 
                   WHERE TABLE_SCHEMA = DATABASE() 
                   AND TABLE_NAME = 'timewish' 
                   AND COLUMN_NAME = 'end_date');

SET @sql = IF(@col_exists = 0, 
    'ALTER TABLE timewish ADD COLUMN end_date DATE DEFAULT NULL AFTER start_date;',
    'SELECT ''end_date existiert bereits'';'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
EOF
```

## 5. Optionale Scripts (nur bei Bedarf!)

### User als Admin setzen
```bash
# Passe user_id an! (Deine User-ID aus der users-Tabelle)
dbexec < set-user-admin.sql
```

### Timezone-Fixes (nur falls Probleme mit Zeitzonen)
```bash
# Worklog Timezone korrigieren
dbexec < fix-worklog-timezone.sql
```

### Overtime Offset Update
```bash
dbexec < update-overtime-offset.sql
```

### Wednesday-Fix (nur falls benötigt)
```bash
dbexec < add-missing-wednesday.sql
```

## Überprüfung

```bash
# Zeige alle Tabellen
dbexec -e "SHOW TABLES;"

# Zeige timewish-Struktur
dbexec -e "DESCRIBE timewish;"

# Zeige Anzahl User
dbexec -e "SELECT COUNT(*) AS user_count FROM users;"

# Zeige deine Timewishes
dbexec -e "SELECT * FROM timewish WHERE user_id = 1 ORDER BY day, start_date;"
```

## Nach SQL-Ausführung: Backend neu starten

```bash
# Backend neu starten damit es die neuen Tabellen/Spalten erkennt
pm2 restart timeclock-backend

# Logs prüfen
pm2 logs timeclock-backend --lines 30

# Sollte zeigen:
# 🕐 TimeClock Server läuft auf Port 3010
# 📍 API verfügbar unter http://localhost:3010/api
```

## Schnell-Befehl (alle wichtigen Scripts):

```bash
cd /var/www/timeclock/backend

# DB-Credentials setzen
DB_HOST=$(grep DB_HOST .env | cut -d= -f2)
DB_USER=$(grep DB_USER .env | cut -d= -f2)
DB_PASSWORD=$(grep DB_PASSWORD .env | cut -d= -f2)
DB_NAME=$(grep DB_NAME .env | cut -d= -f2)

echo "Führe SQL-Scripts aus..."

# 1. Basis-Schema
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < database-schema.sql 2>&1 | grep -v "already exists" || true

# 2. Zusätzliche Tabellen
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < create-invitation-table.sql 2>&1 | grep -v "already exists" || true
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < create-watcher-table.sql 2>&1 | grep -v "already exists" || true

# 3. Indices
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < add-sick-index.sql 2>&1 || true
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < add-vacation-index.sql 2>&1 || true

# 4. Timewish (korrigiert)
mysql -h $DB_HOST -u $DB_USER -p"$DB_PASSWORD" $DB_NAME < setup-timewish-complete.sql

echo "✅ SQL-Scripts ausgeführt!"

# Backend neu starten
pm2 restart timeclock-backend

echo "✅ Backend neu gestartet!"
```

Kopiere diesen Schnell-Befehl auf den Server und führe ihn aus! 🚀

