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

[5.7] Allow migration table name to be guessed without _table suffix #26429

Merged
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
22 changes: 18 additions & 4 deletions src/Illuminate/Database/Console/Migrations/TableGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

class TableGuesser
{
const CREATE_PATTERNS = [
'/^create_(\w+)_table$/',
'/^create_(\w+)$/',
];

const CHANGE_PATTERNS = [
'/_(to|from|in)_(\w+)_table$/',
'/_(to|from|in)_(\w+)$/',
];

/**
* Attempt to guess the table name and "creation" status of the given migration.
*
Expand All @@ -12,12 +22,16 @@ class TableGuesser
*/
public static function guess($migration)
{
if (preg_match('/^create_(\w+)_table$/', $migration, $matches)) {
return [$matches[1], $create = true];
foreach (self::CREATE_PATTERNS as $pattern) {
if (preg_match($pattern, $migration, $matches)) {
return [$matches[1], $create = true];
}
}

if (preg_match('/_(to|from|in)_(\w+)_table$/', $migration, $matches)) {
return [$matches[2], $create = false];
foreach (self::CHANGE_PATTERNS as $pattern) {
if (preg_match($pattern, $migration, $matches)) {
return [$matches[2], $create = false];
}
}
}
}
6 changes: 3 additions & 3 deletions tests/Database/DatabaseMigrationMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testBasicCreateDumpsAutoload()
$app = new Application;
$app->useDatabasePath(__DIR__);
$command->setLaravel($app);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', null, false);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true);
$composer->shouldReceive('dumpAutoloads')->once();

$this->runCommand($command, ['name' => 'create_foo']);
Expand All @@ -42,7 +42,7 @@ public function testBasicCreateGivesCreatorProperArguments()
$app = new Application;
$app->useDatabasePath(__DIR__);
$command->setLaravel($app);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', null, false);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true);

$this->runCommand($command, ['name' => 'create_foo']);
}
Expand All @@ -56,7 +56,7 @@ public function testBasicCreateGivesCreatorProperArgumentsWhenNameIsStudlyCase()
$app = new Application;
$app->useDatabasePath(__DIR__);
$command->setLaravel($app);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', null, false);
$creator->shouldReceive('create')->once()->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true);

$this->runCommand($command, ['name' => 'CreateFoo']);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/TableGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,31 @@ public function test_migration_is_properly_parsed()
$this->assertEquals('users', $table);
$this->assertFalse($create);

[$table, $create] = TableGuesser::guess('change_status_column_in_users_table');
$this->assertEquals('users', $table);
$this->assertFalse($create);

[$table, $create] = TableGuesser::guess('drop_status_column_from_users_table');
$this->assertEquals('users', $table);
$this->assertFalse($create);
}

public function test_migration_is_properly_parsed_without_table_suffix()
{
[$table, $create] = TableGuesser::guess('create_users');
$this->assertEquals('users', $table);
$this->assertTrue($create);

[$table, $create] = TableGuesser::guess('add_status_column_to_users');
$this->assertEquals('users', $table);
$this->assertFalse($create);

[$table, $create] = TableGuesser::guess('change_status_column_in_users');
$this->assertEquals('users', $table);
$this->assertFalse($create);

[$table, $create] = TableGuesser::guess('drop_status_column_from_users');
$this->assertEquals('users', $table);
$this->assertFalse($create);
}
}