Skip to content

Commit

Permalink
{My,Pg}SqlDriver: do not connect to database in constructor
Browse files Browse the repository at this point in the history
[closes #72]
  • Loading branch information
JanTvrdik committed Oct 2, 2017
1 parent fb80c71 commit d0ab005
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
11 changes: 10 additions & 1 deletion src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ abstract class BaseDriver implements IDriver
/** @var string */
protected $tableName;

/** @var NULL|string */
protected $tableNameQuoted;


/**
* @param IDbal $dbal
Expand All @@ -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);
}


Expand Down
13 changes: 7 additions & 6 deletions src/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
Expand Down Expand Up @@ -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) . "," .
Expand All @@ -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)
);
Expand All @@ -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'];
Expand All @@ -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,
Expand All @@ -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) . ", " .
Expand Down
25 changes: 13 additions & 12 deletions src/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class PgSqlDriver extends BaseDriver implements IDriver
/** @var string */
protected $schema;

/** @var string */
protected $schemaStr;
/** @var NULL|string */
protected $schemaQuoted;


/**
Expand All @@ -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}");
}


Expand Down Expand Up @@ -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) . "," .
Expand All @@ -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)
);
Expand All @@ -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'];
Expand All @@ -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,
Expand All @@ -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) . ", " .
Expand Down
2 changes: 2 additions & 0 deletions tests/inc/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d0ab005

Please sign in to comment.