Skip to content
Snippets Groups Projects
Commit 388c6e90 authored by Demian Katz's avatar Demian Katz
Browse files

Added support for changed defaults in database upgrade tool.

parent edfd1844
Branches
Tags
No related merge requests found
...@@ -386,6 +386,28 @@ class DbUpgrade extends AbstractPlugin ...@@ -386,6 +386,28 @@ class DbUpgrade extends AbstractPlugin
return $missing; return $missing;
} }
/**
* Given a current row default, return true if the current default matches the
* one found in the SQL provided as the $sql parameter. Return false if there
* is a mismatch that will require table structure updates.
*
* @param string $currentDefault Object to check
* @param string $sql SQL to compare against
*
* @return bool
*/
protected function defaultMatches($currentDefault, $sql)
{
preg_match("/.* DEFAULT (.*)$/", $sql, $matches);
$expectedDefault = isset($matches[1]) ? $matches[1] : null;
if (null !== $expectedDefault) {
$expectedDefault = trim(rtrim($expectedDefault, ','), "'");
$expectedDefault = (strtoupper($expectedDefault) == 'NULL')
? null : $expectedDefault;
}
return ($expectedDefault === $currentDefault);
}
/** /**
* Given a table column object, return true if the object's type matches the * Given a table column object, return true if the object's type matches the
* specified $type parameter. Return false if there is a mismatch that will * specified $type parameter. Return false if there is a mismatch that will
...@@ -457,7 +479,7 @@ class DbUpgrade extends AbstractPlugin ...@@ -457,7 +479,7 @@ class DbUpgrade extends AbstractPlugin
public function getModifiedColumns($missingTables = [], public function getModifiedColumns($missingTables = [],
$missingColumns = [] $missingColumns = []
) { ) {
$missing = []; $modified = [];
foreach ($this->dbCommands as $table => $sql) { foreach ($this->dbCommands as $table => $sql) {
// Skip missing tables if we're logging // Skip missing tables if we're logging
if (in_array($table, $missingTables)) { if (in_array($table, $missingTables)) {
...@@ -494,15 +516,20 @@ class DbUpgrade extends AbstractPlugin ...@@ -494,15 +516,20 @@ class DbUpgrade extends AbstractPlugin
continue; continue;
} }
$currentColumn = $actualColumns[$column]; $currentColumn = $actualColumns[$column];
if (!$this->typeMatches($currentColumn, $expectedTypes[$i])) { if (!$this->typeMatches($currentColumn, $expectedTypes[$i])
if (!isset($missing[$table])) { || !$this->defaultMatches(
$missing[$table] = []; $currentColumn->getColumnDefault(),
$columnDefinitions[$column]
)
) {
if (!isset($modified[$table])) {
$modified[$table] = [];
} }
$missing[$table][] = $columnDefinitions[$column]; $modified[$table][] = $columnDefinitions[$column];
} }
} }
} }
return $missing; return $modified;
} }
/** /**
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment