From 4cb805e100695760fa43fa307568b48c301fec8c Mon Sep 17 00:00:00 2001 From: Julien Turby Date: Thu, 9 Aug 2018 18:28:37 +0200 Subject: [PATCH 1/2] add on delete/update options on foreignKeys query --- src/Dumper/Sql/Dumper/StructureDumper.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Dumper/Sql/Dumper/StructureDumper.php b/src/Dumper/Sql/Dumper/StructureDumper.php index 1d1a19c..3d63650 100644 --- a/src/Dumper/Sql/Dumper/StructureDumper.php +++ b/src/Dumper/Sql/Dumper/StructureDumper.php @@ -60,9 +60,15 @@ public function dumpTableStructure(array $tables) public function dumpConstraints(array $tables) { foreach ($tables as $table) { foreach ($table->getForeignKeys() as $constraint) { + $options = ''; + if ($onUpdate = $constraint->onUpdate()) { + $options .= ' ON UPDATE' . $onUpdate; + } + if ($onDelete = $constraint->onDelete()) { + $options .= ' ON DELETE ' . $onDelete; + } $constraint = $this->platform->getCreateConstraintSQL($constraint, $table); - - $this->dumpOutput->writeln($constraint . ';'); + $this->dumpOutput->writeln($constraint . $options . ';'); } } } From fddc691d879d7d27cc4557d0ca1d35f58bd02392 Mon Sep 17 00:00:00 2001 From: Julien Turby Date: Tue, 21 Aug 2018 11:11:13 +0200 Subject: [PATCH 2/2] add test for foreign key delete/update options --- src/Converter/Tests/ConditionalConverterTest.php | 4 ++-- src/Converter/Tests/RandomConverterTest.php | 3 ++- src/Dumper/Sql/Dumper/StructureDumper.php | 2 +- src/Dumper/Sql/Tests/AbstractSqlTest.php | 7 +++---- src/Dumper/Sql/Tests/TableFilterTest.php | 3 ++- src/Dumper/Sql/Tests/test_dump.sql | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Converter/Tests/ConditionalConverterTest.php b/src/Converter/Tests/ConditionalConverterTest.php index e6e0e51..3665e0c 100644 --- a/src/Converter/Tests/ConditionalConverterTest.php +++ b/src/Converter/Tests/ConditionalConverterTest.php @@ -3,9 +3,9 @@ namespace Digilist\SnakeDumper\Converter\Tests; use Digilist\SnakeDumper\Converter\ConditionalConverter; -use Digilist\SnakeDumper\Converter\Service\DataConverter; +use PHPUnit\Framework\TestCase; -class ConditionalConverterTest extends \PHPUnit_Framework_TestCase +class ConditionalConverterTest extends TestCase { public function testConditionalIfTrue() diff --git a/src/Converter/Tests/RandomConverterTest.php b/src/Converter/Tests/RandomConverterTest.php index 907e15f..2a57c21 100644 --- a/src/Converter/Tests/RandomConverterTest.php +++ b/src/Converter/Tests/RandomConverterTest.php @@ -4,12 +4,13 @@ use Digilist\SnakeDumper\Converter\RandomConverter; use Digilist\SnakeDumper\Exception\InvalidArgumentException; +use PHPUnit\Framework\TestCase; /** * @package Digilist\SnakeDumper\Converter\Tests * @author moellers */ -class RandomConverterTest extends \PHPUnit_Framework_TestCase +class RandomConverterTest extends TestCase { public function testEqualBounds() diff --git a/src/Dumper/Sql/Dumper/StructureDumper.php b/src/Dumper/Sql/Dumper/StructureDumper.php index 3d63650..50d9bc7 100644 --- a/src/Dumper/Sql/Dumper/StructureDumper.php +++ b/src/Dumper/Sql/Dumper/StructureDumper.php @@ -62,7 +62,7 @@ public function dumpConstraints(array $tables) { foreach ($table->getForeignKeys() as $constraint) { $options = ''; if ($onUpdate = $constraint->onUpdate()) { - $options .= ' ON UPDATE' . $onUpdate; + $options .= ' ON UPDATE ' . $onUpdate; } if ($onDelete = $constraint->onDelete()) { $options .= ' ON DELETE ' . $onDelete; diff --git a/src/Dumper/Sql/Tests/AbstractSqlTest.php b/src/Dumper/Sql/Tests/AbstractSqlTest.php index 923db5b..1324b04 100644 --- a/src/Dumper/Sql/Tests/AbstractSqlTest.php +++ b/src/Dumper/Sql/Tests/AbstractSqlTest.php @@ -2,13 +2,12 @@ namespace Digilist\SnakeDumper\Dumper\Sql\Tests; -use Digilist\SnakeDumper\Configuration\DatabaseConfiguration; -use Digilist\SnakeDumper\Dumper\Sql\ConnectionHandler; use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Platforms\AbstractPlatform; +use PHPUnit\Framework\TestCase; -abstract class AbstractSqlTest extends \PHPUnit_Framework_TestCase +abstract class AbstractSqlTest extends TestCase { const DBNAME = 'snakedumper'; @@ -75,7 +74,7 @@ protected function createTestSchema($randomTables = false) customer_id INTEGER, product VARCHAR(100), amount REAL, - CONSTRAINT customer_id FOREIGN KEY (customer_id) REFERENCES Customer(id) + CONSTRAINT customer_id FOREIGN KEY (customer_id) REFERENCES Customer(id) ON UPDATE CASCADE ON DELETE SET NULL )'); $pdo->query('CREATE INDEX billing_product ON Billing (product)'); diff --git a/src/Dumper/Sql/Tests/TableFilterTest.php b/src/Dumper/Sql/Tests/TableFilterTest.php index 8633597..39eb773 100644 --- a/src/Dumper/Sql/Tests/TableFilterTest.php +++ b/src/Dumper/Sql/Tests/TableFilterTest.php @@ -6,8 +6,9 @@ use Digilist\SnakeDumper\Configuration\Table\TableConfiguration; use Digilist\SnakeDumper\Dumper\Sql\TableFilter; use Doctrine\DBAL\Schema\Table; +use PHPUnit\Framework\TestCase; -class TableFilterTest extends \PHPUnit_Framework_TestCase +class TableFilterTest extends TestCase { /** diff --git a/src/Dumper/Sql/Tests/test_dump.sql b/src/Dumper/Sql/Tests/test_dump.sql index 9a19c0e..3ebcbfc 100644 --- a/src/Dumper/Sql/Tests/test_dump.sql +++ b/src/Dumper/Sql/Tests/test_dump.sql @@ -10,4 +10,4 @@ CREATE TABLE `RandomTable` (`id` INT AUTO_INCREMENT NOT NULL, `name` VARCHAR(10) INSERT INTO `Customer` (`id`, `name`) VALUES ('1', 'Foobar'), ('2', 'Foobar'), ('3', 'Foobar'); INSERT INTO `Customer` (`id`, `name`) VALUES ('4', 'Foobar'); INSERT INTO `Billing` (`id`, `customer_id`, `product`, `amount`) VALUES ('1', '1', 'IT', '42'), ('2', '1', NULL, '1337'), ('3', '2', 'Some stuff', '1337'); -ALTER TABLE `Billing` ADD CONSTRAINT `customer_id` FOREIGN KEY (`customer_id`) REFERENCES `Customer` (`id`); +ALTER TABLE `Billing` ADD CONSTRAINT `customer_id` FOREIGN KEY (`customer_id`) REFERENCES `Customer` (`id`) ON UPDATE CASCADE ON DELETE SET NULL;