Skip to content

Commit

Permalink
Merge pull request #249 from donejeh/master
Browse files Browse the repository at this point in the history
This PR introduces a new feature that improves the usability of the iSeed package by allowing seeders to be generated for all tables when no specific table names are provided.
  • Loading branch information
tihomiro authored Feb 13, 2025
2 parents 403ea73 + 9e3163b commit 171281b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 94 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,28 @@ Orangehill\Iseed\IseedServiceProvider::class,
## Artisan command options

### [table_name]
Mandatory parameter which defines which table/s will be used for seed creation.
Use CSV notation for multiple tables. Seed file will be generated for each table.
Optional. This parameter defines which table(s) will be used for seed creation.

Examples:
If provided:
Use CSV notation to list one or more table names. A seed file will be generated for each specified table.

Examples Generate a seed file for a single table:
```
php artisan iseed my_table
```
Example Generate seed files for multiple tables:

```
php artisan iseed my_table,another_table
```

If omitted:
The command automatically retrieves all table names from your database and generates seeders for every table.
Examples:
```
php artisan iseed
```

### classnameprefix & classnamesuffix
Optionally specify a prefix or suffix for the Seeder class name and file name.
This is useful if you want to create an additional seed for a table that has an existing seed without overwriting the existing one.
Expand Down
7 changes: 7 additions & 0 deletions src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,11 @@ public function updateDatabaseSeederRunMethod($className)

return $this->files->put($databaseSeederPath, $content) !== false;
}

public function getAllTableNames()
{
// Depending on your Laravel version, you may use the Doctrine schema manager:
$schema = \DB::connection($this->databaseName)->getDoctrineSchemaManager();
return $schema->listTableNames();
}
}
182 changes: 91 additions & 91 deletions src/Orangehill/Iseed/IseedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,96 +47,96 @@ public function handle()
*
* @return void
*/
public function fire()
{
// if clean option is checked empty iSeed template in DatabaseSeeder.php
if ($this->option('clean')) {
app('iseed')->cleanSection();
}

$tables = explode(",", $this->argument('tables'));
$max = intval($this->option('max'));
$chunkSize = intval($this->option('chunksize'));
$exclude = explode(",", $this->option('exclude'));
$prerunEvents = explode(",", $this->option('prerun'));
$postrunEvents = explode(",", $this->option('postrun'));
$dumpAuto = intval($this->option('dumpauto'));
$indexed = !$this->option('noindex');
$orderBy = $this->option('orderby');
$direction = $this->option('direction');
$prefix = $this->option('classnameprefix');
$suffix = $this->option('classnamesuffix');

if ($max < 1) {
$max = null;
}

if ($chunkSize < 1) {
$chunkSize = null;
}

$tableIncrement = 0;
foreach ($tables as $table) {
$table = trim($table);
$prerunEvent = null;
if (isset($prerunEvents[$tableIncrement])) {
$prerunEvent = trim($prerunEvents[$tableIncrement]);
}
$postrunEvent = null;
if (isset($postrunEvents[$tableIncrement])) {
$postrunEvent = trim($postrunEvents[$tableIncrement]);
}
$tableIncrement++;

// generate file and class name based on name of the table
list($fileName, $className) = $this->generateFileName($table, $prefix, $suffix);

// if file does not exist or force option is turned on generate seeder
if (!\File::exists($fileName) || $this->option('force')) {
$this->printResult(
app('iseed')->generateSeed(
$table,
$prefix,
$suffix,
$this->option('database'),
$max,
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed,
$orderBy,
$direction
),
$table
);
continue;
}

if ($this->confirm('File ' . $className . ' already exist. Do you wish to override it? [yes|no]')) {
// if user said yes overwrite old seeder
$this->printResult(
app('iseed')->generateSeed(
$table,
$prefix,
$suffix,
$this->option('database'),
$max,
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed
),
$table
);
}
}

return;
}

public function fire()
{
// Retrieve the tables argument. Since we want to allow a default of "all tables",
// make sure the tables argument is optional in getArguments() (see below).
$tablesArg = $this->argument('tables');
if (empty($tablesArg)) {
// Get all table names from the database
$tables = app('iseed')->getAllTableNames();
} else {
// Otherwise, split the provided comma-separated table names
$tables = explode(',', $tablesArg);
}

// Convert other options as needed
$max = intval($this->option('max'));
$chunkSize = intval($this->option('chunksize'));
$exclude = explode(",", $this->option('exclude'));
$prerunEvents = explode(",", $this->option('prerun'));
$postrunEvents = explode(",", $this->option('postrun'));
$dumpAuto = intval($this->option('dumpauto'));
$indexed = !$this->option('noindex');
$orderBy = $this->option('orderby');
$direction = $this->option('direction');
$prefix = $this->option('classnameprefix');
$suffix = $this->option('classnamesuffix');
if ($max < 1) {
$max = null;
}
if ($chunkSize < 1) {
$chunkSize = null;
}

$tableIncrement = 0;
foreach ($tables as $table) {
$table = trim($table);
$prerunEvent = isset($prerunEvents[$tableIncrement]) ? trim($prerunEvents[$tableIncrement]) : null;
$postrunEvent = isset($postrunEvents[$tableIncrement]) ? trim($postrunEvents[$tableIncrement]) : null;
$tableIncrement++;

// generate file and class name based on name of the table
list($fileName, $className) = $this->generateFileName($table, $prefix, $suffix);

// if file does not exist or force option is turned on, generate seeder
if (!\File::exists($fileName) || $this->option('force')) {
$this->printResult(
app('iseed')->generateSeed(
$table,
$prefix,
$suffix,
$this->option('database'),
$max,
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed,
$orderBy,
$direction
),
$table
);
continue;
}

if ($this->confirm('File ' . $className . ' already exists. Do you wish to override it? [yes|no]')) {
// Overwrite old seeder if confirmed
$this->printResult(
app('iseed')->generateSeed(
$table,
$prefix,
$suffix,
$this->option('database'),
$max,
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent,
$dumpAuto,
$indexed
),
$table
);
}
}
}

/**
* Get the console command arguments.
Expand All @@ -146,7 +146,7 @@ public function fire()
protected function getArguments()
{
return array(
array('tables', InputArgument::REQUIRED, 'comma separated string of table names'),
array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),
);
}

Expand Down

0 comments on commit 171281b

Please sign in to comment.