Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primary keys to all tables #2535

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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 null;
}
}
Loading