Skip to content

Commit 1313823

Browse files
committed
feature: add option to add all tables by '-a' or '--all-tables'
1 parent 3912339 commit 1313823

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Commands/MakeAll.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
namespace Nanvaie\DatabaseRepository\Commands;
44

55
use Illuminate\Console\Command;
6+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
7+
use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString;
68

79
class MakeAll extends Command
810
{
11+
use CustomMySqlQueries;
12+
913
/**
1014
* The name and signature of the console command.
1115
*
1216
* @var string
1317
*/
14-
protected $signature = 'repository:make-all {table_names*}
18+
protected $signature = 'repository:make-all
19+
{--table_names= : Table names, separate names with comma}
1520
{--k|foreign-keys : Detect foreign keys}
1621
{--d|delete : Delete resource}
1722
{--f|force : Override/Delete existing classes}
18-
{--g|add-to-git : Add created file to git repository}';
23+
{--g|add-to-git : Add created file to git repository}
24+
{--a|all-tables : Add created file to git repository}';
1925

2026
/**
2127
* The console command description.
@@ -29,12 +35,20 @@ class MakeAll extends Command
2935
*/
3036
public function handle()
3137
{
32-
$tableNames = $this->argument('table_names');
3338
$force = $this->option('force');
3439
$delete = $this->option('delete');
3540
$detectForeignKeys = $this->option('foreign-keys');
3641
$addToGit = $this->option('add-to-git');
3742

43+
if ($this->option('all-tables')) {
44+
$tableNames = $this->getAllTableNames()->pluck('TABLE_NAME');
45+
} else if ($this->option('table_names')) {
46+
$tableNames = explode(',', $this->option('table_names'));
47+
} else {
48+
$this->alert("Please choose one of two options '--all-tables' or '--table_names=' ");
49+
die;
50+
}
51+
3852
foreach ($tableNames as $_tableName) {
3953
$arguments = [
4054
'table_name' => $_tableName,

src/CustomMySqlQueries.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public function getAllColumnsInTable(string $tableName): Collection
5353
->get();
5454
}
5555

56+
/**
57+
* Extract all table names.
58+
* @return Collection
59+
*/
60+
public function getAllTableNames(): Collection
61+
{
62+
return DB::table('INFORMATION_SCHEMA.TABLES')
63+
->select('TABLE_NAME')
64+
->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
65+
->where('TABLE_NAME', '<>', 'migrations')
66+
->get();
67+
}
68+
5669
/**
5770
* Extract all foreign keys from a given table. Foreign key's relations must define in MySql!
5871
* @param string $tableName

0 commit comments

Comments
 (0)