diff --git a/README.md b/README.md index 110491f..d41ea38 100755 --- a/README.md +++ b/README.md @@ -186,6 +186,15 @@ Example: php artisan iseed users --noindex ``` +### keychecks +By using --keychecks the seeds can be generated with `FOREIGN_KEY_CHECKS` disabled and enabled. +The use case for this feature is when you have an exisiting data in a table then you can run your seeds without having to truncate your tables. Without it, generating your seeds in an existing table with foreign keys might throw an error as `foreign key constraint failed`. So, by using `--keychecks` option you can directly seed without the constraint error. On default, foreign key checks are disabled. + +Example: +``` +php artisan iseed users --keychecks +``` + ## Usage To generate a seed file for your users table simply call: `\Iseed::generateSeed('users', 'connectionName', 'numOfRows');`. `connectionName` and `numOfRows` are not required arguments. diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index 26eb5c9..decb1a8 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -58,10 +58,11 @@ public function readStubFile($file) * @param int $max * @param string $prerunEvent * @param string $postunEvent + * @param bool $keyChecks * @return bool * @throws Orangehill\Iseed\TableNotFoundException */ - public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC') + public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $keyChecks = false, $orderBy = null, $direction = 'ASC') { if (!$database) { $database = config('database.default'); @@ -101,7 +102,8 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul $chunkSize, $prerunEvent, $postrunEvent, - $indexed + $indexed, + $keyChecks ); // Save a populated stub @@ -218,9 +220,11 @@ public function getStubPath() * @param int $chunkSize * @param string $prerunEvent * @param string $postunEvent + * @param bool $indexed + * @param bool $keyChecks * @return string */ - public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true) + public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true, $keyChecks = false) { $chunkSize = $chunkSize ?: config('iseed::config.chunk_size'); @@ -278,6 +282,14 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p '{{postrun_event}}', $postrunEventInsert, $stub ); + if ($keyChecks) { + $stub = str_replace('{{enable_key_checks}}', "\DB::statement(\"SET FOREIGN_KEY_CHECKS = 1\");", $stub); + $stub = str_replace('{{disable_key_checks}}', "\DB::statement(\"SET FOREIGN_KEY_CHECKS = 0\");", $stub); + }else{ + $stub = str_replace('{{enable_key_checks}}', "", $stub); + $stub = str_replace('{{disable_key_checks}}', "", $stub); + } + $stub = str_replace('{{insert_statements}}', $inserts, $stub); return $stub; diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php index 58517ec..06621c4 100755 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -66,7 +66,7 @@ public function fire() $direction = $this->option('direction'); $prefix = $this->option('classnameprefix'); $suffix = $this->option('classnamesuffix'); - + $keychecks = $this->option('keychecks'); if ($max < 1) { $max = null; } @@ -106,8 +106,9 @@ public function fire() $postrunEvent, $dumpAuto, $indexed, + $keychecks, $orderBy, - $direction + $direction, ), $table ); @@ -128,7 +129,8 @@ public function fire() $prerunEvent, $postrunEvent, $dumpAuto, - $indexed + $indexed, + $keychecks ), $table ); @@ -172,6 +174,7 @@ protected function getOptions() array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null), array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null), array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null), + array('keychecks', null, InputOption::VALUE_NONE, 'enable disable foreign keys checks', null), ); } diff --git a/src/Orangehill/Iseed/Stubs/seed.stub b/src/Orangehill/Iseed/Stubs/seed.stub index 382eb2a..5a01482 100644 --- a/src/Orangehill/Iseed/Stubs/seed.stub +++ b/src/Orangehill/Iseed/Stubs/seed.stub @@ -14,7 +14,10 @@ class {{class}} extends Seeder { {{prerun_event}} + {{disable_key_checks}} \DB::table('{{table}}')->delete(); + {{enable_key_checks}} + {{insert_statements}} {{postrun_event}}