Skip to content

Commit

Permalink
Update index tokenizer to look for "NO ACTION", "SET NULL", "SET DEFA…
Browse files Browse the repository at this point in the history
…ULT" for foreign key constraints.
  • Loading branch information
bennett-treptow committed Apr 12, 2021
1 parent 42c9d39 commit 1cb4b37
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Tokenizers/MySQL/IndexTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Tokenizers/MySQL/IndexTokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1cb4b37

Please sign in to comment.