mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-27 06:25:28 +03:00
wn:migration command improuved
This commit is contained in:
parent
3fb6c053a9
commit
b0e163eb09
31
formats/foreign-keys.json
Normal file
31
formats/foreign-keys.json
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"separator": " ",
|
||||||
|
"fields": {
|
||||||
|
"type": "object",
|
||||||
|
"separator": ":",
|
||||||
|
"fields": [
|
||||||
|
"name",
|
||||||
|
{
|
||||||
|
"name": "column",
|
||||||
|
"type": "string",
|
||||||
|
"default": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "table",
|
||||||
|
"type": "string",
|
||||||
|
"default": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "on_delete",
|
||||||
|
"type": "string",
|
||||||
|
"default": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "on_update",
|
||||||
|
"type": "string",
|
||||||
|
"default": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
$I = new AcceptanceTester($scenario);
|
$I = new AcceptanceTester($scenario);
|
||||||
|
|
||||||
$I->wantTo('generate a migration without schema');
|
$I->wantTo('generate a migration without schema');
|
||||||
$I->runShellCommand('php artisan wn:migration tasks');
|
$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks');
|
||||||
$I->seeInShellOutput('tasks migration generated');
|
$I->seeInShellOutput('tasks migration generated');
|
||||||
$I->seeFileFound('./database/migrations/create_tasks.php');
|
$I->seeFileFound('./database/migrations/create_tasks.php');
|
||||||
$I->openFile('./database/migrations/create_tasks.php');
|
$I->openFile('./database/migrations/create_tasks.php');
|
||||||
@ -19,6 +19,7 @@ class CreateTasksMigration extends Migration
|
|||||||
Schema::create(\'tasks\', function(Blueprint $table) {
|
Schema::create(\'tasks\', function(Blueprint $table) {
|
||||||
$table->increments(\'id\');
|
$table->increments(\'id\');
|
||||||
// Schema declaration
|
// Schema declaration
|
||||||
|
// Constraints declaration
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -31,8 +32,8 @@ class CreateTasksMigration extends Migration
|
|||||||
');
|
');
|
||||||
$I->deleteFile('./database/migrations/create_tasks.php');
|
$I->deleteFile('./database/migrations/create_tasks.php');
|
||||||
|
|
||||||
$I->wantTo('generate a migration without schema');
|
$I->wantTo('generate a migration with schema');
|
||||||
$I->runShellCommand('php artisan wn:migration tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"');
|
$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"');
|
||||||
$I->seeInShellOutput('tasks migration generated');
|
$I->seeInShellOutput('tasks migration generated');
|
||||||
$I->seeFileFound('./database/migrations/create_tasks.php');
|
$I->seeFileFound('./database/migrations/create_tasks.php');
|
||||||
$I->openFile('./database/migrations/create_tasks.php');
|
$I->openFile('./database/migrations/create_tasks.php');
|
||||||
@ -50,6 +51,7 @@ class CreateTasksMigration extends Migration
|
|||||||
$table->increments(\'id\');
|
$table->increments(\'id\');
|
||||||
$table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8);
|
$table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8);
|
||||||
$table->string(\'title\')->nullable();
|
$table->string(\'title\')->nullable();
|
||||||
|
// Constraints declaration
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -61,3 +63,17 @@ class CreateTasksMigration extends Migration
|
|||||||
}
|
}
|
||||||
');
|
');
|
||||||
$I->deleteFile('./database/migrations/create_tasks.php');
|
$I->deleteFile('./database/migrations/create_tasks.php');
|
||||||
|
|
||||||
|
$I->wantTo('generate a migration with schema and foreign keys');
|
||||||
|
$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks --keys="category_type_id user_id:identifier:members:cascade" --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"');
|
||||||
|
$I->seeInShellOutput('tasks migration generated');
|
||||||
|
$I->seeFileFound('./database/migrations/create_tasks.php');
|
||||||
|
$I->openFile('./database/migrations/create_tasks.php');
|
||||||
|
$I->seeInThisFile('$table->foreign(\'category_type_id\')
|
||||||
|
->references(\'id\')
|
||||||
|
->on(\'category_types\');');
|
||||||
|
$I->seeInThisFile('$table->foreign(\'user_id\')
|
||||||
|
->references(\'identifier\')
|
||||||
|
->on(\'members\')
|
||||||
|
->onDelete(\'cascade\');');
|
||||||
|
$I->deleteFile('./database/migrations/create_tasks.php');
|
@ -5,7 +5,10 @@ class MigrationCommand extends BaseCommand {
|
|||||||
|
|
||||||
protected $signature = 'wn:migration
|
protected $signature = 'wn:migration
|
||||||
{table : The table name.}
|
{table : The table name.}
|
||||||
{--schema= : the schema.}';
|
{--schema= : the schema.}
|
||||||
|
{--keys= : foreign keys.}
|
||||||
|
{--file= : name of the migration file.}
|
||||||
|
';
|
||||||
// {action : One of create, add, remove or drop options.}
|
// {action : One of create, add, remove or drop options.}
|
||||||
// The action is only create for the moment
|
// The action is only create for the moment
|
||||||
|
|
||||||
@ -20,12 +23,17 @@ class MigrationCommand extends BaseCommand {
|
|||||||
->with([
|
->with([
|
||||||
'table' => $table,
|
'table' => $table,
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'schema' => $this->getSchema()
|
'schema' => $this->getSchema(),
|
||||||
|
'constraints' => $this->getConstraints()
|
||||||
])
|
])
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$name = snake_case($name);
|
$file = $this->option('file');
|
||||||
$this->save($content, "./database/migrations/{$name}.php");
|
if(! $file){
|
||||||
|
$file = date('Y_m_d_His_') . snake_case($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->save($content, "./database/migrations/{$file}.php");
|
||||||
|
|
||||||
$this->info("{$table} migration generated !");
|
$this->info("{$table} migration generated !");
|
||||||
}
|
}
|
||||||
@ -34,7 +42,7 @@ class MigrationCommand extends BaseCommand {
|
|||||||
{
|
{
|
||||||
$schema = $this->option('schema');
|
$schema = $this->option('schema');
|
||||||
if(! $schema){
|
if(! $schema){
|
||||||
return "\t\t\t// Schema declaration";
|
return " // Schema declaration";
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = $this->getArgumentParser('schema')->parse($schema);
|
$items = $this->getArgumentParser('schema')->parse($schema);
|
||||||
@ -55,7 +63,62 @@ class MigrationCommand extends BaseCommand {
|
|||||||
$parts = array_map(function($part){
|
$parts = array_map(function($part){
|
||||||
return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')';
|
return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')';
|
||||||
}, $parts);
|
}, $parts);
|
||||||
return "\t\t\t\$table" . implode('', $parts) . ';';
|
return " \$table" . implode('', $parts) . ';';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getConstraints()
|
||||||
|
{
|
||||||
|
$keys = $this->option('keys');
|
||||||
|
if(! $keys){
|
||||||
|
return " // Constraints declaration";
|
||||||
|
}
|
||||||
|
|
||||||
|
$items = $this->getArgumentParser('foreign-keys')->parse($keys);
|
||||||
|
|
||||||
|
$constraints = [];
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$constraints[] = $this->getConstraintDeclaration($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(PHP_EOL, $constraints);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getConstraintDeclaration($key)
|
||||||
|
{
|
||||||
|
if(! $key['column']){
|
||||||
|
$key['column'] = 'id';
|
||||||
|
}
|
||||||
|
if(! $key['table']){
|
||||||
|
$key['table'] = str_plural(substr($key['name'], 0, count($key['name']) - 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraint = $this->getTemplate('migration/foreign-key')
|
||||||
|
->with([
|
||||||
|
'name' => $key['name'],
|
||||||
|
'table' => $key['table'],
|
||||||
|
'column' => $key['column']
|
||||||
|
])
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if($key['on_delete']){
|
||||||
|
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
||||||
|
->with([
|
||||||
|
'event' => 'Delete',
|
||||||
|
'action' => $key['on_delete']
|
||||||
|
])
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
if($key['on_update']){
|
||||||
|
$constraint .= PHP_EOL . $this->getTemplate('migration/on-constraint')
|
||||||
|
->with([
|
||||||
|
'event' => 'Update',
|
||||||
|
'action' => $key['on_update']
|
||||||
|
])
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $constraint . ';';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
wn:resource
|
||||||
|
{name : Name of the resource.}
|
||||||
|
{--fields= : fields description.}
|
||||||
|
name
|
||||||
|
schema
|
||||||
|
attrs: fillable, date
|
||||||
|
rules
|
||||||
|
{--has-many= : hasMany relationships.}
|
||||||
|
{--has-one= : hasOne relationships.}
|
||||||
|
{--belongs-to= : belongsTo relationships.}
|
||||||
|
|
||||||
|
wn:migration
|
||||||
|
table => str_plural(name)
|
||||||
|
--schema =>
|
||||||
|
wn:route
|
||||||
|
resource => name
|
||||||
|
|
||||||
|
wn:controller
|
||||||
|
model => (namespace from --model-path) ucwords(camel_case(name))
|
||||||
|
--no-routes => true
|
||||||
|
|
||||||
|
wn:model
|
||||||
|
name => ucwords(camel_case(name))
|
||||||
|
--fillable => having_fillable_attr(fields)
|
||||||
|
--dates => having_date_attr(fields)
|
||||||
|
--rules => rules_of(fields)
|
||||||
|
--path => --model-path
|
||||||
|
--has-many => --has-many
|
||||||
|
--has-one => --has-one
|
||||||
|
--belongs-to => --belongs-to
|
||||||
|
*/
|
@ -12,9 +12,9 @@ class CommandsServiceProvider extends ServiceProvider
|
|||||||
$this->registerControllerCommand();
|
$this->registerControllerCommand();
|
||||||
$this->registerRouteCommand();
|
$this->registerRouteCommand();
|
||||||
$this->registerMigrationCommand();
|
$this->registerMigrationCommand();
|
||||||
|
// $this->registerResourceCommand();
|
||||||
// $this->registerSeedCommand();
|
// $this->registerSeedCommand();
|
||||||
// $this->registerTestCommand();
|
// $this->registerTestCommand();
|
||||||
// $this->registerResourceCommand();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function registerModelCommand(){
|
protected function registerModelCommand(){
|
||||||
|
@ -11,6 +11,7 @@ class {{name}}Migration extends Migration
|
|||||||
Schema::create('{{table}}', function(Blueprint $table) {
|
Schema::create('{{table}}', function(Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
{{schema}}
|
{{schema}}
|
||||||
|
{{constraints}}
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
3
templates/migration/foreign-key.wnt
Normal file
3
templates/migration/foreign-key.wnt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
$table->foreign('{{name}}')
|
||||||
|
->references('{{column}}')
|
||||||
|
->on('{{table}}')
|
1
templates/migration/on-constraint.wnt
Normal file
1
templates/migration/on-constraint.wnt
Normal file
@ -0,0 +1 @@
|
|||||||
|
->on{{event}}('{{action}}')
|
Loading…
Reference in New Issue
Block a user