diff --git a/readme.md b/readme.md index 9f3fc4e..06f1978 100644 --- a/readme.md +++ b/readme.md @@ -104,7 +104,7 @@ class CreateUsersTable extends Migration { */ public function up() { - Schema::create('users', function(Blueprint $table) { + Schema::connection('mysql')->create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('email')->unique(); @@ -119,7 +119,7 @@ class CreateUsersTable extends Migration { */ public function down() { - Schema::drop('users'); + Schema::connection('mysql')->drop('users'); } } @@ -151,7 +151,7 @@ class RemoveUserIdFromPostsTable extends Migration { */ public function up() { - Schema::table('posts', function(Blueprint $table) { + Schema::connection('mysql')->table('posts', function(Blueprint $table) { $table->dropColumn('user_id'); }); } @@ -163,7 +163,7 @@ class RemoveUserIdFromPostsTable extends Migration { */ public function down() { - Schema::table('posts', function(Blueprint $table) { + Schema::connection('mysql')->table('posts', function(Blueprint $table) { $table->integer('user_id'); }); } @@ -174,6 +174,7 @@ class RemoveUserIdFromPostsTable extends Migration { Here's a few other examples of commands that you might write: - `php artisan make:migration:schema create_posts_table` +- `php artisan make:migration:schema create_posts_table --schema="title:string" --database="other-mysql-connection"` - `php artisan make:migration:schema create_posts_table --schema="title:string, body:text, excerpt:string:nullable"` - `php artisan make:migration:schema remove_excerpt_from_posts_table --schema="excerpt:string:nullable"` @@ -187,6 +188,22 @@ php artisan make:migration:schema create_dogs_table --schema="name:string" You'll get a migration, populated with the schema...but you'll also get an Eloquent model at `app/Dog.php`. Naturally, you can opt out of this by adding the `--model=0` flag/option. +#### Options +There are some options to help you. + +##### Execute a migration in an different database +``` +php artisan make:migration:schema create_posts_table --schema="title:string" --database="other-mysql-connection" +``` + +Our schema should look like so: +``` +Schema::connection('other-mysql-connection')->create('posts', function(Blueprint $table) { + $table->increments('id'); + $table->string('title'); + $table->timestamps(); +); +``` #### Foreign Constraints There's also a secret bit of sugar for when you need to generate foreign constraints. Imagine that you have a posts table, where each post belongs to a user. Let's try: @@ -205,7 +222,7 @@ $table->foreign('user_id')->references('id')->on('users'); As such, for that full command, our schema should look like so: ``` -Schema::create('posts', function(Blueprint $table) { +Schema::connection('mysql')->create('posts', function(Blueprint $table) { $table->increments('id'); $table->integer('user_id'); $table->foreign('user_id')->references('id')->on('users'); @@ -242,7 +259,7 @@ class CreatePostTagPivotTable extends Migration { */ public function up() { - Schema::create('post_tag', function(Blueprint $table) + Schema::connection('mysql')->create('post_tag', function(Blueprint $table) { $table->integer('post_id')->unsigned()->index(); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); @@ -258,7 +275,7 @@ class CreatePostTagPivotTable extends Migration { */ public function down() { - Schema::drop('post_tag'); + Schema::connection('mysql')->drop('post_tag'); } } diff --git a/spec/Migrations/SyntaxBuilderSpec.php b/spec/Migrations/SyntaxBuilderSpec.php index 71ceb15..48ab7af 100644 --- a/spec/Migrations/SyntaxBuilderSpec.php +++ b/spec/Migrations/SyntaxBuilderSpec.php @@ -26,7 +26,7 @@ function it_creates_the_php_syntax_for_the_schema() ]]; $this->create($schema, ['table' => 'posts', 'action' => 'create'])['up']->shouldBe(getStub()); - $this->create($schema, ['table' => 'posts', 'action' => 'create'])['down']->shouldBe("Schema::drop('posts');"); + $this->create($schema, ['table' => 'posts', 'action' => 'create'])['down']->shouldBe("Schema::connection('mysql')->drop('posts');"); } } @@ -34,7 +34,7 @@ function it_creates_the_php_syntax_for_the_schema() function getStub() { return <<create('{{table}}', function (Blueprint \$table) { \$table->increments('id'); \$table->string('email', 100)->unique()->nullable()->default("foo@example.com"); \$table->timestamps(); diff --git a/src/Commands/MigrationMakeCommand.php b/src/Commands/MigrationMakeCommand.php index 800d1e5..60217a6 100644 --- a/src/Commands/MigrationMakeCommand.php +++ b/src/Commands/MigrationMakeCommand.php @@ -41,6 +41,13 @@ class MigrationMakeCommand extends Command */ protected $meta; + /** + * Database name for migration. + * + * @var string + */ + protected $database_name; + /** * @var Composer */ @@ -79,6 +86,8 @@ public function handle() public function fire() { $this->meta = (new NameParser)->parse($this->argument('name')); + $this->database_name = array_key_exists('database', $this->options()) ? $this->option('database') : 'mysql'; + $this->meta['database'] = $this->database_name; $this->makeMigration(); $this->makeModel(); @@ -176,6 +185,7 @@ protected function compileMigrationStub() $this->replaceClassName($stub) ->replaceSchema($stub) + ->replaceDatabaseName($stub) ->replaceTableName($stub); return $stub; @@ -211,6 +221,19 @@ protected function replaceTableName(&$stub) return $this; } + /** + * Replace the database name in the stub. + * + * @param string $stub + * @return $this + */ + protected function replaceDatabaseName(&$stub) + { + $stub = str_replace('{{database}}', $this->database_name, $stub); + + return $this; + } + /** * Replace the schema for the stub. * @@ -262,6 +285,7 @@ protected function getOptions() return [ ['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null], ['model', null, InputOption::VALUE_OPTIONAL, 'Want a model for this table?', true], + ['database', null, InputOption::VALUE_OPTIONAL, 'Optional database reference', 'mysql'], ]; } } diff --git a/src/Migrations/SyntaxBuilder.php b/src/Migrations/SyntaxBuilder.php index f820101..83db22b 100644 --- a/src/Migrations/SyntaxBuilder.php +++ b/src/Migrations/SyntaxBuilder.php @@ -71,7 +71,7 @@ private function createSchemaForDownMethod($schema, $meta) // If the user created a table, then for the down // method, we should drop it. if ($meta['action'] == 'create') { - return sprintf("Schema::drop('%s');", $meta['table']); + return sprintf("Schema::connection('%s')->drop('%s');", $meta['database'], $meta['table']); } // If the user added columns to a table, then for diff --git a/src/stubs/pivot.stub b/src/stubs/pivot.stub index 6604797..6c5764a 100644 --- a/src/stubs/pivot.stub +++ b/src/stubs/pivot.stub @@ -12,7 +12,7 @@ class {{class}} extends Migration */ public function up() { - Schema::create('{{pivotTableName}}', function (Blueprint $table) { + Schema::connection('{{database}}')->create('{{pivotTableName}}', function (Blueprint $table) { $table->integer('{{columnOne}}_id')->unsigned()->index(); $table->foreign('{{columnOne}}_id')->references('id')->on('{{tableOne}}')->onDelete('cascade'); $table->integer('{{columnTwo}}_id')->unsigned()->index(); @@ -28,6 +28,6 @@ class {{class}} extends Migration */ public function down() { - Schema::drop('{{pivotTableName}}'); + Schema::connection('{{database}}')->drop('{{pivotTableName}}'); } } diff --git a/src/stubs/schema-change.stub b/src/stubs/schema-change.stub index d0e297c..f3911b2 100644 --- a/src/stubs/schema-change.stub +++ b/src/stubs/schema-change.stub @@ -1,3 +1,3 @@ -Schema::table('{{table}}', function (Blueprint $table) { +Schema::connection('{{database}}')->table('{{table}}', function (Blueprint $table) { {{schema_up}} - }); \ No newline at end of file + }); diff --git a/src/stubs/schema-create.stub b/src/stubs/schema-create.stub index e1be930..361344f 100644 --- a/src/stubs/schema-create.stub +++ b/src/stubs/schema-create.stub @@ -1,4 +1,4 @@ -Schema::create('{{table}}', function (Blueprint $table) { +Schema::connection('{{database}}')->create('{{table}}', function (Blueprint $table) { $table->increments('id'); {{schema_up}} $table->timestamps();