Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key checks option #157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -101,7 +102,8 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul
$chunkSize,
$prerunEvent,
$postrunEvent,
$indexed
$indexed,
$keyChecks
);

// Save a populated stub
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/Orangehill/Iseed/IseedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -106,8 +106,9 @@ public function fire()
$postrunEvent,
$dumpAuto,
$indexed,
$keychecks,
$orderBy,
$direction
$direction,
),
$table
);
Expand All @@ -128,7 +129,8 @@ public function fire()
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed
$indexed,
$keychecks
),
$table
);
Expand Down Expand Up @@ -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),
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Orangehill/Iseed/Stubs/seed.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class {{class}} extends Seeder
{
{{prerun_event}}

{{disable_key_checks}}
\DB::table('{{table}}')->delete();
{{enable_key_checks}}

{{insert_statements}}

{{postrun_event}}
Expand Down