Skip to content

Commit 481b94a

Browse files
authored
Dump TRIGGER definitions _after_ data (#100)
When the new CSV output mode with the concept of "output drivers" was added in #92, dumping `TRIGGER` definitions was made an implementation detail of the MySQL output driver. This caused `TRIGGER` definitions to be dumped right after the `CREATE TABLE ...` commands, before the actual data `INSERT` statements. This potentially breaks the generated SQL files, since a newly created trigger may be relevant for the subsequent `INSERT` statements; however, MySQL requires that tables used in the trigger are also included in the `LOCK TABLES` statements. The aim of this PR is to revert that change, i. e. to dump trigger definitions for a table _after_ the data insert statements for it. I think it is not necessary to move all trigger definitions to the very end of the output – that is, after _all_ tables have been created and filled with data: A trigger depends on insert/update/deletes for a particular table and is executed only on these events. So, it is not a problem if a trigger refers to a table that has not been created/loaded yet as long as the trigger is not run (and avoiding to run it is the aim of this PR).
1 parent 000e2df commit 481b94a

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ public function dumpTableRow(array $row, Schema\Table $asset, Table $config): vo
7575

7676
fputcsv($this->outputFile, $row);
7777
}
78+
79+
public function dumpTriggerDefinition(Schema\Table $asset, Table $config): void
80+
{
81+
}
7882
}

src/Webfactory/Slimdump/Database/Dumper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,22 @@ private function dumpTable(Schema\Table $asset, Table $config): void
7777
if ($config->isDataDumpRequired()) {
7878
$this->dumpData($asset, $config);
7979
}
80+
81+
if ($config->isTriggerDumpRequired()) {
82+
$this->dumpTriggers($asset, $config);
83+
}
8084
}
8185

8286
private function dumpView(Schema\View $asset, Table $config): void
8387
{
8488
$this->outputFormatDriver->dumpViewDefinition($asset, $config);
8589
}
8690

91+
private function dumpTriggers(Schema\Table $asset, Table $config): void
92+
{
93+
$this->outputFormatDriver->dumpTriggerDefinition($asset, $config);
94+
}
95+
8796
private function dumpData(Schema\Table $asset, Table $tableConfig): void
8897
{
8998
$table = $asset->getName();

src/Webfactory/Slimdump/Database/MysqlOutputFormatDriver.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,19 @@ public function dumpTableStructure(Schema\Table $asset, Table $config): void
6060
}
6161

6262
$this->output->writeln($tableCreationCommand.";\n", OutputInterface::OUTPUT_RAW);
63-
64-
if ($config->isTriggerDumpRequired()) {
65-
$this->dumpTriggers($tableName, $config->getDumpTriggersLevel());
66-
}
6763
}
6864

69-
/**
70-
* @param int $level One of the Table::TRIGGER_* constants
71-
*/
72-
private function dumpTriggers(string $tableName, int $level = Table::DEFINER_NO_DEFINER): void
65+
public function dumpTriggerDefinition(Schema\Table $asset, Table $config): void
7366
{
67+
$tableName = $asset->getName();
68+
7469
$triggers = $this->db->fetchAll(sprintf('SHOW TRIGGERS LIKE %s', $this->db->quote($tableName)));
7570

7671
if (!$triggers) {
7772
return;
7873
}
7974

75+
$level = $config->getDumpTriggersLevel();
8076
$this->output->writeln("-- BEGIN TRIGGERS $tableName", OutputInterface::OUTPUT_RAW);
8177

8278
$this->output->writeln("DELIMITER ;;\n");

src/Webfactory/Slimdump/Database/OutputFormatDriverInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function dumpTableStructure(Schema\Table $asset, Table $config): void;
2727
*/
2828
public function dumpViewDefinition(Schema\View $asset, Table $config): void;
2929

30+
/**
31+
* Called to dump trigger definitions.
32+
*/
33+
public function dumpTriggerDefinition(Schema\Table $asset, Table $config): void;
34+
3035
/**
3136
* Called at the beginning when dumping data for a single table.
3237
*/

0 commit comments

Comments
 (0)