# Migration Guide - Von altem zu neuem Domain System ## Übersicht Dieser Guide führt Sie durch die Migration vom aktuellen Domain-System zum neuen optimierten System. Die Migration erfolgt schrittweise, um Risiken zu minimieren. ## Vor der Migration ### 1. Backup erstellen ```bash # Datenbank Backup mysqldump mivita > mivita_backup_$(date +%Y%m%d_%H%M%S).sql # Code Backup cp -r app/ app_backup_$(date +%Y%m%d_%H%M%S)/ ``` ### 2. Tests vorbereiten ```bash # Bestehende Tests ausführen php artisan test # Domain-spezifische Tests erstellen php artisan make:test DomainRoutingTest php artisan make:test SessionDomainSwitchingTest ``` ### 3. Neue Dateien bereitstellen ```bash # Neue Klassen kopieren cp dev/subdomain-optimization-grok/src/Domain/DomainType.php app/Domain/ cp dev/subdomain-optimization-grok/src/Domain/DomainContext.php app/Domain/ cp dev/subdomain-optimization-grok/src/Middleware/DomainResolver.php app/Http/Middleware/ cp dev/subdomain-optimization-grok/src/Middleware/DomainSessionHandler.php app/Http/Middleware/ cp dev/subdomain-optimization-grok/src/Services/DomainService.php app/Services/ cp dev/subdomain-optimization-grok/src/Services/DomainCacheManager.php app/Services/ cp dev/subdomain-optimization-grok/src/Providers/RouteServiceProvider.php app/Providers/ cp dev/subdomain-optimization-grok/src/Events/DomainChangedEvent.php app/Events/ ``` ## Schritt-für-Schritt Migration ### Schritt 1: Neue Middleware registrieren (Test-Environment) **Datei: `app/Http/Kernel.php`** ```php 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, // NEUE MIDDLEWARE (nach Cookies, vor Session) \App\Http\Middleware\DomainResolver::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, // NEUE MIDDLEWARE (nach Session) \App\Http\Middleware\DomainSessionHandler::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \App\Http\Middleware\Localization::class, ], ``` ### Schritt 2: DomainServiceProvider aktualisieren **Datei: `app/Providers/DomainServiceProvider.php`** ```php public function register() { // DomainService registrieren (bleibt gleich) $this->app->singleton(DomainService::class, function ($app) { $domainService = new DomainService($app['config']['domains']); // ... bestehende Validierung return $domainService; }); // DomainContext registrieren (AKTUALISIERT) $this->app->singleton(DomainContext::class, function ($app) { /** @var DomainService $domainService */ $domainService = $app->make(DomainService::class); $request = $app->make('request'); $domainInfo = $domainService->parseDomain($request->getHost()); $userShop = null; if ($domainInfo['type'] === 'user-shop' && $domainInfo['subdomain']) { $userShop = $domainService->getUserShop($domainInfo['subdomain']); if (!$userShop) { $domainInfo['type'] = 'unknown'; } } if ($domainInfo['type'] === 'main-shop' && $domainInfo['default_user_shop']) { $userShop = $domainService->getUserShop($domainInfo['default_user_shop']); } // NEU: DomainContext::fromArray verwenden return DomainContext::fromArray($domainInfo, $userShop); }); } ``` ### Schritt 3: RouteServiceProvider aktualisieren **Datei: `app/Providers/RouteServiceProvider.php`** ```php protected function loadDomainAwareRoutes(): void { /** @var DomainContext $context */ $context = app(DomainContext::class); if (config('app.debug')) { \Log::channel('domain')->info('loadDomainAwareRoutes', ['context' => $context]); } // NEUE DOMAIN-TYPES verwenden match ($context->type) { 'main' => $this->loadDomainRoutes('main', 'main.php'), 'shop' => $this->loadDomainRoutes('shop', 'shop.php'), 'crm' => $this->loadDomainRoutes('crm', 'crm.php'), 'portal' => $this->loadDomainRoutes('portal', 'portal.php'), 'checkout' => $this->loadDomainRoutes('checkout', 'checkout.php'), 'user-shop' => [ $this->loadDomainRoutes('user-shop', 'user-shop.php'), $this->loadDomainRoutes('portal', 'portal.php'), ], default => $this->loadAllDomainRoutesForCaching(), }; } ``` ### Schritt 4: Event-ServiceProvider registrieren **Datei: `config/app.php`** ```php 'providers' => [ // ... andere Provider App\Providers\EventServiceProvider::class, ], ``` **Datei: `app/Providers/EventServiceProvider.php`** ```php protected $listen = [ \App\Events\DomainChangedEvent::class => [ // Optional: Listener für Domain-Änderungen // \App\Listeners\DomainChangedListener::class, ], ]; ``` ### Schritt 5: Konfiguration aktualisieren **Datei: `config/domains.php`** (keine Änderungen nötig) ### Schritt 6: Tests durchführen ```bash # Grundfunktionalität testen php artisan tinker ``` ```php // Domain-Auflösung testen $domainService = app(\App\Services\DomainService::class); $context = $domainService->resolveDomain('my.mivita.care'); echo $context; // Sollte DomainType::CRM zeigen // UserShop testen $userShop = $domainService->getUserShop('aloevera'); echo $userShop?->name ?? 'UserShop nicht gefunden'; ``` ## Rollback-Plan Falls Probleme auftreten: ### Schnell-Rollback ```bash # Kernel.php zurücksetzen git checkout HEAD -- app/Http/Kernel.php # Neue Middleware auskommentieren # \App\Http\Middleware\DomainResolver::class, # \App\Http\Middleware\DomainSessionHandler::class, ``` ### Vollständiges Rollback ```bash # Alle neuen Dateien entfernen rm app/Domain/DomainType.php rm app/Domain/DomainContext.php rm app/Http/Middleware/DomainResolver.php rm app/Http/Middleware/DomainSessionHandler.php rm app/Services/DomainCacheManager.php rm app/Events/DomainChangedEvent.php # Alte Dateien wiederherstellen git checkout HEAD -- app/Providers/DomainServiceProvider.php git checkout HEAD -- app/Providers/RouteServiceProvider.php ``` ## Nach der Migration ### 1. Cache leeren ```bash php artisan cache:clear php artisan config:clear php artisan route:clear ``` ### 2. Route-Cache neu aufbauen ```bash php artisan route:cache ``` ### 3. Tests ausführen ```bash # Vollständige Test-Suite php artisan test # Domain-spezifische Tests php artisan test --filter=Domain ``` ### 4. Monitoring einrichten ```bash # Domain-Logs überwachen tail -f storage/logs/domain-*.log # Performance überwachen php artisan tinker ``` ```php $cacheManager = app(\App\Services\DomainCacheManager::class); $stats = $cacheManager->getCacheStatistics(); $health = $cacheManager->performHealthCheck(); ``` ## Häufige Probleme und Lösungen ### Problem 1: Session-Konflikte **Symptom:** Zwei Sessions werden erstellt **Lösung:** - Sicherstellen, dass DomainResolver NACH Cookies aber VOR Session läuft - DomainSessionHandler NACH Session-Start ausführen ### Problem 2: UserShop wird nicht beibehalten **Symptom:** Beim Wechsel von Shop zu in. geht UserShop verloren **Lösung:** - Überprüfen, dass `shouldPreserveSession()` korrekt implementiert ist - DomainSessionHandler muss UserShop-Daten korrekt setzen ### Problem 3: Performance-Probleme **Symptom:** Langsame Domain-Auflösung **Lösung:** ```bash # Cache leeren php artisan cache:clear # Cache vorwärmen php artisan tinker $cacheManager = app(\App\Services\DomainCacheManager::class); $cacheManager->warmupCache(['mivita.care', 'my.mivita.care'], ['aloevera']); ``` ### Problem 4: Route-Loading Fehler **Symptom:** Falsche Routen werden geladen **Lösung:** - Route-Cache leeren: `php artisan route:clear` - Domain-Kontext überprüfen im Debug-Modus - Route-Dateien auf Existenz prüfen ## Performance-Optimierungen ### Cache-Konfiguration **Datei: `config/cache.php`** ```php 'stores' => [ 'redis' => [ 'driver' => 'redis', 'connection' => 'cache', 'lock_connection' => 'default', ], ], ``` ### Queue für Cache-Operationen ```bash # Cache-Operationen asynchron verarbeiten php artisan queue:work ``` ## Monitoring und Alerting ### Log-Konfiguration **Datei: `config/logging.php`** ```php 'channels' => [ 'domain' => [ 'driver' => 'daily', 'path' => storage_path('logs/domain.log'), 'level' => 'debug', 'days' => 30, ], ], ``` ### Health-Checks ```bash # Domain-System Health-Check curl https://mivita.care/health/domain ``` ## Support Bei Problemen: 1. Logs überprüfen: `storage/logs/domain-*.log` 2. Debug-Modus aktivieren: `.env APP_DEBUG=true` 3. Cache-Statistiken prüfen 4. Domain-Service direkt testen ## Zeitplan - **Tag 1:** Neue Dateien bereitstellen und testen - **Tag 2:** Middleware-Migration in Test-Environment - **Tag 3:** Vollständige Migration in Staging - **Tag 4:** Production-Deployment mit Rollback-Plan - **Tag 5:** Monitoring und Optimierung ## Checklisten ### Pre-Migration ✅ - [ ] Backup erstellt - [ ] Tests vorbereitet - [ ] Neue Dateien bereitgestellt - [ ] Rollback-Plan dokumentiert ### Post-Migration ✅ - [ ] Cache geleert - [ ] Routes gecacht - [ ] Tests bestanden - [ ] Monitoring aktiv - [ ] Performance baseline etabliert