Skip to content

Commit

Permalink
Merge pull request #14300 from craftcms/feature/cms-1254-convert-json…
Browse files Browse the repository at this point in the history
…-only-columns-to-json

Convert JSON columns
  • Loading branch information
brandonkelly authored Feb 6, 2024
2 parents 0280d64 + b0ebbfd commit e2db1bf
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.0.0-alpha.12',
'schemaVersion' => '5.0.0.17',
'schemaVersion' => '5.0.0.18',
'minVersionRequired' => '4.4.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
12 changes: 6 additions & 6 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function createTables(): void
'file' => $this->string()->notNull(),
'line' => $this->smallInteger()->unsigned(),
'message' => $this->text(),
'traces' => $this->text(),
'traces' => $this->json(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->uid(),
Expand Down Expand Up @@ -424,7 +424,7 @@ public function createTables(): void
$this->createTable(Table::FIELDLAYOUTS, [
'id' => $this->primaryKey(),
'type' => $this->string()->notNull(),
'config' => $this->text(),
'config' => $this->json(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'dateDeleted' => $this->dateTime()->null(),
Expand Down Expand Up @@ -471,7 +471,7 @@ public function createTables(): void
$this->createTable(Table::GQLSCHEMAS, [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'scope' => $this->text(),
'scope' => $this->json(),
'isPublic' => $this->boolean()->notNull()->defaultValue(false),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
Expand Down Expand Up @@ -558,7 +558,7 @@ public function createTables(): void
'maxAuthors' => $this->smallInteger()->unsigned()->defaultValue(1)->notNull(),
'propagationMethod' => $this->string()->defaultValue(PropagationMethod::All->value)->notNull(),
'defaultPlacement' => $this->enum('defaultPlacement', [Section::DEFAULT_PLACEMENT_BEGINNING, Section::DEFAULT_PLACEMENT_END])->defaultValue('end')->notNull(),
'previewTargets' => $this->text(),
'previewTargets' => $this->json(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'dateDeleted' => $this->dateTime()->null(),
Expand Down Expand Up @@ -714,7 +714,7 @@ public function createTables(): void
]);
$this->createTable(Table::USERPREFERENCES, [
'userId' => $this->primaryKey(),
'preferences' => $this->text(),
'preferences' => $this->json(),
]);
$this->createTable(Table::USERS, [
'id' => $this->integer()->notNull(),
Expand Down Expand Up @@ -792,7 +792,7 @@ public function createTables(): void
'type' => $this->string()->notNull(),
'sortOrder' => $this->smallInteger()->unsigned(),
'colspan' => $this->tinyInteger(),
'settings' => $this->text(),
'settings' => $this->json(),
'enabled' => $this->boolean()->defaultValue(true)->notNull(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
Expand Down
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: 0 additions & 10 deletions src/models/GqlSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace craft\models;

use craft\base\Model;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\records\GqlSchema as GqlSchemaRecord;
use craft\validators\UniqueValidator;
Expand Down Expand Up @@ -52,15 +51,6 @@ class GqlSchema extends Model
*/
private array $_cachedPairs = [];

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

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down
3 changes: 2 additions & 1 deletion src/services/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use craft\widgets\QuickPost as QuickPostWidget;
use craft\widgets\RecentEntries as RecentEntriesWidget;
use craft\widgets\Updates as UpdatesWidget;
use DateTime;
use Throwable;
use yii\base\Component;
use yii\base\Exception;
Expand Down Expand Up @@ -113,7 +114,7 @@ public function getAllWidgetTypes(): array
*
* @template T of WidgetInterface
* @param string|array $config The widget’s class name, or its config, with a `type` value and optionally a `settings` value.
* @phpstan-param class-string<T>|array{type:class-string<T>} $config
* @phpstan-param class-string<T>|array{type:class-string<T>,id?:int,dateCreated?:DateTime,dateUpdated?:DateTime,colspan?:int,settings?:array|string} $config
* @return T
*/
public function createWidget(mixed $config): WidgetInterface
Expand Down
4 changes: 2 additions & 2 deletions src/services/Deprecator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use craft\elements\db\ElementQuery;
use craft\errors\DeprecationException;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\helpers\Template;
use craft\models\DeprecationError;
Expand Down Expand Up @@ -139,6 +138,7 @@ private function _storeLogsInDb(): bool
public function storeLogs(): void
{
$db = Craft::$app->getDb();
$tableSchema = $db->getSchema()->getTableSchema(Table::DEPRECATIONERRORS);

foreach ($this->_pendingRequestLogs as $log) {
try {
Expand All @@ -149,7 +149,7 @@ public function storeLogs(): void
'file' => $log->file,
'line' => $log->line,
'message' => $log->message,
'traces' => Json::encode($log->traces),
'traces' => Db::prepareValueForDb($log->traces, $tableSchema->columns['traces']->dbType),
]);
$log->id = (int)$db->getLastInsertID();
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private function _sections(): MemoizableArray
}

$this->_sections = new MemoizableArray($results, function(array $result) use (&$siteSettingsBySection) {
if (!empty($result['previewTargets'])) {
if (!empty($result['previewTargets']) && is_string($result['previewTargets'])) {
$result['previewTargets'] = Json::decode($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
3 changes: 1 addition & 2 deletions src/services/Gql.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;
use craft\helpers\Gql as GqlHelper;
use craft\helpers\Json;
use craft\helpers\ProjectConfig as ProjectConfigHelper;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
Expand Down Expand Up @@ -992,7 +991,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 = ($data['scope'] ?? false) ?: [];

// Save the schema record
$schemaRecord->save(false);
Expand Down
13 changes: 11 additions & 2 deletions src/services/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,15 @@ public function getUserPreferences(int $userId): array
->where(['userId' => $userId])
->scalar();

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

$this->_userPreferences[$userId] = $preferences;
}

return $this->_userPreferences[$userId];
Expand All @@ -387,10 +395,11 @@ public function saveUserPreferences(User $user, array $preferences): void
{
// Merge in any other saved preferences
$preferences += $this->getUserPreferences($user->id);
$tableSchema = Craft::$app->getDb()->getSchema()->getTableSchema(Table::USERPREFERENCES);

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

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

0 comments on commit e2db1bf

Please sign in to comment.