Skip to content

Commit

Permalink
PgSqlDriver: used advisory locks
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Jul 28, 2017
1 parent d13a2d3 commit 639c6e4
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class PgSqlDriver extends BaseDriver implements IDriver
/** @var string */
protected $primarySequence;

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


/**
* @param IDbal $dbal
Expand All @@ -47,7 +44,6 @@ public function __construct(IDbal $dbal, $tableName = 'migrations', $schema = 'p
$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 @@ -84,18 +80,8 @@ 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)");
$this->dbal->exec('SELECT pg_advisory_lock(-2099128779216184107)');

This comment has been minimized.

Copy link
@hrach

hrach Aug 3, 2017

Member

some description should be added with the weird number explanation! @JanTvrdik (please add it)

This comment has been minimized.

Copy link
@JanTvrdik

JanTvrdik Aug 3, 2017

Author Member

Well, postgre does not support named locks. This could be any 64bit number but for fun it was generated from first 64 bits of md5 hash for Nextras Migrations string. Or sth like that.


} catch (\Exception $e) {
throw new LockException('Unable to acquire a lock.', NULL, $e);
}
Expand All @@ -105,7 +91,8 @@ public function lock()
public function unlock()
{
try {
$this->dbal->exec("DROP TABLE IF EXISTS {$this->schema}.{$this->lockTableName}");
$this->dbal->exec('SELECT pg_advisory_unlock(-2099128779216184107)');

} catch (\Exception $e) {
throw new LockException('Unable to release a lock.', NULL, $e);
}
Expand Down

0 comments on commit 639c6e4

Please sign in to comment.