From b0e163eb0952457c4fa261f78e64f5d3574502fd Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Sat, 26 Sep 2015 22:43:06 +0100 Subject: [PATCH] wn:migration command improuved --- formats/foreign-keys.json | 31 ++++++++ .../tests/acceptance/MigrationCommandCept.php | 28 +++++-- src/Commands/MigrationCommand.php | 75 +++++++++++++++++-- src/Commands/ResourceCommand.php | 33 ++++++++ src/CommandsServiceProvider.php | 2 +- templates/migration.wnt | 1 + templates/migration/foreign-key.wnt | 3 + templates/migration/on-constraint.wnt | 1 + templates/migration/schema-field.wnt | 0 9 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 formats/foreign-keys.json create mode 100644 templates/migration/foreign-key.wnt create mode 100644 templates/migration/on-constraint.wnt delete mode 100644 templates/migration/schema-field.wnt diff --git a/formats/foreign-keys.json b/formats/foreign-keys.json new file mode 100644 index 0000000..383bcaf --- /dev/null +++ b/formats/foreign-keys.json @@ -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": "" + } + ] + } +} \ No newline at end of file diff --git a/lumen-test/tests/acceptance/MigrationCommandCept.php b/lumen-test/tests/acceptance/MigrationCommandCept.php index f8deae9..fc57103 100644 --- a/lumen-test/tests/acceptance/MigrationCommandCept.php +++ b/lumen-test/tests/acceptance/MigrationCommandCept.php @@ -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(); }); } @@ -60,4 +62,18 @@ 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'); \ No newline at end of file diff --git a/src/Commands/MigrationCommand.php b/src/Commands/MigrationCommand.php index 927c377..6369b86 100644 --- a/src/Commands/MigrationCommand.php +++ b/src/Commands/MigrationCommand.php @@ -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 . ';'; } } \ No newline at end of file diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index e69de29..726df9a 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -0,0 +1,33 @@ + 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 +*/ diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index 4de9d9f..8cf3fae 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -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(){ diff --git a/templates/migration.wnt b/templates/migration.wnt index 6ba5067..ef942af 100644 --- a/templates/migration.wnt +++ b/templates/migration.wnt @@ -11,6 +11,7 @@ class {{name}}Migration extends Migration Schema::create('{{table}}', function(Blueprint $table) { $table->increments('id'); {{schema}} +{{constraints}} $table->timestamps(); }); } diff --git a/templates/migration/foreign-key.wnt b/templates/migration/foreign-key.wnt new file mode 100644 index 0000000..aa53455 --- /dev/null +++ b/templates/migration/foreign-key.wnt @@ -0,0 +1,3 @@ + $table->foreign('{{name}}') + ->references('{{column}}') + ->on('{{table}}') \ No newline at end of file diff --git a/templates/migration/on-constraint.wnt b/templates/migration/on-constraint.wnt new file mode 100644 index 0000000..15164a4 --- /dev/null +++ b/templates/migration/on-constraint.wnt @@ -0,0 +1 @@ + ->on{{event}}('{{action}}') \ No newline at end of file diff --git a/templates/migration/schema-field.wnt b/templates/migration/schema-field.wnt deleted file mode 100644 index e69de29..0000000