From 1cb4b37c4c97a5f620ec4ca828340f4f7cec7d29 Mon Sep 17 00:00:00 2001 From: Bennett Treptow Date: Sun, 11 Apr 2021 19:34:18 -0500 Subject: [PATCH] Update index tokenizer to look for "NO ACTION", "SET NULL", "SET DEFAULT" for foreign key constraints. --- src/Tokenizers/MySQL/IndexTokenizer.php | 10 ++++- .../Tokenizers/MySQL/IndexTokenizerTest.php | 45 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Tokenizers/MySQL/IndexTokenizer.php b/src/Tokenizers/MySQL/IndexTokenizer.php index e886165..afb90a7 100644 --- a/src/Tokenizers/MySQL/IndexTokenizer.php +++ b/src/Tokenizers/MySQL/IndexTokenizer.php @@ -77,8 +77,14 @@ private function consumeConstraintActions() { while ($token = $this->consume()) { if (strtoupper($token) === 'ON') { - $actionType = strtolower($this->consume()); - $actionMethod = strtolower($this->consume()); + $actionType = strtolower($this->consume()); //UPDATE + $actionMethod = strtolower($this->consume()); //CASCADE | NO ACTION | SET NULL | SET DEFAULT + if ($actionMethod === 'no') { + $this->consume(); //consume ACTION + $actionMethod = 'restrict'; + } elseif ($actionMethod === 'set') { + $actionMethod = 'set ' . $this->consume(); //consume NULL or DEFAULT + } $currentActions = $this->definition->getConstraintActions(); $currentActions[$actionType] = $actionMethod; $this->definition->setConstraintActions($currentActions); diff --git a/tests/Unit/Tokenizers/MySQL/IndexTokenizerTest.php b/tests/Unit/Tokenizers/MySQL/IndexTokenizerTest.php index ee44b28..e8210b3 100644 --- a/tests/Unit/Tokenizers/MySQL/IndexTokenizerTest.php +++ b/tests/Unit/Tokenizers/MySQL/IndexTokenizerTest.php @@ -136,5 +136,50 @@ public function test_it_tokenizes_foreign_key_with_update_and_delete() $this->assertEquals('$table->foreign(\'user_id\', \'fk_bank_accounts_user_id\')->references(\'id\')->on(\'users\')->onUpdate(\'cascade\')->onDelete(\'cascade\')', $indexDefinition->render()); } + public function test_it_tokenizes_foreign_key_with_update_restrict() + { + $indexTokenizer = IndexTokenizer::parse('CONSTRAINT `fk_bank_accounts_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION'); + $indexDefinition = $indexTokenizer->definition(); + + $this->assertEquals('foreign', $indexDefinition->getIndexType()); + $this->assertFalse($indexDefinition->isMultiColumnIndex()); + $this->assertCount(1, $indexDefinition->getIndexColumns()); + $this->assertEquals('users', $indexDefinition->getForeignReferencedTable()); + $this->assertEquals(['id'], $indexDefinition->getForeignReferencedColumns()); + $this->assertEquals(['user_id'], $indexDefinition->getIndexColumns()); + + $this->assertEquals('$table->foreign(\'user_id\', \'fk_bank_accounts_user_id\')->references(\'id\')->on(\'users\')->onUpdate(\'restrict\')', $indexDefinition->render()); + } + + public function test_it_tokenizes_foreign_key_with_update_set_null() + { + $indexTokenizer = IndexTokenizer::parse('CONSTRAINT `fk_bank_accounts_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE SET NULL'); + $indexDefinition = $indexTokenizer->definition(); + + $this->assertEquals('foreign', $indexDefinition->getIndexType()); + $this->assertFalse($indexDefinition->isMultiColumnIndex()); + $this->assertCount(1, $indexDefinition->getIndexColumns()); + $this->assertEquals('users', $indexDefinition->getForeignReferencedTable()); + $this->assertEquals(['id'], $indexDefinition->getForeignReferencedColumns()); + $this->assertEquals(['user_id'], $indexDefinition->getIndexColumns()); + + $this->assertEquals('$table->foreign(\'user_id\', \'fk_bank_accounts_user_id\')->references(\'id\')->on(\'users\')->onUpdate(\'set NULL\')', $indexDefinition->render()); + } + + public function test_it_tokenizes_foreign_key_with_update_set_default() + { + $indexTokenizer = IndexTokenizer::parse('CONSTRAINT `fk_bank_accounts_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE SET DEFAULT'); + $indexDefinition = $indexTokenizer->definition(); + + $this->assertEquals('foreign', $indexDefinition->getIndexType()); + $this->assertFalse($indexDefinition->isMultiColumnIndex()); + $this->assertCount(1, $indexDefinition->getIndexColumns()); + $this->assertEquals('users', $indexDefinition->getForeignReferencedTable()); + $this->assertEquals(['id'], $indexDefinition->getForeignReferencedColumns()); + $this->assertEquals(['user_id'], $indexDefinition->getIndexColumns()); + + $this->assertEquals('$table->foreign(\'user_id\', \'fk_bank_accounts_user_id\')->references(\'id\')->on(\'users\')->onUpdate(\'set DEFAULT\')', $indexDefinition->render()); + } + //endregion }