Skip to content

Commit

Permalink
Merge pull request #9 from weblabormx/feature-seeds-by-table
Browse files Browse the repository at this point in the history
save seeds by tables
  • Loading branch information
weezqyd authored Sep 19, 2018
2 parents 83f82d0 + ec6f7b3 commit c4a5a05
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/DbExporter/DbExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ abstract public function convert($database = null);
*
* @return mixed
*/
abstract protected function compile();
abstract protected function compile($table);
}
2 changes: 1 addition & 1 deletion src/DbExporter/DbMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function columnType($type, $columns = 'columns', $method = '')
*
* @return string
*/
protected function compile()
protected function compile($null = null)
{
$upSchema = '';
$downSchema = '';
Expand Down
55 changes: 38 additions & 17 deletions src/DbExporter/DbSeeding.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public function write()
$this->convert();
}

$seed = $this->compile();
$absolutePath = Config::get('db-exporter.export_path.seeds');
$this->filename = ucfirst(Str::camel($this->database)) . 'DatabaseSeeder';
$this->makePath($absolutePath);
file_put_contents($absolutePath . "/{$this->filename}.php", $seed);
foreach ($this->seedingStub as $table => $value)
{
$seed = $this->compile($table);
$absolutePath = Config::get('db-exporter.export_path.seeds');
$this->filename = ucfirst(Str::camel($table)) . 'DatabaseSeeder';
$this->makePath($absolutePath);
file_put_contents($absolutePath . "/{$this->filename}.php", $seed);
}
}

//end write()
Expand All @@ -77,6 +80,7 @@ public function convert($database = null)

// Get the tables for the database
$tables = $this->getTables();
$result = [];

// Get tables to ignore
$config = config('db-exporter.seeds');
Expand All @@ -85,7 +89,6 @@ public function convert($database = null)
$ignore_tables = collect($config['ignore_tables']);
}

$stub = '';
// Loop over the tables
foreach ($tables as $key => $value)
{
Expand Down Expand Up @@ -116,15 +119,24 @@ public function convert($database = null)
}
}

if ($this->hasTableData($tableData)) {
$stub .= "
DB::table('" . $tableName . "')->insert([
$insertStub = "
\$data = [
{$insertStub}
]);";
];";

if ($this->hasTableData($tableData)) {
$stub = $insertStub.'
foreach($data as $item)
{
$this->saveData("'.$tableName.'", $item);
}';
}

$result[$tableName] = $stub;
}//end foreach

$this->seedingStub = $stub;
$this->seedingStub = $result;

return $this;
}
Expand All @@ -136,14 +148,14 @@ public function convert($database = null)
*
* @return mixed
*/
protected function compile()
protected function compile($table)
{
// Grab the template
$template = File::get(__DIR__ . '/stubs/seed.stub');

// Replace the classname
$template = str_replace('{{className}}', ucfirst(Str::camel($this->database)) . 'DatabaseSeeder', $template);
$template = str_replace('{{run}}', $this->seedingStub, $template);
$template = str_replace('{{className}}', ucfirst(Str::camel($table)) . 'DatabaseSeeder', $template);
$template = str_replace('{{run}}', $this->seedingStub[$table], $template);

return $template;
}
Expand All @@ -152,10 +164,19 @@ protected function compile()

private function insertPropertyAndValue($prop, $value)
{
$prop = addslashes($prop);
$value = addslashes($value);
if(strlen($prop) > 0) {
$prop = "'{$prop}'";
} else {
$prop = 'null';
}

return " '{$prop}' => '{$value}',\n";
if(strlen($value) > 0) {
$value = str_replace("'", "\'", $value);
$value = "'{$value}'";
} else {
$value = 'null';
}
return " {$prop} => {$value},\n";
}

//end insertPropertyAndValue()
Expand Down
49 changes: 49 additions & 0 deletions src/DbExporter/SeederHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Elimuswift\DbExporter;

trait SeederHelper
{

public function saveData($table, $item)
{
// If have primary id
if(isset($item['id']))
{
if(\DB::table($table)->where('id', $item['id'])->count() > 0)
{
\DB::table($table)->where('id', $item['id'])->update($item);
}
else
{
\DB::table($table)->insert($item);
}
}
else
{
$ids = collect($item)->filter(function($item, $key) {
return str_contains($key, '_id');
})->keys()->values();

// If there isnt any column with _id, so check that every column matches
if($ids->count() <= 0) {
$ids = collect($item)->keys()->values();
}
$object = \DB::table($table);
foreach ($ids as $id) {
$object = $object->where($id, $item[$id]);
}

// save or update
if($object->count() > 0)
{
$object->update($item);
}
else
{
$object->insert($item);
}

}
}
}
12 changes: 8 additions & 4 deletions src/DbExporter/stubs/seed.stub
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

use Illuminate\Database\Seeder;
use Elimuswift\DbExporter\SeederHelper;

class {{className}} extends Seeder {
class {{className}} extends Seeder
{

use SeederHelper;

public function run()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');

{{run}}
{{run}}
}

}

0 comments on commit c4a5a05

Please sign in to comment.