Skip to content

Commit fb80c71

Browse files
committed
PgSqlDrive: fetch id of inserted migration with RETURNING clause
1 parent f27759e commit fb80c71

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/Drivers/PgSqlDriver.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class PgSqlDriver extends BaseDriver implements IDriver
2929
/** @var string */
3030
protected $schemaStr;
3131

32-
/** @var string */
33-
protected $primarySequence;
34-
3532

3633
/**
3734
* @param IDbal $dbal
@@ -43,7 +40,6 @@ public function __construct(IDbal $dbal, $tableName = 'migrations', $schema = 'p
4340
parent::__construct($dbal, $tableName);
4441
$this->schema = $dbal->escapeIdentifier($schema);
4542
$this->schemaStr = $dbal->escapeString($schema);
46-
$this->primarySequence = $this->dbal->escapeString($tableName . '_id_seq');
4743
}
4844

4945

@@ -113,7 +109,7 @@ public function dropTable()
113109

114110
public function insertMigration(Migration $migration)
115111
{
116-
$this->dbal->exec("
112+
$rows = $this->dbal->query("
117113
INSERT INTO {$this->schema}.{$this->tableName}" . '
118114
("group", "file", "checksum", "executed", "ready") VALUES (' .
119115
$this->dbal->escapeString($migration->group) . "," .
@@ -122,9 +118,10 @@ public function insertMigration(Migration $migration)
122118
$this->dbal->escapeDateTime($migration->executedAt) . "," .
123119
$this->dbal->escapeBool(FALSE) .
124120
")
121+
RETURNING id
125122
");
126123

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

130127

0 commit comments

Comments
 (0)