wn:migration command improuved

This commit is contained in:
Amine Ben hammou 2015-09-26 22:43:06 +01:00
parent 3fb6c053a9
commit b0e163eb09
9 changed files with 161 additions and 13 deletions

31
formats/foreign-keys.json Normal file
View 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": ""
}
]
}
}

View File

@ -2,7 +2,7 @@
$I = new AcceptanceTester($scenario);
$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->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
@ -18,7 +18,8 @@ class CreateTasksMigration extends Migration
{
Schema::create(\'tasks\', function(Blueprint $table) {
$table->increments(\'id\');
// Schema declaration
// Schema declaration
// Constraints declaration
$table->timestamps();
});
}
@ -31,8 +32,8 @@ class CreateTasksMigration extends Migration
');
$I->deleteFile('./database/migrations/create_tasks.php');
$I->wantTo('generate a migration without schema');
$I->runShellCommand('php artisan wn:migration tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"');
$I->wantTo('generate a migration with schema');
$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->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
@ -48,8 +49,9 @@ class CreateTasksMigration extends Migration
{
Schema::create(\'tasks\', function(Blueprint $table) {
$table->increments(\'id\');
$table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8);
$table->string(\'title\')->nullable();
$table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8);
$table->string(\'title\')->nullable();
// Constraints declaration
$table->timestamps();
});
}
@ -61,3 +63,17 @@ class CreateTasksMigration extends Migration
}
');
$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');

View File

@ -5,7 +5,10 @@ class MigrationCommand extends BaseCommand {
protected $signature = 'wn:migration
{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.}
// The action is only create for the moment
@ -20,12 +23,17 @@ class MigrationCommand extends BaseCommand {
->with([
'table' => $table,
'name' => $name,
'schema' => $this->getSchema()
'schema' => $this->getSchema(),
'constraints' => $this->getConstraints()
])
->get();
$name = snake_case($name);
$this->save($content, "./database/migrations/{$name}.php");
$file = $this->option('file');
if(! $file){
$file = date('Y_m_d_His_') . snake_case($name);
}
$this->save($content, "./database/migrations/{$file}.php");
$this->info("{$table} migration generated !");
}
@ -34,7 +42,7 @@ class MigrationCommand extends BaseCommand {
{
$schema = $this->option('schema');
if(! $schema){
return "\t\t\t// Schema declaration";
return " // Schema declaration";
}
$items = $this->getArgumentParser('schema')->parse($schema);
@ -55,7 +63,62 @@ class MigrationCommand extends BaseCommand {
$parts = array_map(function($part){
return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')';
}, $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 . ';';
}
}

View File

@ -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
*/

View File

@ -12,9 +12,9 @@ class CommandsServiceProvider extends ServiceProvider
$this->registerControllerCommand();
$this->registerRouteCommand();
$this->registerMigrationCommand();
// $this->registerResourceCommand();
// $this->registerSeedCommand();
// $this->registerTestCommand();
// $this->registerResourceCommand();
}
protected function registerModelCommand(){

View File

@ -11,6 +11,7 @@ class {{name}}Migration extends Migration
Schema::create('{{table}}', function(Blueprint $table) {
$table->increments('id');
{{schema}}
{{constraints}}
$table->timestamps();
});
}

View File

@ -0,0 +1,3 @@
$table->foreign('{{name}}')
->references('{{column}}')
->on('{{table}}')

View File

@ -0,0 +1 @@
->on{{event}}('{{action}}')