Skip to content

Commit

Permalink
Postreview changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Aug 2, 2017
1 parent 907bfa3 commit 20fa3f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
1 change: 0 additions & 1 deletion lib/private/DB/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function getConnection($type, $additionalConnectionParams) {
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
break;
case 'pgsql':
case 'postgresql':
$additionalConnectionParams['platform'] = new OCPostgreSqlPlatform();
break;
}
Expand Down
41 changes: 25 additions & 16 deletions lib/private/DB/OCPostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,53 @@

namespace OC\DB;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;

class OCPostgreSqlPlatform extends \Doctrine\DBAL\Platforms\PostgreSqlPlatform {
class OCPostgreSqlPlatform extends PostgreSqlPlatform {

/**
* {@inheritDoc}
*/
/**
* {@inheritDoc}
*/
public function getAlterTableSQL(TableDiff $diff){
$sqls = parent::getAlterTableSQL($diff);
foreach ($sqls as $index => $sql){
$queries = parent::getAlterTableSQL($diff);
foreach ($queries as $index => $sql){
// BIGSERIAL could not be used in statements altering column type
// That's why we replace it with BIGINT
// see https://github.com/owncloud/core/pull/28364#issuecomment-315006853
if (preg_match('|(ALTER TABLE\s+\S+\s+ALTER\s+\S+\s+TYPE\s+)(BIGSERIAL)|i', $sql, $matches)){
$alterTable = $matches[1];
$sqls[$index] = $alterTable . 'BIGINT';
$queries[$index] = $alterTable . 'BIGINT';
}
// Changing integer to bigint kills next autoincrement value
// see https://github.com/owncloud/core/pull/28364#issuecomment-315006853
if (preg_match('|ALTER TABLE\s+(\S+)\s+ALTER\s+(\S+)\s+DROP DEFAULT|i', $sql, $matches)){
$queryTableName = $matches[1];
$queryColumnName = $matches[2];
$columnDiff = $this->findColumnDiffByName($diff, $queryColumnName);
if ($columnDiff){
if ($this->shouldSkipDropDefault($columnDiff)){
unset($sqls[$index]);
unset($queries[$index]);
continue;
}
}
}
}

return $sqls;
return $queries;
}

/**
* We should NOT drop next seqence value if
* We should NOT drop next sequence value if
* - type was changed from INTEGER to BIGINT
* - column keeps an autoincrement
* - default value is kept NULL
*
* @param ColumnDiff $columnDiff
* @return bool
*/
private function shouldSkipDropDefault($columnDiff){
private function shouldSkipDropDefault(ColumnDiff $columnDiff){
$column = $columnDiff->column;
$fromColumn = $columnDiff->fromColumn;
return $fromColumn->getType()->getName() === Type::INTEGER
Expand All @@ -73,14 +77,19 @@ private function shouldSkipDropDefault($columnDiff){
&& $fromColumn->getAutoincrement()
&& $column->getAutoincrement();
}

private function findColumnDiffByName($diff, $name){

/**
* @param TableDiff $diff
* @param string $name
* @return ColumnDiff | false
*/
private function findColumnDiffByName(TableDiff $diff, $name){
foreach ($diff->changedColumns as $columnDiff){
$oldColumnName = $columnDiff->getOldColumnName()->getQuotedName($this);
if ($oldColumnName === $name){
return $columnDiff;
}
return false;
}
return false;
}
}

0 comments on commit 20fa3f0

Please sign in to comment.