* @param list > $columns * @return array */
(int $datasetId, int $expectedSchemaVersion, array $columns, ?string $name = null)
| 71 | * @return array<string,mixed> |
| 72 | */ |
| 73 | public function updateSchema(int $datasetId, int $expectedSchemaVersion, array $columns, ?string $name = null): array { |
| 74 | $resolved = $this->requireFlexible($datasetId, true); |
| 75 | if ($name !== null && (trim($name) === '' || mb_strlen(trim($name)) > 64)) { |
| 76 | throw new FlexibleStorageException('invalid_dataset_name', 'Dataset names must contain between 1 and 64 characters.', ['field' => 'name']); |
| 77 | } |
| 78 | $currentVersion = (int)($resolved['dataset']['schema_version'] ?? 1); |
| 79 | if ($expectedSchemaVersion !== $currentVersion) { |
| 80 | throw new FlexibleStorageException('stale_schema', 'The dataset schema changed after it was loaded.', [ |
| 81 | 'expectedSchemaVersion' => $expectedSchemaVersion, |
| 82 | 'currentSchemaVersion' => $currentVersion, |
| 83 | ], 409); |
| 84 | } |
| 85 | |
| 86 | $normalized = $this->normalizeColumns($columns); |
| 87 | $current = $this->storageMapper->getColumns($datasetId); |
| 88 | $currentById = array_column($current, null, 'id'); |
| 89 | $count = (int)$this->storageMapper->getRecordCount($datasetId)['count']; |
| 90 | $seen = []; |
| 91 | foreach ($normalized as $position => &$column) { |
| 92 | $requestedRef = $columns[$position]['ref'] ?? null; |
| 93 | if ($requestedRef === null) { |
| 94 | if ($count > 0 && ($column['role'] !== 'measure' || $column['nullable'] !== true)) { |
| 95 | throw new FlexibleStorageException('schema_change_not_allowed', 'Only nullable measure columns can be added to a populated dataset.', ['position' => $position], 409); |
| 96 | } |
| 97 | $column['id'] = null; |
| 98 | continue; |
| 99 | } |
| 100 | $existing = (new FlexibleColumnResolver($current))->resolve((string)$requestedRef); |
| 101 | $column['id'] = (int)$existing['id']; |
| 102 | if (isset($seen[$column['id']])) { |
| 103 | throw new FlexibleStorageException('duplicate_column_reference', 'Each existing column reference may appear only once.', ['column' => $existing['ref']]); |
| 104 | } |
| 105 | $seen[$column['id']] = true; |
| 106 | if ($count > 0 && ( |
| 107 | $column['type'] !== $existing['type'] |
| 108 | || $column['role'] !== $existing['role'] |
| 109 | || $column['nullable'] !== $existing['nullable'] |
| 110 | )) { |
| 111 | throw new FlexibleStorageException('schema_change_not_allowed', 'Column type, role, and nullability can change only while the dataset is empty.', ['column' => $existing['ref']], 409); |
| 112 | } |
| 113 | } |
| 114 | unset($column); |
| 115 | if ($count > 0 && count($seen) !== count($currentById)) { |
| 116 | throw new FlexibleStorageException('schema_change_not_allowed', 'Columns cannot be removed from a populated dataset.', [], 409); |
| 117 | } |
| 118 | |
| 119 | $this->storageMapper->beginTransaction(); |
| 120 | try { |
| 121 | foreach ($normalized as $column) { |
| 122 | if ($column['id'] === null) { |
| 123 | $this->storageMapper->createColumn($datasetId, $column); |
| 124 | } else { |
| 125 | $this->storageMapper->updateColumn($datasetId, $column['id'], $column); |
| 126 | } |
| 127 | } |
| 128 | foreach ($currentById as $columnId => $column) { |
| 129 | if (!isset($seen[$columnId])) { |
| 130 | $this->storageMapper->deleteColumn($datasetId, (int)$columnId); |
no test coverage detected