Skip to content

Commit

Permalink
PgSqlDrive: fetch id of inserted migration with RETURNING clause
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Oct 2, 2017
1 parent f27759e commit fb80c71
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class PgSqlDriver extends BaseDriver implements IDriver
/** @var string */
protected $schemaStr;

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


/**
* @param IDbal $dbal
Expand All @@ -43,7 +40,6 @@ 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');
}


Expand Down Expand Up @@ -113,7 +109,7 @@ public function dropTable()

public function insertMigration(Migration $migration)
{
$this->dbal->exec("
$rows = $this->dbal->query("
INSERT INTO {$this->schema}.{$this->tableName}" . '
("group", "file", "checksum", "executed", "ready") VALUES (' .
$this->dbal->escapeString($migration->group) . "," .
Expand All @@ -122,9 +118,10 @@ public function insertMigration(Migration $migration)
$this->dbal->escapeDateTime($migration->executedAt) . "," .
$this->dbal->escapeBool(FALSE) .
")
RETURNING id
");

$migration->id = (int) $this->dbal->query('SELECT CURRVAL('. $this->primarySequence . ') AS id')[0]['id'];
$migration->id = (int) $rows[0]['id'];
}


Expand Down

0 comments on commit fb80c71

Please sign in to comment.