From 8e8edd7f6843ed12908bd7a4783bcfc1d945ae7a Mon Sep 17 00:00:00 2001 From: frederik Date: Mon, 15 Dec 2014 12:54:57 +0100 Subject: [PATCH] bugfix: could not set different table name, subclasses had table name hard coded --- src/Adapters/AbstractPdoAdapter.php | 10 +++++++++- src/Adapters/PdoMysqlAdapter.php | 4 ++-- src/Adapters/PdoSqliteAdapter.php | 2 +- tests/Integration/MigrateTest.php | 22 ++++++++++++++++++---- tests/Unit/CollectionTest.php | 13 +++++++++++-- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Adapters/AbstractPdoAdapter.php b/src/Adapters/AbstractPdoAdapter.php index eea2d9c..62c3730 100644 --- a/src/Adapters/AbstractPdoAdapter.php +++ b/src/Adapters/AbstractPdoAdapter.php @@ -20,7 +20,7 @@ abstract class AbstractPdoAdapter implements AdapterInterface /** * @var string */ - private $table = 'migrations'; + protected $table = 'migrations'; /** * @param PDO $pdo @@ -30,6 +30,14 @@ public function __construct(PDO $pdo) $this->pdo = $pdo; } + /** + * @param $tableName + */ + public function setTableName ($tableName) + { + $this->table = $tableName; + } + /** * @return void */ diff --git a/src/Adapters/PdoMysqlAdapter.php b/src/Adapters/PdoMysqlAdapter.php index 0b766cc..d76e697 100644 --- a/src/Adapters/PdoMysqlAdapter.php +++ b/src/Adapters/PdoMysqlAdapter.php @@ -14,13 +14,13 @@ protected function createTableIfNotExists() { $this->getPdo()->exec( -'CREATE TABLE IF NOT EXISTS `migrations` ( +"CREATE TABLE IF NOT EXISTS `{$this->table}` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, `migration` VARCHAR(255) NOT NULL, `direction` TINYINT(1) UNSIGNED NOT NULL, `migrated_on` DATETIME NOT NULL, - PRIMARY KEY (`id`))' + PRIMARY KEY (`id`))" ); } diff --git a/src/Adapters/PdoSqliteAdapter.php b/src/Adapters/PdoSqliteAdapter.php index d14c0e7..24654c3 100644 --- a/src/Adapters/PdoSqliteAdapter.php +++ b/src/Adapters/PdoSqliteAdapter.php @@ -13,7 +13,7 @@ class PdoSqliteAdapter extends AbstractPdoAdapter protected function createTableIfNotExists() { $this->getPdo()->exec( - 'CREATE TABLE IF NOT EXISTS migrations(id INTEGER PRIMARY KEY ASC, name TEXT, migration TEXT, direction INTEGER, migrated_on TEXT)' + "CREATE TABLE IF NOT EXISTS {$this->table} (id INTEGER PRIMARY KEY ASC, name TEXT, migration TEXT, direction INTEGER, migrated_on TEXT)" ); } } diff --git a/tests/Integration/MigrateTest.php b/tests/Integration/MigrateTest.php index 74941a2..99e5505 100644 --- a/tests/Integration/MigrateTest.php +++ b/tests/Integration/MigrateTest.php @@ -1,10 +1,9 @@ assertEquals(1, $this->adapter->getNumberOfMigrations()); $this->setExpectedException(InvalidArgumentException::class); - $list = $this->factory->newListFromDirectory(__DIR__.'/namespaced', 'namespaced'); + $this->factory->newListFromDirectory(__DIR__.'/namespaced', 'namespaced'); } /** @@ -133,4 +132,19 @@ public function testCallback() $this->assertCount(1, $result); } + + /** + * + */ + public function testDifferentTableName () + { + $this->adapter->setTableName('other_table_name'); + + $list = $this->factory->newListFromDirectory(__DIR__.'/migrations'); + $this->assertCount(2, $list); + + $result = $list->migrate(); + $this->assertCount(2, $result); + $this->assertEquals(2, $this->adapter->getNumberOfMigrations()); + } } diff --git a/tests/Unit/CollectionTest.php b/tests/Unit/CollectionTest.php index 6be08de..ed3df7c 100644 --- a/tests/Unit/CollectionTest.php +++ b/tests/Unit/CollectionTest.php @@ -16,20 +16,29 @@ class CollectionTest extends AbstractTestCase * @var Collection */ private $collection; - + + /** + * + */ protected function setUp() { $this->collection = new Collection( new PdoSqliteAdapter(new PDO('sqlite::memory:')) ); } - + + /** + * + */ public function testAttach() { $this->collection->attach(new TestMigration()); $this->assertCount(1, $this->collection); } + /** + * + */ public function testDetach() { $script = new TestMigration();