Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Open Register drijft apps zoals OpenCatalogi, Procest, Pipelinq en Software Cata

Vrij en open source onder de EUPL-licentie.
]]></description>
<version>2.1.42-beta.20261102161558</version>
<version>2.1.42-beta.20261106120000</version>
<licence>EUPL-1.2</licence>
<author mail="info@conduction.nl" homepage="https://www.conduction.nl/">Conduction</author>
<namespace>OpenRegister</namespace>
Expand Down
10 changes: 5 additions & 5 deletions lib/Db/ImportPreviewRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
*
* @method int|null getPreviewId()
* @method void setPreviewId(?int $previewId)
* @method int getRowNumber()
* @method void setRowNumber(int $rowNumber)
* @method int getSourceRow()
* @method void setSourceRow(int $sourceRow)
* @method string|null getDecision()
* @method void setDecision(?string $decision)
* @method string|null getReason()
Expand Down Expand Up @@ -113,7 +113,7 @@ class ImportPreviewRow extends Entity implements JsonSerializable {
*
* @var integer
*/
protected int $rowNumber = 0;
protected int $sourceRow = 0;

/**
* The decision.
Expand Down Expand Up @@ -169,7 +169,7 @@ class ImportPreviewRow extends Entity implements JsonSerializable {
*/
public function __construct() {
$this->addType(fieldName: 'previewId', type: 'integer');
$this->addType(fieldName: 'rowNumber', type: 'integer');
$this->addType(fieldName: 'sourceRow', type: 'integer');
$this->addType(fieldName: 'decision', type: 'string');
$this->addType(fieldName: 'reason', type: 'string');
$this->addType(fieldName: 'targetUuid', type: 'string');
Expand Down Expand Up @@ -198,7 +198,7 @@ public function jsonSerialize(): array {
return [
'id' => $this->id,
'previewId' => $this->previewId,
'row' => $this->rowNumber,
'row' => $this->sourceRow,
'decision' => $this->decision,
'reason' => $this->reason,
'targetUuid' => $this->targetUuid,
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/ImportPreviewRowMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function findByPreview(int $previewId, ?string $decision = null, ?int $li
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('preview_id', $qb->createNamedParameter($previewId, IQueryBuilder::PARAM_INT)))
->orderBy('row_number', 'ASC');
->orderBy('source_row', 'ASC');

if ($decision !== null) {
$qb->andWhere($qb->expr()->eq('decision', $qb->createNamedParameter($decision)));
Expand Down Expand Up @@ -102,7 +102,7 @@ public function findPendingWrites(int $previewId, ?int $limit = null): array {
)
)
)
->orderBy('row_number', 'ASC');
->orderBy('source_row', 'ASC');

if ($limit !== null) {
$qb->setMaxResults($limit);
Expand Down
5 changes: 3 additions & 2 deletions lib/Migration/Version1Date20260915050000.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table = $schema->createTable('openregister_import_preview_rows');
$table->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'notnull' => true, 'unsigned' => true]);
$table->addColumn('preview_id', Types::BIGINT, ['notnull' => true, 'unsigned' => true]);
$table->addColumn('row_number', Types::INTEGER, ['notnull' => true, 'default' => 0, 'unsigned' => true]);
// `row_number` is a reserved word that Doctrine does not quote on MariaDB.
$table->addColumn('source_row', Types::INTEGER, ['notnull' => true, 'default' => 0, 'unsigned' => true]);
$table->addColumn('decision', Types::STRING, ['notnull' => true, 'length' => 16, 'default' => 'create']);
$table->addColumn('reason', Types::TEXT, ['notnull' => false]);
$table->addColumn('target_uuid', Types::STRING, ['notnull' => false, 'length' => 36]);
Expand All @@ -122,7 +123,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->setPrimaryKey(['id']);
$table->addIndex(['preview_id'], 'idx_or_improw_preview');
$table->addIndex(['preview_id', 'decision'], 'idx_or_improw_decision');
$table->addUniqueIndex(['preview_id', 'row_number'], 'idx_or_improw_unique');
$table->addUniqueIndex(['preview_id', 'source_row'], 'idx_or_improw_unique');

$output->info('Created openregister_import_preview_rows table');
}//end if
Expand Down
88 changes: 88 additions & 0 deletions lib/Migration/Version1Date20261106120000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* Rename openregister_import_preview_rows.row_number to source_row.
*
* `row_number` is a reserved word that Doctrine leaves unquoted on MariaDB, so
* the column could not be created there. Version1Date20260915050000 now creates
* `source_row`; this step brings the installs that already hold the old column
* in line, keeping their rows and the unique index.
*
* A fresh install takes Nextcloud's schema-only path and skips this step, which
* it does not need. Idempotent: it acts only while the old column exists.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Migration
* @package OCA\OpenRegister\Migration
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @link https://OpenRegister.app
*
* @spec openspec/changes/import-preview-and-conflict-policy/specs/data-import-export/spec.md
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Rename the reserved row_number column of the import preview rows.
*
* @spec openspec/changes/import-preview-and-conflict-policy/specs/data-import-export/spec.md
*/
class Version1Date20261106120000 extends SimpleMigrationStep {
private const TABLE = 'openregister_import_preview_rows';

/**
* Hold the connection the rename runs on.
*
* @param IDBConnection $connection The database connection.
*/
public function __construct(private readonly IDBConnection $connection) {
}//end __construct()

/**
* Rename the column before Nextcloud compares the schema.
*
* @param IOutput $output Migration output.
* @param Closure $schemaClosure Returns the schema wrapper.
* @param array<string, mixed> $options Migration options.
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter) The signature is Nextcloud's.
* @spec openspec/changes/import-preview-and-conflict-policy/specs/data-import-export/spec.md
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
/*
* @var ISchemaWrapper $schema
*/

$schema = $schemaClosure();
if ($schema->hasTable(self::TABLE) === false || $schema->getTable(self::TABLE)->hasColumn('row_number') === false) {
return;
}

$query = $this->connection->getQueryBuilder();
$this->connection->executeStatement(
sprintf(
'ALTER TABLE %s RENAME COLUMN %s TO %s',
$query->getTableName(self::TABLE),
$query->getColumnName('row_number'),
$query->getColumnName('source_row')
)
);
$output->info('Renamed row_number to source_row on '.self::TABLE);
}//end preSchemaChange()
}//end class
4 changes: 2 additions & 2 deletions lib/Service/Import/ImportPreviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ private function refusal(string $reason, array $payload = []): array {
private function persistDecision(ImportPreview $preview, array $row, array $decision): void {
$entity = new ImportPreviewRow();
$entity->setPreviewId((int)$preview->getId());
$entity->setRowNumber((int)$row['row']);
$entity->setSourceRow((int)$row['row']);
$entity->setDecision((string)$decision['decision']);
$entity->setReason($decision['reason']);
$entity->setTargetUuid($decision['targetUuid']);
Expand Down Expand Up @@ -689,7 +689,7 @@ private function buildReport(ImportPreview $preview): array {
decision: ImportPreviewRow::DECISION_REFUSE
) as $row) {
$refusals[] = [
'row' => $row->getRowNumber(),
'row' => $row->getSourceRow(),
'reason' => $row->getReason(),
'candidates' => ($row->getCandidates() ?? []),
];
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controller/ImportPreviewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function testTheRowsRouteReturnsTheDecisionsAndTheirReasons(): void {

$refused = new ImportPreviewRow();
$refused->setPreviewId(7);
$refused->setRowNumber(2);
$refused->setSourceRow(2);
$refused->setDecision(ImportPreviewRow::DECISION_REFUSE);
$refused->setReason('The match key hits more than one object: object-a, object-b.');
$refused->setCandidates(['object-a', 'object-b']);
Expand Down
110 changes: 110 additions & 0 deletions tests/Unit/Db/ImportPreviewRowMapperMigratedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* The import-preview row mapper against the table the app's migrations create.
*
* A mocked query builder accepts any column name, so the mapper could order by
* a column the table does not have. This test runs the mapper's queries as SQL
* against the migrated table: the source row number lives in `source_row`, not in
* `row_number`, which is reserved on MariaDB (#4909), and both readers return
* rows in source order.
*
* It needs a Nextcloud server's classes for the migrated table, like the
* other MigratedSqliteDatabase tests, so it runs in CI and not in a bare clone.
*
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
* SPDX-License-Identifier: EUPL-1.2
*
* @spec openspec/changes/import-preview-and-conflict-policy/specs/data-import-export/spec.md
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Tests\Unit\Db;

use OCA\OpenRegister\Db\ImportPreviewRow;
use OCA\OpenRegister\Db\ImportPreviewRowMapper;
use OCA\OpenRegister\Tests\Support\MigratedSqliteDatabase;
use PHPUnit\Framework\TestCase;

/**
* @covers \OCA\OpenRegister\Db\ImportPreviewRowMapper
* @uses \OCA\OpenRegister\Db\ImportPreviewRow
*/
class ImportPreviewRowMapperMigratedTest extends TestCase {

private MigratedSqliteDatabase $database;

private ImportPreviewRowMapper $mapper;

protected function setUp(): void {
$this->database = new MigratedSqliteDatabase($this, ['openregister_import_preview_rows']);
$this->mapper = new ImportPreviewRowMapper(db: $this->database->idbConnection());

// Stored out of source order, with one row of another preview.
$rows = [
['preview_id' => 7, 'source_row' => 3, 'decision' => ImportPreviewRow::DECISION_UPDATE],
['preview_id' => 7, 'source_row' => 1, 'decision' => ImportPreviewRow::DECISION_CREATE],
['preview_id' => 8, 'source_row' => 1, 'decision' => ImportPreviewRow::DECISION_CREATE],
['preview_id' => 7, 'source_row' => 2, 'decision' => ImportPreviewRow::DECISION_CREATE, 'applied_at' => '2026-11-06 10:00:00'],
];
foreach ($rows as $row) {
$this->database->insert('openregister_import_preview_rows', $row);
}
}//end setUp()

/**
* The row numbers of the given rows, in the order they came back.
*
* @param ImportPreviewRow[] $rows The rows.
*
* @return int[]
*/
private static function rowNumbers(array $rows): array {
return array_map(static fn (ImportPreviewRow $row): int => $row->getSourceRow(), $rows);
}//end rowNumbers()

/**
* The migrated table has the new column and not the reserved one.
*
* @return void
*/
public function testTheTableHasSourceRowAndNotRowNumber(): void {
$columns = array_map(
static fn ($column): string => trim($column->getName(), '"`'),
$this->database->connection()->createSchemaManager()->listTableColumns('openregister_import_preview_rows')
);

$this->assertContains('source_row', $columns);
$this->assertNotContains('row_number', $columns);
}//end testTheTableHasSourceRowAndNotRowNumber()

/**
* A preview's rows come back in source order, and only that preview's.
*
* @return void
*/
public function testFindByPreviewReturnsTheRowsInSourceOrder(): void {
$this->assertSame([1, 2, 3], self::rowNumbers($this->mapper->findByPreview(previewId: 7)));
}//end testFindByPreviewReturnsTheRowsInSourceOrder()

/**
* The rows a commit still has to write come back in source order, without the written one.
*
* @return void
*/
public function testFindPendingWritesReturnsTheUnwrittenRowsInSourceOrder(): void {
$this->assertSame([1, 3], self::rowNumbers($this->mapper->findPendingWrites(previewId: 7)));
}//end testFindPendingWritesReturnsTheUnwrittenRowsInSourceOrder()

/**
* The JSON the frontend reads keeps the `row` key.
*
* @return void
*/
public function testTheJsonKeepsTheRowKey(): void {
$first = $this->mapper->findByPreview(previewId: 7)[0];

$this->assertSame(1, $first->jsonSerialize()['row']);
}//end testTheJsonKeepsTheRowKey()
}//end class
Loading
Loading