From 4fbe1fc4c21d0e592d606953f1ca3bed79da18fd Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Thu, 15 Dec 2022 10:26:43 +0900 Subject: [PATCH] fix: throw exception if no primary key is defined. (#58) Co-authored-by: Taka Oyama # Conflicts: # tests/Schema/BlueprintTest.php --- CHANGELOG.md | 3 +++ src/Schema/Grammar.php | 2 +- tests/Schema/BlueprintTest.php | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c0f0739..0b04075b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Added - Command `spanner:cooldown` which clears all connections in the session pool. +Changed +- Checks that primary key is defined in schema and throws an exception if not defined. + # v4.3.0 Added diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index c0621e10..4ec2feb6 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -294,7 +294,7 @@ protected function addPrimaryKeys(Blueprint $blueprint) if (! is_null($primary = $this->getCommandByName($blueprint, 'primary'))) { return "primary key ({$this->columnize($primary->columns)})"; } - return ''; + throw new LogicException('Cloud Spanner require a primary key!'); } /** diff --git a/tests/Schema/BlueprintTest.php b/tests/Schema/BlueprintTest.php index ece0b619..a8078319 100644 --- a/tests/Schema/BlueprintTest.php +++ b/tests/Schema/BlueprintTest.php @@ -150,6 +150,18 @@ public function testDropIndex(): void ); } + public function test_no_primaryKey(): void + { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Cloud Spanner require a primary key!'); + + $blueprint = new Blueprint('test', function (Blueprint $table) { + $table->create(); + $table->uuid('id'); + }); + $blueprint->toSql($this->getDefaultConnection(), new Grammar()); + } + public function testCompositePrimaryKey(): void { $conn = $this->getDefaultConnection();