Skip to content

Commit 36fd4b7

Browse files
committed
{My,Pg}SqlDriver: do not connect to database in constructor
[closes #72]
1 parent 3c08349 commit 36fd4b7

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/Drivers/BaseDriver.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ abstract class BaseDriver implements IDriver
2727
/** @var string */
2828
protected $tableName;
2929

30+
/** @var NULL|string */
31+
protected $tableNameQuoted;
32+
3033

3134
/**
3235
* @param IDbal $dbal
@@ -35,7 +38,13 @@ abstract class BaseDriver implements IDriver
3538
public function __construct(IDbal $dbal, $tableName = 'migrations')
3639
{
3740
$this->dbal = $dbal;
38-
$this->tableName = $dbal->escapeIdentifier($tableName);
41+
$this->tableName = $tableName;
42+
}
43+
44+
45+
public function setupConnection()
46+
{
47+
$this->tableNameQuoted = $this->dbal->escapeIdentifier($this->tableName);
3948
}
4049

4150

src/Drivers/MySqlDriver.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class MySqlDriver extends BaseDriver implements IDriver
2424
{
2525
public function setupConnection()
2626
{
27+
parent::setupConnection();
2728
$this->dbal->exec('SET NAMES "utf8"');
2829
$this->dbal->exec('SET foreign_key_checks = 0');
2930
$this->dbal->exec('SET time_zone = "SYSTEM"');
@@ -91,14 +92,14 @@ public function createTable()
9192

9293
public function dropTable()
9394
{
94-
$this->dbal->exec("DROP TABLE {$this->tableName}");
95+
$this->dbal->exec("DROP TABLE {$this->tableNameQuoted}");
9596
}
9697

9798

9899
public function insertMigration(Migration $migration)
99100
{
100101
$this->dbal->exec("
101-
INSERT INTO {$this->tableName}
102+
INSERT INTO {$this->tableNameQuoted}
102103
(`group`, `file`, `checksum`, `executed`, `ready`) VALUES (" .
103104
$this->dbal->escapeString($migration->group) . "," .
104105
$this->dbal->escapeString($migration->filename) . "," .
@@ -115,7 +116,7 @@ public function insertMigration(Migration $migration)
115116
public function markMigrationAsReady(Migration $migration)
116117
{
117118
$this->dbal->exec("
118-
UPDATE {$this->tableName}
119+
UPDATE {$this->tableNameQuoted}
119120
SET `ready` = 1
120121
WHERE `id` = " . $this->dbal->escapeInt($migration->id)
121122
);
@@ -125,7 +126,7 @@ public function markMigrationAsReady(Migration $migration)
125126
public function getAllMigrations()
126127
{
127128
$migrations = array();
128-
$result = $this->dbal->query("SELECT * FROM {$this->tableName} ORDER BY `executed`");
129+
$result = $this->dbal->query("SELECT * FROM {$this->tableNameQuoted} ORDER BY `executed`");
129130
foreach ($result as $row) {
130131
$migration = new Migration;
131132
$migration->id = (int) $row['id'];
@@ -145,7 +146,7 @@ public function getAllMigrations()
145146
public function getInitTableSource()
146147
{
147148
return preg_replace('#^\t{3}#m', '', trim("
148-
CREATE TABLE IF NOT EXISTS {$this->tableName} (
149+
CREATE TABLE IF NOT EXISTS {$this->tableNameQuoted} (
149150
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
150151
`group` varchar(100) NOT NULL,
151152
`file` varchar(100) NOT NULL,
@@ -163,7 +164,7 @@ public function getInitMigrationsSource(array $files)
163164
{
164165
$out = '';
165166
foreach ($files as $file) {
166-
$out .= "INSERT INTO {$this->tableName} ";
167+
$out .= "INSERT INTO {$this->tableNameQuoted} ";
167168
$out .= "(`group`, `file`, `checksum`, `executed`, `ready`) VALUES (" .
168169
$this->dbal->escapeString($file->group->name) . ", " .
169170
$this->dbal->escapeString($file->name) . ", " .

src/Drivers/PgSqlDriver.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class PgSqlDriver extends BaseDriver implements IDriver
2626
/** @var string */
2727
protected $schema;
2828

29-
/** @var string */
30-
protected $schemaStr;
29+
/** @var NULL|string */
30+
protected $schemaQuoted;
3131

3232

3333
/**
@@ -38,20 +38,21 @@ class PgSqlDriver extends BaseDriver implements IDriver
3838
public function __construct(IDbal $dbal, $tableName = 'migrations', $schema = 'public')
3939
{
4040
parent::__construct($dbal, $tableName);
41-
$this->schema = $dbal->escapeIdentifier($schema);
42-
$this->schemaStr = $dbal->escapeString($schema);
41+
$this->schema = $schema;
4342
}
4443

4544

4645
public function setupConnection()
4746
{
47+
parent::setupConnection();
48+
$this->schemaQuoted = $this->dbal->escapeIdentifier($this->schema);
4849
}
4950

5051

5152
public function emptyDatabase()
5253
{
53-
$this->dbal->exec("DROP SCHEMA IF EXISTS {$this->schema} CASCADE");
54-
$this->dbal->exec("CREATE SCHEMA {$this->schema}");
54+
$this->dbal->exec("DROP SCHEMA IF EXISTS {$this->schemaQuoted} CASCADE");
55+
$this->dbal->exec("CREATE SCHEMA {$this->schemaQuoted}");
5556
}
5657

5758

@@ -103,14 +104,14 @@ public function createTable()
103104

104105
public function dropTable()
105106
{
106-
$this->dbal->exec("DROP TABLE {$this->schema}.{$this->tableName}");
107+
$this->dbal->exec("DROP TABLE {$this->schemaQuoted}.{$this->tableNameQuoted}");
107108
}
108109

109110

110111
public function insertMigration(Migration $migration)
111112
{
112113
$rows = $this->dbal->query("
113-
INSERT INTO {$this->schema}.{$this->tableName}" . '
114+
INSERT INTO {$this->schemaQuoted}.{$this->tableNameQuoted}" . '
114115
("group", "file", "checksum", "executed", "ready") VALUES (' .
115116
$this->dbal->escapeString($migration->group) . "," .
116117
$this->dbal->escapeString($migration->filename) . "," .
@@ -128,7 +129,7 @@ public function insertMigration(Migration $migration)
128129
public function markMigrationAsReady(Migration $migration)
129130
{
130131
$this->dbal->exec("
131-
UPDATE {$this->schema}.{$this->tableName}" . '
132+
UPDATE {$this->schemaQuoted}.{$this->tableNameQuoted}" . '
132133
SET "ready" = TRUE
133134
WHERE "id" = ' . $this->dbal->escapeInt($migration->id)
134135
);
@@ -138,7 +139,7 @@ public function markMigrationAsReady(Migration $migration)
138139
public function getAllMigrations()
139140
{
140141
$migrations = array();
141-
$result = $this->dbal->query("SELECT * FROM {$this->schema}.{$this->tableName} ORDER BY \"executed\"");
142+
$result = $this->dbal->query("SELECT * FROM {$this->schemaQuoted}.{$this->tableNameQuoted} ORDER BY \"executed\"");
142143
foreach ($result as $row) {
143144
$migration = new Migration;
144145
$migration->id = (int) $row['id'];
@@ -158,7 +159,7 @@ public function getAllMigrations()
158159
public function getInitTableSource()
159160
{
160161
return preg_replace('#^\t{3}#m', '', trim("
161-
CREATE TABLE IF NOT EXISTS {$this->schema}.{$this->tableName} (" . '
162+
CREATE TABLE IF NOT EXISTS {$this->schemaQuoted}.{$this->tableNameQuoted} (" . '
162163
"id" serial4 NOT NULL,
163164
"group" varchar(100) NOT NULL,
164165
"file" varchar(100) NOT NULL,
@@ -176,7 +177,7 @@ public function getInitMigrationsSource(array $files)
176177
{
177178
$out = '';
178179
foreach ($files as $file) {
179-
$out .= "INSERT INTO {$this->schema}.{$this->tableName} ";
180+
$out .= "INSERT INTO {$this->schemaQuoted}.{$this->tableNameQuoted} ";
180181
$out .= '("group", "file", "checksum", "executed", "ready") VALUES (' .
181182
$this->dbal->escapeString($file->group->name) . ", " .
182183
$this->dbal->escapeString($file->name) . ", " .

tests/inc/IntegrationTestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ protected function setUp()
5858
$initDb();
5959

6060
$this->driver = $this->createDriver($options['driver'], $this->dbal);
61+
$this->driver->setupConnection();
62+
6163
$this->printer = $this->createPrinter();
6264
$this->runner = new Runner($this->driver, $this->printer);
6365

0 commit comments

Comments
 (0)