Skip to content

Commit

Permalink
Excluded migrations table when checking if the database schema exists
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Mar 5, 2022
1 parent c22e38f commit 07c30f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
const DEFAULT_QR_CODE_ERROR_CORRECTION = 'l';
const DEFAULT_QR_CODE_ROUND_BLOCK_SIZE = true;
const MIN_TASK_WORKERS = 4;
const MIGRATIONS_TABLE = 'migrations';
4 changes: 3 additions & 1 deletion migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

declare(strict_types=1);

use const Shlinkio\Shlink\MIGRATIONS_TABLE;

return [

'migrations_paths' => [
'ShlinkMigrations' => 'data/migrations',
],
'table_storage' => [
'table_name' => 'migrations',
'table_name' => MIGRATIONS_TABLE,
],
'custom_template' => 'data/migrations_template.txt',

Expand Down
8 changes: 6 additions & 2 deletions module/CLI/src/Command/Db/CreateDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\Process\PhpExecutableFinder;

use function Functional\contains;
use function Functional\filter;

use const Shlinkio\Shlink\MIGRATIONS_TABLE;

class CreateDatabaseCommand extends AbstractDatabaseCommand
{
Expand Down Expand Up @@ -81,8 +84,9 @@ private function checkDbExists(): void
private function schemaExists(): bool
{
// If at least one of the shlink tables exist, we will consider the database exists somehow.
// Any inconsistency should be taken care by the migrations
// We exclude the migrations table, in case db:migrate was run first by mistake.
// Any other inconsistency will be taken care by the migrations.
$schemaManager = $this->regularConn->createSchemaManager();
return ! empty($schemaManager->listTableNames());
return ! empty(filter($schemaManager->listTableNames(), fn (string $table) => $table !== MIGRATIONS_TABLE));
}
}
18 changes: 14 additions & 4 deletions module/CLI/test/Command/Db/CreateDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use const Shlinkio\Shlink\MIGRATIONS_TABLE;

class CreateDatabaseCommandTest extends TestCase
{
Expand Down Expand Up @@ -89,7 +90,7 @@ public function databaseIsCreatedIfItDoesNotExist(): void
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']);
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
});
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']);
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table', MIGRATIONS_TABLE]);

$this->commandTester->execute([]);

Expand All @@ -99,15 +100,18 @@ public function databaseIsCreatedIfItDoesNotExist(): void
$listTables->shouldHaveBeenCalledOnce();
}

/** @test */
public function tablesAreCreatedIfDatabaseIsEmpty(): void
/**
* @test
* @dataProvider provideEmptyDatabase
*/
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
{
$shlinkDatabase = 'shlink_database';
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase);
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']);
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
});
$listTables = $this->schemaManager->listTableNames()->willReturn([]);
$listTables = $this->schemaManager->listTableNames()->willReturn($tables);
$runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [
'/usr/local/bin/php',
CreateDatabaseCommand::DOCTRINE_SCRIPT,
Expand All @@ -127,6 +131,12 @@ public function tablesAreCreatedIfDatabaseIsEmpty(): void
$runCommand->shouldHaveBeenCalledOnce();
}

public function provideEmptyDatabase(): iterable
{
yield 'no tables' => [[]];
yield 'migrations table' => [[MIGRATIONS_TABLE]];
}

/** @test */
public function databaseCheckIsSkippedForSqlite(): void
{
Expand Down

0 comments on commit 07c30f8

Please sign in to comment.