From a18c2e0f55d8ca36b35f8f3e688f4202fde69728 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 23 Dec 2024 15:25:07 +0100 Subject: [PATCH] feat: migrate to new encryption options --- ..._142402_update_email_encryption_values.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 database/migrations/2024_12_23_142402_update_email_encryption_values.php diff --git a/database/migrations/2024_12_23_142402_update_email_encryption_values.php b/database/migrations/2024_12_23_142402_update_email_encryption_values.php new file mode 100644 index 000000000..c21c910d1 --- /dev/null +++ b/database/migrations/2024_12_23_142402_update_email_encryption_values.php @@ -0,0 +1,130 @@ + 'starttls', + 'ssl' => 'tls', + '' => 'none', + ]; + + /** + * Run the migrations. + */ + public function up(): void + { + try { + DB::beginTransaction(); + + $instanceSettings = DB::table('instance_settings')->get(); + foreach ($instanceSettings as $setting) { + try { + if (array_key_exists($setting->smtp_encryption, $this->encryptionMappings)) { + DB::table('instance_settings') + ->where('id', $setting->id) + ->update([ + 'smtp_encryption' => $this->encryptionMappings[$setting->smtp_encryption], + ]); + } + } catch (\Exception $e) { + \Log::error('Failed to update instance_settings encryption for ID: '.$setting->id, [ + 'error' => $e->getMessage(), + 'old_value' => $setting->smtp_encryption, + ]); + } + } + + $emailSettings = DB::table('email_notification_settings')->get(); + foreach ($emailSettings as $setting) { + try { + if (array_key_exists($setting->smtp_encryption, $this->encryptionMappings)) { + DB::table('email_notification_settings') + ->where('id', $setting->id) + ->update([ + 'smtp_encryption' => $this->encryptionMappings[$setting->smtp_encryption], + ]); + } + } catch (\Exception $e) { + \Log::error('Failed to update email_notification_settings encryption for ID: '.$setting->id, [ + 'error' => $e->getMessage(), + 'old_value' => $setting->smtp_encryption, + ]); + } + } + + DB::commit(); + } catch (\Exception $e) { + DB::rollBack(); + \Log::error('Failed to complete email encryption migration', [ + 'error' => $e->getMessage(), + ]); + throw $e; + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + try { + DB::beginTransaction(); + + $reverseMapping = [ + 'starttls' => 'tls', + 'tls' => 'ssl', + 'none' => '', + ]; + + $instanceSettings = DB::table('instance_settings')->get(); + foreach ($instanceSettings as $setting) { + try { + if (array_key_exists($setting->smtp_encryption, $reverseMapping)) { + DB::table('instance_settings') + ->where('id', $setting->id) + ->update([ + 'smtp_encryption' => $reverseMapping[$setting->smtp_encryption], + ]); + } + } catch (\Exception $e) { + \Log::error('Failed to reverse instance_settings encryption for ID: '.$setting->id, [ + 'error' => $e->getMessage(), + 'old_value' => $setting->smtp_encryption, + ]); + } + } + + $emailSettings = DB::table('email_notification_settings')->get(); + foreach ($emailSettings as $setting) { + try { + if (array_key_exists($setting->smtp_encryption, $reverseMapping)) { + DB::table('email_notification_settings') + ->where('id', $setting->id) + ->update([ + 'smtp_encryption' => $reverseMapping[$setting->smtp_encryption], + ]); + } + } catch (\Exception $e) { + \Log::error('Failed to reverse email_notification_settings encryption for ID: '.$setting->id, [ + 'error' => $e->getMessage(), + 'old_value' => $setting->smtp_encryption, + ]); + } + } + + DB::commit(); + } catch (\Exception $e) { + DB::rollBack(); + \Log::error('Failed to complete email encryption rollback migration', [ + 'error' => $e->getMessage(), + ]); + throw $e; + } + } +}