Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/database name #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -119,7 +119,7 @@ class CreateUsersTable extends Migration {
*/
public function down()
{
Schema::drop('users');
Schema::connection('mysql')->drop('users');
}

}
Expand Down Expand Up @@ -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');
});
}
Expand All @@ -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');
});
}
Expand All @@ -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"`

Expand All @@ -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:
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -258,7 +275,7 @@ class CreatePostTagPivotTable extends Migration {
*/
public function down()
{
Schema::drop('post_tag');
Schema::connection('mysql')->drop('post_tag');
}

}
Expand Down
4 changes: 2 additions & 2 deletions spec/Migrations/SyntaxBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ 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');");
}

}

function getStub()
{
return <<<EOT
Schema::create('{{table}}', function (Blueprint \$table) {
Schema::connection('mysql')->create('{{table}}', function (Blueprint \$table) {
\$table->increments('id');
\$table->string('email', 100)->unique()->nullable()->default("[email protected]");
\$table->timestamps();
Expand Down
24 changes: 24 additions & 0 deletions src/Commands/MigrationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class MigrationMakeCommand extends Command
*/
protected $meta;

/**
* Database name for migration.
*
* @var string
*/
protected $database_name;

/**
* @var Composer
*/
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -176,6 +185,7 @@ protected function compileMigrationStub()

$this->replaceClassName($stub)
->replaceSchema($stub)
->replaceDatabaseName($stub)
->replaceTableName($stub);

return $stub;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'],
];
}
}
2 changes: 1 addition & 1 deletion src/Migrations/SyntaxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/stubs/pivot.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -28,6 +28,6 @@ class {{class}} extends Migration
*/
public function down()
{
Schema::drop('{{pivotTableName}}');
Schema::connection('{{database}}')->drop('{{pivotTableName}}');
}
}
4 changes: 2 additions & 2 deletions src/stubs/schema-change.stub
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Schema::table('{{table}}', function (Blueprint $table) {
Schema::connection('{{database}}')->table('{{table}}', function (Blueprint $table) {
{{schema_up}}
});
});
2 changes: 1 addition & 1 deletion src/stubs/schema-create.stub
Original file line number Diff line number Diff line change
@@ -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();
Expand Down