Skip to content

Commit 4748011

Browse files
committed
enh: set UUID for selection options on migration
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 4ff6b70 commit 4748011

4 files changed

Lines changed: 58 additions & 16 deletions

File tree

lib/Db/Column.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public static function fromDto(ColumnDto $data): self {
262262
$column->setNumberDecimals($data->getNumberDecimals());
263263
$column->setNumberPrefix($data->getNumberPrefix() ?? '');
264264
$column->setNumberSuffix($data->getNumberSuffix() ?? '');
265-
$column->setSelectionOptionsCollection(SelectionOptions::createFromInputJsonString($data->getSelectionOptions() ?? '[]', $data->getSelectionDefault()));
265+
$column->setSelectionOptionsCollection(SelectionOptions::createFromInputJsonString($data->getSelectionOptions() ?? '[]', $data->getSelectionDefault(), true));
266266
$column->setDatetimeDefault($data->getDatetimeDefault());
267267
$column->setUsergroupDefault($data->getUsergroupDefault());
268268
$column->setUsergroupMultipleItems($data->getUsergroupMultipleItems());
@@ -289,7 +289,7 @@ public function setUsergroupDefaultArray(array $array):void {
289289
}
290290

291291
public function getSelectionOptionsCollection(): SelectionOptions {
292-
return SelectionOptions::createFromInputJsonString($this->getSelectionOptions() ?? '[]', $this->getSelectionDefault());
292+
return SelectionOptions::createFromInputJsonString($this->getSelectionOptions() ?? '[]', $this->getSelectionDefault(), true);
293293
}
294294

295295
public function setSelectionOptionsCollection(SelectionOptions $selectionOptions): void {

lib/Migration/Version2020Date20260513185340.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Closure;
1313
use OCA\Tables\Vendor\Symfony\Component\Uid\Uuid;
1414
use OCP\DB\ISchemaWrapper;
15+
use OCP\DB\QueryBuilder\IQueryBuilder;
1516
use OCP\DB\Types;
1617
use OCP\IDBConnection;
1718
use OCP\Migration\IOutput;
@@ -22,6 +23,7 @@ class Version2020Date20260513185340 extends SimpleMigrationStep {
2223
private const TARGET_TABLE = 'tables_columns';
2324
private const COL_ID = 'id';
2425
private const COL_UUID = 'uuid';
26+
private const COL_SELECTION_OPTIONS = 'selection_options';
2527
private const INDEX_NAME = 'tables_col_uuid_uniq';
2628

2729
public function __construct(
@@ -54,16 +56,52 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
5456
return $schema;
5557
}
5658

59+
private function applyColumnOptionsUpdateIfNecessary(IQueryBuilder $query, int $columnId, ?string $rawSelectionOptions): void {
60+
$columnSelectionOptions = trim($rawSelectionOptions ?? '');
61+
if ($columnSelectionOptions === '') {
62+
return;
63+
}
64+
65+
$selectionOptions = \json_decode($columnSelectionOptions, true);
66+
if (!is_array($selectionOptions) || empty($selectionOptions)) {
67+
return;
68+
}
69+
70+
foreach ($selectionOptions as &$selectionOption) {
71+
if (!isset($selectionOption['uuid'])) {
72+
$selectionOption['uuid'] = Uuid::v7()->toRfc4122();
73+
}
74+
}
75+
76+
$updatedSelectionOptions = json_encode($selectionOptions);
77+
unset($selectionOption);
78+
$query->setParameters(
79+
[
80+
'columnLocalId' => $columnId,
81+
'columnSelectionOptions' => $updatedSelectionOptions,
82+
],
83+
[
84+
Types::INTEGER,
85+
Types::TEXT,
86+
]
87+
);
88+
$query->executeStatement();
89+
}
90+
5791
#[Override]
5892
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
93+
$qbColUuidUpdate = $this->db->getQueryBuilder();
94+
$qbColUuidUpdate->update(self::TARGET_TABLE)
95+
->set(self::COL_UUID, $qbColUuidUpdate->createParameter('columnUuid'))
96+
->where($qbColUuidUpdate->expr()->eq(self::COL_ID, $qbColUuidUpdate->createParameter('columnLocalId')));
5997

60-
$qbUpdate = $this->db->getQueryBuilder();
61-
$qbUpdate->update(self::TARGET_TABLE)
62-
->set(self::COL_UUID, $qbUpdate->createParameter('columnUuid'))
63-
->where($qbUpdate->expr()->eq(self::COL_ID, $qbUpdate->createParameter('columnLocalId')));
98+
$qbColOptionsUuidUpdate = $this->db->getQueryBuilder();
99+
$qbColOptionsUuidUpdate->update(self::TARGET_TABLE)
100+
->set(self::COL_SELECTION_OPTIONS, $qbColOptionsUuidUpdate->createParameter('columnSelectionOptions'))
101+
->where($qbColOptionsUuidUpdate->expr()->eq(self::COL_ID, $qbColOptionsUuidUpdate->createParameter('columnLocalId')));
64102

65103
$qbSelect = $this->db->getQueryBuilder();
66-
$qbSelect->select(self::COL_ID)
104+
$qbSelect->select(self::COL_ID, self::COL_SELECTION_OPTIONS)
67105
->from(self::TARGET_TABLE);
68106
$select = $qbSelect->executeQuery();
69107

@@ -72,8 +110,9 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
72110

73111
try {
74112
$this->db->beginTransaction();
75-
while (($columnId = $select->fetchOne()) !== false) {
76-
$qbUpdate->setParameters(
113+
while (($columnData = $select->fetchAssociative()) !== false) {
114+
$columnId = $columnData[self::COL_ID];
115+
$qbColUuidUpdate->setParameters(
77116
[
78117
'columnLocalId' => (int)$columnId,
79118
'columnUuid' => Uuid::v7()->toRfc4122(),
@@ -83,7 +122,10 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
83122
Types::STRING,
84123
]
85124
);
86-
$qbUpdate->executeStatement();
125+
$qbColUuidUpdate->executeStatement();
126+
127+
$this->applyColumnOptionsUpdateIfNecessary($qbColOptionsUuidUpdate, (int)$columnId, $columnData[self::COL_SELECTION_OPTIONS]);
128+
87129
$updates++;
88130
if ($updates % $writeBatches === 0) {
89131
$this->db->commit();

lib/Model/SelectionOption.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
) {
1818
}
1919

20-
public static function createFromInputArray(array $data): self {
20+
public static function createFromInputArray(array $data, bool $allowPassingUuid = false): self {
2121
if (!isset($data['id']) || !is_numeric($data['id'])) {
2222
throw new \InvalidArgumentException('Only integer keys are allowed for options');
2323
}
@@ -26,7 +26,7 @@ public static function createFromInputArray(array $data): self {
2626
throw new \InvalidArgumentException('Option label is missing');
2727
}
2828

29-
if (isset($data['uuid'])) {
29+
if (isset($data['uuid']) && !$allowPassingUuid) {
3030
throw new \InvalidArgumentException('It is forbidden to set the Uuid from external');
3131
}
3232

lib/Model/SelectionOptions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ private function applyArrayDefault(): void {
7171
$this->default = $confirmedOptions;
7272
}
7373

74-
public static function createFromInputArray(?array $data, null|bool|int|string $default): self {
74+
public static function createFromInputArray(?array $data, null|bool|int|string $default, bool $allowPassingUuid = false): self {
7575
if ($data !== null) {
7676
$selectionOptions = [];
7777
foreach ($data as $inputSelectionOption) {
78-
$selectionOptions[] = SelectionOption::createFromInputArray($inputSelectionOption);
78+
$selectionOptions[] = SelectionOption::createFromInputArray($inputSelectionOption, $allowPassingUuid);
7979
}
8080
}
8181
// `check` subtype has null as options
8282
return new self($selectionOptions ?? null, $default);
8383
}
8484

85-
public static function createFromInputJsonString(?string $data, null|bool|int|string $default): self {
85+
public static function createFromInputJsonString(?string $data, null|bool|int|string $default, bool $allowPassingUuid = false): self {
8686
if ($data !== null && $data !== 'null') {
8787
$inputArray = \json_decode($data === '' ? '[]' : $data, true);
8888
if (!is_array($inputArray)) {
@@ -92,7 +92,7 @@ public static function createFromInputJsonString(?string $data, null|bool|int|st
9292
// `check` subtype has "null" as options
9393
$inputArray = null;
9494
}
95-
return self::createFromInputArray($inputArray, $default);
95+
return self::createFromInputArray($inputArray, $default, $allowPassingUuid);
9696
}
9797

9898
public function default(): mixed {

0 commit comments

Comments
 (0)