Skip to content

Commit

Permalink
PgSqlDriver: fixed reset failing to create schema [closes #22]
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Jul 22, 2015
1 parent 0cdbefb commit 438e750
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class PgSqlDriver extends BaseDriver implements IDriver
/** @var string */
protected $schema;

/** @var string */
protected $schemaStr;

/** @var string */
protected $primarySequence;

Expand All @@ -42,6 +45,7 @@ public function __construct(IDbal $dbal, $tableName = 'migrations', $schema = 'p
{
parent::__construct($dbal, $tableName);
$this->schema = $dbal->escapeIdentifier($schema);
$this->schemaStr = $dbal->escapeString($schema);
$this->primarySequence = $this->dbal->escapeString($tableName . '_id_seq');
$this->lockTableName = $dbal->escapeIdentifier($tableName . '_lock');
}
Expand Down Expand Up @@ -80,6 +84,17 @@ public function rollbackTransaction()
public function lock()
{
try {
$schemaExist = (bool) $this->dbal->query("
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = {$this->schemaStr}
");

if (!$schemaExist) {
// CREATE SCHEMA IF NOT EXIST is not available in PostgreSQL < 9.3
$this->dbal->exec("CREATE SCHEMA {$this->schema}");
}

$this->dbal->exec("CREATE TABLE {$this->schema}.{$this->lockTableName} (\"foo\" INT)");
} catch (\Exception $e) {
throw new LockException('Unable to acquire a lock.', NULL, $e);
Expand Down

0 comments on commit 438e750

Please sign in to comment.