Skip to content

Commit

Permalink
feat: upgrader to data v3
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed Jun 2, 2024
1 parent a5f4eca commit 4e6406b
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/Core/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Upgrade
*
* @var int
*/
public static $dataVersion = 2;
public static $dataVersion = 3;

/**
* Version of database tables
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 4e6406b

Please sign in to comment.