Skip to content

Commit

Permalink
Convert columns to json
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Feb 6, 2024
1 parent 0a85d84 commit b2173fb
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 7 deletions.
36 changes: 36 additions & 0 deletions src/migrations/m240206_035135_convert_json_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m240206_035135_convert_json_columns migration.
*/
class m240206_035135_convert_json_columns extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->alterColumn(Table::DEPRECATIONERRORS, 'traces', $this->json());
$this->alterColumn(Table::FIELDLAYOUTS, 'config', $this->json());
$this->alterColumn(Table::GQLSCHEMAS, 'scope', $this->json());
$this->alterColumn(Table::SECTIONS, 'previewTargets', $this->json());
$this->alterColumn(Table::USERPREFERENCES, 'preferences', $this->json());
$this->alterColumn(Table::WIDGETS, 'settings', $this->json());

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m240206_035135_convert_json_columns cannot be reverted.\n";
return false;
}
}
10 changes: 10 additions & 0 deletions src/models/DeprecationError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\models;

use craft\base\Model;
use craft\helpers\Json;
use craft\validators\DateTimeValidator;
use DateTime;

Expand Down Expand Up @@ -59,6 +60,15 @@ class DeprecationError extends Model
*/
public ?array $traces = null;

public function __construct($config = [])
{
if (is_string($config['traces'] ?? null)) {
$config['traces'] = Json::decode($config['traces']);
}

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down
10 changes: 10 additions & 0 deletions src/records/GqlSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use craft\db\ActiveRecord;
use craft\db\Table;
use craft\helpers\Json;

/**
* Class GqlSchema record.
Expand All @@ -30,4 +31,13 @@ public static function tableName(): string
{
return Table::GQLSCHEMAS;
}

public function __construct($config = [])
{
if (is_string($config['scope'] ?? null)) {
$config['scope'] = Json::decode($config['scope']);
}

parent::__construct($config);
}
}
5 changes: 5 additions & 0 deletions src/services/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\events\WidgetEvent;
use craft\helpers\Component as ComponentHelper;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\records\Widget as WidgetRecord;
use craft\widgets\CraftSupport as CraftSupportWidget;
use craft\widgets\Feed as FeedWidget;
Expand Down Expand Up @@ -122,6 +123,10 @@ public function createWidget(mixed $config): WidgetInterface
$config = ['type' => $config];
}

if (is_string($config['settings'] ?? null)) {

Check failure on line 126 in src/services/Dashboard.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Call to function is_string() with null will always evaluate to false.

Check failure on line 126 in src/services/Dashboard.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Offset 'settings' on array{type: class-string<T of craft\base\WidgetInterface>} on left side of ?? does not exist.
$config['settings'] = Json::decode($config['settings']);
}

try {
$widget = ComponentHelper::createComponent($config, WidgetInterface::class);
} catch (MissingComponentException $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/Deprecator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function storeLogs(): void
'file' => $log->file,
'line' => $log->line,
'message' => $log->message,
'traces' => Json::encode($log->traces),
'traces' => $log->traces,
]);
$log->id = (int)$db->getLastInsertID();
} catch (Exception $e) {
Expand Down
4 changes: 3 additions & 1 deletion src/services/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ private function _sections(): MemoizableArray

$this->_sections = new MemoizableArray($results, function(array $result) use (&$siteSettingsBySection) {
if (!empty($result['previewTargets'])) {
$result['previewTargets'] = Json::decode($result['previewTargets']);
$result['previewTargets'] = is_string($result['previewTargets'])
? Json::decode($result['previewTargets'])
: $result['previewTargets'];
} else {
$result['previewTargets'] = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ private function _layouts(): MemoizableArray
if (array_key_exists('config', $config)) {
$nestedConfig = ArrayHelper::remove($config, 'config');
if ($nestedConfig) {
$config += Json::decode($nestedConfig);
$config += is_string($nestedConfig) ? Json::decode($nestedConfig) : $nestedConfig;
}
$loadTabs = false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/services/Gql.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ public function handleChangedSchema(ConfigEvent $event): void
$schemaRecord->uid = $schemaUid;
$schemaRecord->name = $data['name'];
$schemaRecord->isPublic = (bool)($data['isPublic'] ?? false);
$schemaRecord->scope = (!empty($data['scope']) && is_array($data['scope'])) ? Json::encode($data['scope']) : [];
$schemaRecord->scope = empty($data['scope']) ? [] : $data['scope'];

// Save the schema record
$schemaRecord->save(false);
Expand Down
8 changes: 5 additions & 3 deletions src/services/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,11 @@ public function getUserPreferences(int $userId): array
->select(['preferences'])
->from([Table::USERPREFERENCES])
->where(['userId' => $userId])
->scalar();
->scalar() ?: [];

$this->_userPreferences[$userId] = $preferences ? Json::decode($preferences) : [];
$this->_userPreferences[$userId] = is_string($preferences)
? Json::decode($preferences)
: $preferences;
}

return $this->_userPreferences[$userId];
Expand All @@ -390,7 +392,7 @@ public function saveUserPreferences(User $user, array $preferences): void

Db::upsert(Table::USERPREFERENCES, [
'userId' => $user->id,
'preferences' => Json::encode($preferences),
'preferences' => $preferences,
]);

$this->_userPreferences[$user->id] = $preferences;
Expand Down

0 comments on commit b2173fb

Please sign in to comment.