Skip to content
Snippets Groups Projects
Commit 58867c8c authored by Jan Kuchař's avatar Jan Kuchař
Browse files

Merge branch 'missing-primary-index' into 'master'

throw when missing primary index

See merge request !47
parents f50ae947 c839b2d4
No related branches found
No related tags found
1 merge request!47throw when missing primary index
Pipeline #44715 passed
...@@ -6,6 +6,7 @@ namespace Grifart\Tables\Scaffolding; ...@@ -6,6 +6,7 @@ namespace Grifart\Tables\Scaffolding;
use Dibi\Connection; use Dibi\Connection;
use Grifart\Tables\ColumnMetadata; use Grifart\Tables\ColumnMetadata;
use Grifart\Tables\MissingPrimaryIndex;
use function Functional\map; use function Functional\map;
use function Functional\reindex; use function Functional\reindex;
...@@ -75,6 +76,10 @@ WHERE pg_index.indisprimary AND pg_namespace.nspname = %s AND pg_class.relname = ...@@ -75,6 +76,10 @@ WHERE pg_index.indisprimary AND pg_namespace.nspname = %s AND pg_class.relname =
SQL SQL
, $schema, $table)->setType('indkey', null)->fetchSingle(); , $schema, $table)->setType('indkey', null)->fetchSingle();
if ($rawPrimaryKeyColumnPositions === null) {
throw MissingPrimaryIndex::in($schema, $table);
}
$primaryKeyColumnPositions = \explode(' ', $rawPrimaryKeyColumnPositions); $primaryKeyColumnPositions = \explode(' ', $rawPrimaryKeyColumnPositions);
return reindex( return reindex(
map( map(
......
...@@ -5,6 +5,14 @@ namespace Grifart\Tables; ...@@ -5,6 +5,14 @@ namespace Grifart\Tables;
abstract class UsageException extends \LogicException {} abstract class UsageException extends \LogicException {}
abstract class RuntimeException extends \RuntimeException {} abstract class RuntimeException extends \RuntimeException {}
final class MissingPrimaryIndex extends UsageException
{
public static function in(string $schema, string $table): self
{
return new self(\sprintf('Table "%s"."%s" must have a primary index. Provide one and try again.', $schema, $table));
}
}
final class ProbablyBrokenPrimaryIndexImplementation extends UsageException { final class ProbablyBrokenPrimaryIndexImplementation extends UsageException {
public function __construct(Table $table, int $affectedRows) public function __construct(Table $table, int $affectedRows)
{ {
......
<?php
declare(strict_types=1);
namespace Grifart\Tables\Tests\Fixtures;
use Dibi\Connection;
use Grifart\Tables\Database\Identifier;
use Grifart\Tables\Scaffolding\PostgresReflector;
use Grifart\Tables\Scaffolding\TablesDefinitions;
use Grifart\Tables\TypeResolver;
use Grifart\Tables\Types\TextType;
$connection = require __DIR__ . '/../createConnection.local.php';
\assert($connection instanceof Connection);
$reflector = new PostgresReflector($connection);
$typeResolver = (new TypeResolver($connection));
$typeResolver->addResolutionByLocation(new Identifier('public', 'missingPrimaryIndex', 'whatever'), TextType::text());
$tableDefinitions = new TablesDefinitions($reflector, $typeResolver);
return [
...$tableDefinitions->for(
'public',
'missingPrimaryIndex',
MissingPrimaryIndexRow::class,
MissingPrimaryIndexModifications::class,
MissingPrimaryIndexTable::class,
MissingPrimaryIndexPrimaryKey::class,
)
];
<?php declare(strict_types = 1);
namespace Grifart\Tables\Tests\Scaffolding;
use Grifart\ClassScaffolder\Definition\ClassDefinition;
use Grifart\ClassScaffolder\DefinitionFile;
use Grifart\ClassScaffolder\DefinitionResult;
use Grifart\ClassScaffolder\FileProcessor;
use Tester\Assert;
use function Grifart\Tables\Tests\connect;
require __DIR__ . '/../bootstrap.php';
connect();
$fileProcessor = new FileProcessor();
$results = $fileProcessor->processFile(
DefinitionFile::from(__DIR__ . '/../Fixtures/.definition.missingPrimaryIndex.php'),
static fn(ClassDefinition $definition) => DefinitionResult::success($definition), // won't be called but has to return something
);
Assert::true( ! $results->isSuccessful());
Assert::same('Table "public"."missingPrimaryIndex" must have a primary index. Provide one and try again.', $results->getError()->getMessage());
...@@ -33,3 +33,9 @@ CREATE TABLE IF NOT EXISTS public.package ( ...@@ -33,3 +33,9 @@ CREATE TABLE IF NOT EXISTS public.package (
"previousVersions" public."packageVersion"[] NOT NULL "previousVersions" public."packageVersion"[] NOT NULL
); );
SQL); SQL);
$connection->nativeQuery(<<<SQL
CREATE TABLE IF NOT EXISTS public."missingPrimaryIndex" (
whatever text NOT NULL
);
SQL);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment