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
3 changes: 3 additions & 0 deletions .changelog/current/2535-primary-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fixed

- Added primary keys to all tables
71 changes: 71 additions & 0 deletions lib/Migration/Version000000Date20241010200522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace OCA\Cookbook\Migration;

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

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version000000Date20241010200522 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper */
$schema = $schemaClosure();

$table = $schema->getTable('cookbook_categories');

if (!$table->hasColumn('id')) {
$table->addColumn('id', 'integer', [
'autoincrement' => true,
]);
$table->setPrimaryKey(['id']);
}

$table = $schema->getTable('cookbook_names');

if (!$table->hasColumn('id')) {
$table->addColumn('id', 'integer', [
'autoincrement' => true,
]);
$table->setPrimaryKey(['id']);
}

$table = $schema->getTable('cookbook_keywords');

if (!$table->hasColumn('id')) {
$table->addColumn('id', 'integer', [
'autoincrement' => true,
]);
$table->setPrimaryKey(['id']);
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OCA\Cookbook\tests\Migration\Setup\Migrations;

include_once __DIR__ . '/AbstractMigrationTestCase.php';

class Version000000Date20241010200522Test extends AbstractMigrationTestCase {
/**
* @runInSeparateProcess
* @covers \OCA\Cookbook\Migration\Version000000Date20190312140601
*/
public function testCreatedTables() {

// Run the migration under test
$this->migrationService->migrate('000000Date20241010200522');
$this->renewSchema();

$this->postTestAsserts('cookbook_categories');
$this->postTestAsserts('cookbook_names');
$this->postTestAsserts('cookbook_keywords');
}

private function postTestAsserts(string $tableName): void {
$this->assertTrue($this->schema->hasTable($tableName));

$table = $this->schema->getTable($tableName);
$this->assertTrue($table->hasColumn('id'));
$this->assertTrue($table->getPrimaryKey() !== null);
}

protected function getPreviousMigrationName(): ?string {
return '000000Date20220703174647';
}
}