diff --git a/src/Drivers/PgSqlDriver.php b/src/Drivers/PgSqlDriver.php index 1332646..268f1f3 100644 --- a/src/Drivers/PgSqlDriver.php +++ b/src/Drivers/PgSqlDriver.php @@ -26,6 +26,9 @@ class PgSqlDriver extends BaseDriver implements IDriver /** @var string */ protected $schema; + /** @var string */ + protected $schemaStr; + /** @var string */ protected $primarySequence; @@ -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'); } @@ -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);