diff --git a/src/Drivers/BaseDriver.php b/src/Drivers/BaseDriver.php index bd5e042..ff48771 100644 --- a/src/Drivers/BaseDriver.php +++ b/src/Drivers/BaseDriver.php @@ -27,6 +27,9 @@ abstract class BaseDriver implements IDriver /** @var string */ protected $tableName; + /** @var NULL|string */ + protected $tableNameQuoted; + /** * @param IDbal $dbal @@ -35,7 +38,13 @@ abstract class BaseDriver implements IDriver public function __construct(IDbal $dbal, $tableName = 'migrations') { $this->dbal = $dbal; - $this->tableName = $dbal->escapeIdentifier($tableName); + $this->tableName = $tableName; + } + + + public function setupConnection() + { + $this->tableNameQuoted = $this->dbal->escapeIdentifier($this->tableName); } diff --git a/src/Drivers/MySqlDriver.php b/src/Drivers/MySqlDriver.php index 7c961e7..d786e23 100644 --- a/src/Drivers/MySqlDriver.php +++ b/src/Drivers/MySqlDriver.php @@ -24,6 +24,7 @@ class MySqlDriver extends BaseDriver implements IDriver { public function setupConnection() { + parent::setupConnection(); $this->dbal->exec('SET NAMES "utf8"'); $this->dbal->exec('SET foreign_key_checks = 0'); $this->dbal->exec('SET time_zone = "SYSTEM"'); @@ -91,14 +92,14 @@ public function createTable() public function dropTable() { - $this->dbal->exec("DROP TABLE {$this->tableName}"); + $this->dbal->exec("DROP TABLE {$this->tableNameQuoted}"); } public function insertMigration(Migration $migration) { $this->dbal->exec(" - INSERT INTO {$this->tableName} + INSERT INTO {$this->tableNameQuoted} (`group`, `file`, `checksum`, `executed`, `ready`) VALUES (" . $this->dbal->escapeString($migration->group) . "," . $this->dbal->escapeString($migration->filename) . "," . @@ -115,7 +116,7 @@ public function insertMigration(Migration $migration) public function markMigrationAsReady(Migration $migration) { $this->dbal->exec(" - UPDATE {$this->tableName} + UPDATE {$this->tableNameQuoted} SET `ready` = 1 WHERE `id` = " . $this->dbal->escapeInt($migration->id) ); @@ -125,7 +126,7 @@ public function markMigrationAsReady(Migration $migration) public function getAllMigrations() { $migrations = array(); - $result = $this->dbal->query("SELECT * FROM {$this->tableName} ORDER BY `executed`"); + $result = $this->dbal->query("SELECT * FROM {$this->tableNameQuoted} ORDER BY `executed`"); foreach ($result as $row) { $migration = new Migration; $migration->id = (int) $row['id']; @@ -145,7 +146,7 @@ public function getAllMigrations() public function getInitTableSource() { return preg_replace('#^\t{3}#m', '', trim(" - CREATE TABLE IF NOT EXISTS {$this->tableName} ( + CREATE TABLE IF NOT EXISTS {$this->tableNameQuoted} ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `group` varchar(100) NOT NULL, `file` varchar(100) NOT NULL, @@ -163,7 +164,7 @@ public function getInitMigrationsSource(array $files) { $out = ''; foreach ($files as $file) { - $out .= "INSERT INTO {$this->tableName} "; + $out .= "INSERT INTO {$this->tableNameQuoted} "; $out .= "(`group`, `file`, `checksum`, `executed`, `ready`) VALUES (" . $this->dbal->escapeString($file->group->name) . ", " . $this->dbal->escapeString($file->name) . ", " . diff --git a/src/Drivers/PgSqlDriver.php b/src/Drivers/PgSqlDriver.php index 2c2e347..63ea8a9 100644 --- a/src/Drivers/PgSqlDriver.php +++ b/src/Drivers/PgSqlDriver.php @@ -26,8 +26,8 @@ class PgSqlDriver extends BaseDriver implements IDriver /** @var string */ protected $schema; - /** @var string */ - protected $schemaStr; + /** @var NULL|string */ + protected $schemaQuoted; /** @@ -38,20 +38,21 @@ class PgSqlDriver extends BaseDriver implements IDriver public function __construct(IDbal $dbal, $tableName = 'migrations', $schema = 'public') { parent::__construct($dbal, $tableName); - $this->schema = $dbal->escapeIdentifier($schema); - $this->schemaStr = $dbal->escapeString($schema); + $this->schema = $schema; } public function setupConnection() { + parent::setupConnection(); + $this->schemaQuoted = $this->dbal->escapeIdentifier($this->schema); } public function emptyDatabase() { - $this->dbal->exec("DROP SCHEMA IF EXISTS {$this->schema} CASCADE"); - $this->dbal->exec("CREATE SCHEMA {$this->schema}"); + $this->dbal->exec("DROP SCHEMA IF EXISTS {$this->schemaQuoted} CASCADE"); + $this->dbal->exec("CREATE SCHEMA {$this->schemaQuoted}"); } @@ -103,14 +104,14 @@ public function createTable() public function dropTable() { - $this->dbal->exec("DROP TABLE {$this->schema}.{$this->tableName}"); + $this->dbal->exec("DROP TABLE {$this->schemaQuoted}.{$this->tableNameQuoted}"); } public function insertMigration(Migration $migration) { $rows = $this->dbal->query(" - INSERT INTO {$this->schema}.{$this->tableName}" . ' + INSERT INTO {$this->schemaQuoted}.{$this->tableNameQuoted}" . ' ("group", "file", "checksum", "executed", "ready") VALUES (' . $this->dbal->escapeString($migration->group) . "," . $this->dbal->escapeString($migration->filename) . "," . @@ -128,7 +129,7 @@ public function insertMigration(Migration $migration) public function markMigrationAsReady(Migration $migration) { $this->dbal->exec(" - UPDATE {$this->schema}.{$this->tableName}" . ' + UPDATE {$this->schemaQuoted}.{$this->tableNameQuoted}" . ' SET "ready" = TRUE WHERE "id" = ' . $this->dbal->escapeInt($migration->id) ); @@ -138,7 +139,7 @@ public function markMigrationAsReady(Migration $migration) public function getAllMigrations() { $migrations = array(); - $result = $this->dbal->query("SELECT * FROM {$this->schema}.{$this->tableName} ORDER BY \"executed\""); + $result = $this->dbal->query("SELECT * FROM {$this->schemaQuoted}.{$this->tableNameQuoted} ORDER BY \"executed\""); foreach ($result as $row) { $migration = new Migration; $migration->id = (int) $row['id']; @@ -158,7 +159,7 @@ public function getAllMigrations() public function getInitTableSource() { return preg_replace('#^\t{3}#m', '', trim(" - CREATE TABLE IF NOT EXISTS {$this->schema}.{$this->tableName} (" . ' + CREATE TABLE IF NOT EXISTS {$this->schemaQuoted}.{$this->tableNameQuoted} (" . ' "id" serial4 NOT NULL, "group" varchar(100) NOT NULL, "file" varchar(100) NOT NULL, @@ -176,7 +177,7 @@ public function getInitMigrationsSource(array $files) { $out = ''; foreach ($files as $file) { - $out .= "INSERT INTO {$this->schema}.{$this->tableName} "; + $out .= "INSERT INTO {$this->schemaQuoted}.{$this->tableNameQuoted} "; $out .= '("group", "file", "checksum", "executed", "ready") VALUES (' . $this->dbal->escapeString($file->group->name) . ", " . $this->dbal->escapeString($file->name) . ", " . diff --git a/tests/inc/IntegrationTestCase.php b/tests/inc/IntegrationTestCase.php index 6fca3a9..b78d207 100644 --- a/tests/inc/IntegrationTestCase.php +++ b/tests/inc/IntegrationTestCase.php @@ -58,6 +58,8 @@ protected function setUp() $initDb(); $this->driver = $this->createDriver($options['driver'], $this->dbal); + $this->driver->setupConnection(); + $this->printer = $this->createPrinter(); $this->runner = new Runner($this->driver, $this->printer);