Skip to content
Closed
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
14 changes: 2 additions & 12 deletions src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Console\Migrations;

use Illuminate\Support\Str;
use Illuminate\Support\Composer;
use Illuminate\Database\Migrations\MigrationCreator;

Expand Down Expand Up @@ -63,7 +64,7 @@ public function handle()
// It's possible for the developer to specify the tables to modify in this
// schema operation. The developer may also specify if this table needs
// to be freshly created so we can create the appropriate migrations.
$name = trim($this->input->getArgument('name'));
$name = Str::snake(trim($this->input->getArgument('name')));

$table = $this->input->getOption('table');

Expand All @@ -78,17 +79,6 @@ public function handle()
$create = true;
}

// Next, we will attempt to guess the table name if this the migration has
// "create" in the name. This will allow us to provide a convenient way
// of creating migrations that create new tables for the application.
if (! $table) {
if (preg_match('/^create_(\w+)_table$/', $name, $matches)) {
$table = $matches[1];

$create = true;
}
}

// Now we are ready to write the migration out to disk. Once we've written
// the migration out, we will dump-autoload for the entire framework to
// make sure that the migrations are registered by the class loaders.
Expand Down
152 changes: 125 additions & 27 deletions src/Illuminate/Database/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ public function create($name, $path, $table = null, $create = false)
{
$this->ensureMigrationDoesntAlreadyExist($name);

// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
// various place-holders, save the file, and run the post create event.
$stub = $this->getStub($table, $create);
// First we will get the file path for the migration and generate the contents
// dynamically based on the arguments provided by the developer. Once we
// have these we will save the file and fire the post create event.
$path = $this->getPath($name, $path);
$content = $this->getContent($name, $table, $create);

$this->files->put(
$path = $this->getPath($name, $path),
$this->populateStub($name, $stub, $table)
);
$this->files->put($path, $content);

// Next, we will fire any hooks that are supposed to fire after a migration is
// created. Once that is done we'll be ready to return the full path to the
Expand All @@ -67,7 +65,7 @@ public function create($name, $path, $table = null, $create = false)
}

/**
* Ensure that a migration with the given name doesn't already exist.
* Ensure that a migration with the given name does not already exist.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this line?

Copy link

@MarkVaughn MarkVaughn Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because typically contractions are used in conversational English and avoided in formal writing. But it's not a big deal, seems like a nit-picky thing to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never met a technical writer that dislikes contractions being used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if that were the case, why change it in this PR? Make a separate PR for it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He wrote and rewrote a bunch of comments in this PR. Are you seriously that offended by it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying it's right or wrong. Nor that it needs to be changed or not. I was simply adding to the idea of "formal writing" that I have yet (in like 6 years of working with technical writers) met one that dislikes their use. Which means, it can't be that informal if few who write professionally in the tech industry seems to really care in either direction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not offended by anything? I'm just saying it's a useless change and not really in the scope of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trivial change. Honestly one that was either automated by my IDE or I did inadvertently when reviewing comments. Glad to revert it if additional changes are required for this PR.

*
* @param string $name
* @return void
Expand All @@ -81,49 +79,89 @@ protected function ensureMigrationDoesntAlreadyExist($name)
}
}

/**
* Generate the content for the migration file.
*
* @param string $name
* @param string $table
* @param bool $create
* @return string
*/
protected function getContent($name, $table, $create)
{
$stub = $this->getStub($name, $table, $create);
$placeholders = $this->getPlaceholders($name, $table);

// Here we will replace the any place-holders with the values specified by
// the developer, which is useful for quickly creating a tables creation
// or update migration from the console instead of typing it manually.

return $this->populateStub($stub, $placeholders);
}

