Permalink
Browse files

PgSqlDriver: used advisory locks

  • Loading branch information...
JanTvrdik committed Jul 28, 2017
1 parent d13a2d3 commit 639c6e48459906ef2abbb81419f59e0ad0da618b
Showing with 4 additions and 17 deletions.
  1. +4 −17 src/Drivers/PgSqlDriver.php
@@ -32,9 +32,6 @@ class PgSqlDriver extends BaseDriver implements IDriver
/** @var string */
protected $primarySequence;
/** @var string */
protected $lockTableName;
/**
* @param IDbal $dbal
@@ -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');
}
@@ -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.

Show comment
Hide comment
@hrach

hrach Aug 3, 2017

Member

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

@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.

Show comment
Hide comment
@JanTvrdik

JanTvrdik Aug 3, 2017

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.

@JanTvrdik

JanTvrdik Aug 3, 2017

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);
}
@@ -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);
}

0 comments on commit 639c6e4

Please sign in to comment.