diff --git a/src/Illuminate/Database/Console/Migrations/TableGuesser.php b/src/Illuminate/Database/Console/Migrations/TableGuesser.php index edcd706ff2d3..82dfbddbbceb 100644 --- a/src/Illuminate/Database/Console/Migrations/TableGuesser.php +++ b/src/Illuminate/Database/Console/Migrations/TableGuesser.php @@ -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. * @@ -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]; + } } } } diff --git a/tests/Database/DatabaseMigrationMakeCommandTest.php b/tests/Database/DatabaseMigrationMakeCommandTest.php index 3481c31d5f23..1e34388da41c 100755 --- a/tests/Database/DatabaseMigrationMakeCommandTest.php +++ b/tests/Database/DatabaseMigrationMakeCommandTest.php @@ -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']); @@ -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']); } @@ -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']); } diff --git a/tests/Database/TableGuesserTest.php b/tests/Database/TableGuesserTest.php index 6a6aecaa97c2..0a8bd2434c91 100644 --- a/tests/Database/TableGuesserTest.php +++ b/tests/Database/TableGuesserTest.php @@ -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); + } }