Skip to content

Commit

Permalink
Check for column existence
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Steffens <florian.steffens@nextcloud.com>
  • Loading branch information
Florian Steffens committed Jan 22, 2024
1 parent 11df530 commit c9a6b66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
33 changes: 30 additions & 3 deletions lib/Db/LegacyRowMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,49 @@ public function findByView(int $id, View $view): LegacyRow {
* @throws InternalError
*/
public function transferLegacyRow(LegacyRow $legacyRow, array $columns) {
$this->rowMapper->insert($this->migrateLegacyRow($legacyRow), $columns);
$this->rowMapper->insert($this->migrateLegacyRow($legacyRow, $columns), $columns);
}

/**
* @param LegacyRow $legacyRow
* @param Column[] $columns
* @return Row2
*/
public function migrateLegacyRow(LegacyRow $legacyRow): Row2 {
public function migrateLegacyRow(LegacyRow $legacyRow, array $columns): Row2 {
$row = new Row2();
$row->setId($legacyRow->getId());
$row->setTableId($legacyRow->getTableId());
$row->setCreatedBy($legacyRow->getCreatedBy());
$row->setCreatedAt($legacyRow->getCreatedAt());
$row->setLastEditBy($legacyRow->getLastEditBy());
$row->setLastEditAt($legacyRow->getLastEditAt());
$row->setData($legacyRow->getDataArray());

$legacyData = $legacyRow->getDataArray();
$data = [];
foreach ($legacyData as $legacyDatum) {
$columnId = $legacyDatum['columnId'];
if ($this->getColumnFromColumnsArray($columnId, $columns)) {
$data[] = $legacyDatum;
} else {
$this->logger->warning("The row with id " . $row->getId() . " has a value for the column with id " . $columnId . ". But this column does not exist or is not part of the table " . $row->getTableId() . ". Will ignore this value abd continue.");
}
}
$row->setData($data);

return $row;
}

/**
* @param int $columnId
* @param Column[] $columns
* @return Column|null
*/
private function getColumnFromColumnsArray(int $columnId, array $columns): ?Column {
foreach ($columns as $column) {
if($column->getId() === $columnId) {
return $column;
}
}
return null;
}
}
12 changes: 6 additions & 6 deletions lib/Db/Row2Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,6 @@ public function isRowInViewPresent(int $rowId, View $view, string $userId): bool
* @throws Exception
*/
public function insert(Row2 $row, array $columns): Row2 {
if(!$columns) {
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': columns are missing');
}
$this->setColumns($columns);

if($row->getId()) {
Expand All @@ -489,9 +486,12 @@ public function insert(Row2 $row, array $columns): Row2 {
$row->setId($rowSleeve->getId());
}

// write all cells to its db-table
foreach ($row->getData() as $cell) {
$this->insertCell($rowSleeve->getId(), $cell['columnId'], $cell['value'], $rowSleeve->getLastEditAt(), $rowSleeve->getLastEditBy());
// if the table/view has columns
if (count($columns) > 0) {
// write all cells to its db-table
foreach ($row->getData() as $cell) {
$this->insertCell($rowSleeve->getId(), $cell['columnId'], $cell['value'], $rowSleeve->getLastEditAt(), $rowSleeve->getLastEditBy());
}
}

return $row;
Expand Down

0 comments on commit c9a6b66

Please sign in to comment.