/**
* Get the migration stub file.
*
* @param string $name
* @param string $table
* @param bool $create
* @return string
*/
protected function getStub($table, $create)
protected function getStub($name, $table, $create)
{
if (is_null($table)) {
return $this->files->get($this->stubPath().'/blank.stub');
// We also have stubs for creating, dropping, renaming tables as well as
// adding, removing, or renaming columns. This saves the developer
// some typing when they making migrations.

if ($create) {
return $this->files->get($this->stubPath().'/create.stub');
}

// We also have stubs for creating new tables and modifying existing tables
// to save the developer some typing when they are creating a new tables
// or modifying existing tables. We'll grab the appropriate stub here.
else {
$stub = $create ? 'create.stub' : 'update.stub';
if ($this->nameFollowsConvention($name)) {
return $this->files->get($this->stubPath().'/'.$this->extractStubFromName($name));
}

return $this->files->get($this->stubPath()."/{$stub}");
if (! is_null($table)) {
return $this->files->get($this->stubPath().'/update.stub');
}

return $this->files->get($this->stubPath().'/blank.stub');
}

/**
* Populate the place-holders in the migration stub.
*
* @param string $name
* @param string $stub
* @param string $table
* @param array $placeholders
* @return string
*/
protected function populateStub($name, $stub, $table)
protected function populateStub($stub, array $placeholders)
{
$stub = str_replace('DummyClass', $this->getClassName($name), $stub);
return str_replace(array_keys($placeholders), $placeholders, $stub);
}

/**
* Determine the place-holders and values for the migration stub.
*
* @param string $name
* @param string $table
* @return array
*/
protected function getPlaceholders($name, $table)
{
$placeholders = [
'DummyClass' => $this->getClassName($name),
];

if ($this->nameFollowsConvention($name)) {
$placeholders += $this->extractPlaceholderValuesFromName($name);
}

// Here we will replace the table place-holders with the table specified by
// the developer, which is useful for quickly creating a tables creation
// or update migration from the console instead of typing it manually.
if (! is_null($table)) {
$stub = str_replace('DummyTable', $table, $stub);
$placeholders['DummyTable'] = $table;
}

return $stub;
return $placeholders;
}

/**
Expand All @@ -149,6 +187,66 @@ protected function getPath($name, $path)
return $path.'/'.$this->getDatePrefix().'_'.$name.'.php';
}

/**
* Does the migration name follow one of the convenient naming conventions.
*
* @param string $name
* @return bool
*/
protected function nameFollowsConvention($name)
{
return preg_match('/(create|drop)_\w+/', $name)
|| preg_match('/(rename|add)_\w+_to_\w+/', $name)
|| preg_match('/remove_\w+_from_\w+/', $name);
}

/**
* Extract the stub file name from the migration name.
*
* @param string $name
* @return bool
*/
protected function extractStubFromName($name)
{
$stub = Str::before($name, '_');

if ($stub === 'rename') {
$stub = Str::contains($name, '_in_') ? 'rename-column' : 'rename-table';
}

return $stub.'.stub';
}

/**
* Extract the place-holders from the migration name.
*
* @param string $name
* @return array
*/
protected function extractPlaceholderValuesFromName($name)
{
$patterns = [
'create_(?P<DummyTable>\w+)',
'drop_(?P<DummyTable>\w+)',
'rename_(?P<DummyColumnFrom>\w+)_to_(?P<DummyColumnTo>\w+)_in_(?P<DummyTable>\w+)',
'rename_(?P<DummyTableFrom>\w+)_to_(?P<DummyTableTo>\w+)',
'add_(?P<DummyColumn>\w+)_to_(?P<DummyTable>\w+)',
'remove_(?P<DummyColumn>\w+)_from_(?P<DummyTable>\w+)',
];

preg_match('/'.implode('|', $patterns).'/J', $name, $matches);

$placeholders = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);

array_walk($placeholders, function (&$value, $key) {
if (Str::startsWith($key, 'DummyTable') && Str::endsWith($value, '_table')) {
$value = Str::replaceLast('_table', '', $value);
}
});

return array_filter($placeholders);
}

/**
* Fire the registered post create hooks.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Migrations/stubs/add.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->string('DummyColumn')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->dropColumn('DummyColumn');
});
}
}
31 changes: 31 additions & 0 deletions src/Illuminate/Database/Migrations/stubs/drop.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('DummyTable');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('DummyTable', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
}
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Migrations/stubs/remove.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->dropColumn('DummyColumn');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->string('DummyColumn')->nullable();
});
}
}
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Migrations/stubs/rename-column.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->renameColumn('DummyColumnFrom', 'DummyColumnTo');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('DummyTable', function (Blueprint $table) {
$table->renameColumn('DummyColumnTo', 'DummyColumnFrom');
});
}
}
Loading