Skip to content

Commit

Permalink
Recover assigned sequence after changing type
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jul 14, 2017
1 parent 8cdf03c commit c0dd51a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/private/DB/PostgreSqlMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@ protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $
$this->emit($sql, $step++, count($sqls));
// BIGSERIAL could not be used in statements altering column type
// see https://github.com/owncloud/core/pull/28364#issuecomment-315006853
$sql = preg_replace('|(ALTER [^s]+ TYPE )(BIGSERIAL)|i', '\1BIGINT', $sql);
if (preg_match('|(ALTER TABLE\s+)(\S+)(\s+ALTER\s+)(\S+)(\s+TYPE\s+)(BIGSERIAL)|i', $sql, $matches)){
$alterTable = $matches[1];
$tableName = $matches[2];
$alterColumn = $matches[3];
$columnName = $matches[4];
$typeKeyword = $matches[5];

$alterSql = $alterTable . $tableName . $alterColumn . $columnName . $typeKeyword . 'BIGINT;';
$connection->query($alterSql);

$sequenceName = $tableName . '_' . $columnName . '_seq';
$sequenceSql = $alterTable . $tableName . $alterColumn . $columnName . ' SET DEFAULT nextval(\'' . $sequenceName . '\');';
$connection->query($sequenceSql);
continue;
}
$connection->query($sql);
}
$connection->commit();
Expand Down

0 comments on commit c0dd51a

Please sign in to comment.