From 4e6406b7defa4c41db9bb94c1586118ef7c41a35 Mon Sep 17 00:00:00 2001 From: Jakub Mikita Date: Sun, 2 Jun 2024 12:31:45 +0200 Subject: [PATCH] feat: upgrader to data v3 --- src/Core/Upgrade.php | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Core/Upgrade.php b/src/Core/Upgrade.php index 1c432452..8501d83d 100644 --- a/src/Core/Upgrade.php +++ b/src/Core/Upgrade.php @@ -26,7 +26,7 @@ class Upgrade * * @var int */ - public static $dataVersion = 2; + public static $dataVersion = 3; /** * Version of database tables @@ -302,10 +302,8 @@ public function upgradeToV2() ); foreach ($notifications as $notificationRaw) { - $data = json_decode( - $notificationRaw->postContent, - true - ); + // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + $data = json_decode($notificationRaw->post_content, true); $data['trigger'] = preg_replace( array_keys($this->triggerSlugReplacements()), @@ -338,4 +336,40 @@ public function upgradeToV2() ['%s'] ); } + + /** + * Upgrades data to v3. + * - 1. Moves the notifications to custom table. + * + * @since [Next] + * @return void + */ + public function upgradeToV3() + { + $db = DatabaseService::db(); + + // 1. Moves the notifications to custom table. + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching + $notifications = (array)$db->get_results( + "SELECT p.ID, p.post_content + FROM {$db->posts} p + WHERE p.post_type = 'notification' AND p.post_content<>''" + ); + + foreach ($notifications as $notificationRaw) { + try { + // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + $notification = Notification::from('json', $notificationRaw->post_content); + } catch (\Throwable $e) { + continue; + } + + if (! $notification instanceof Notification) { + continue; + } + + NotificationDatabaseService::upsert($notification); + } + } }