Skip to content

Commit

Permalink
New method Column::isDifferent()
Browse files Browse the repository at this point in the history
  • Loading branch information
hschletz committed Sep 23, 2018
1 parent 933f6fa commit 59bd08a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,35 @@ public function setComment($comment)
* DBMS-specific implementation for setting a column comment
**/
abstract protected function _setComment();

/**
* Compare with given specification
*
* @param mixed[] $newSpec Specification to compare with. Same keys as toArray() output.
* @param string[] $keys Compare only given attributes (default: compare all)
* @return bool
*/
public function isDifferent(array $newSpec, array $keys = null)
{
$oldSpec = $this->toArray();
if ($keys) {
$keys = array_flip($keys);
$oldSpec = array_intersect_key($oldSpec, $keys);
$newSpec = array_intersect_key($newSpec, $keys);
}
return $this->_isDifferent($oldSpec, $newSpec);
}

/**
* Compare specifications
*
* DBMS may require extending this method with tweaks for emulated types.
*
* @param mixed[] $oldSpec
* @param mixed[] $newSpec
**/
protected function _isDifferent($oldSpec, $newSpec)
{
return $newSpec != $oldSpec;
}
}
17 changes: 17 additions & 0 deletions src/Column/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,21 @@ protected function _setComment()
{
$this->_modify();
}

/** {@inheritdoc} */
protected function _isDifferent($oldSpec, $newSpec)
{
if (
array_key_exists('type', $newSpec) and
$newSpec['type'] == self::TYPE_BOOL and
$oldSpec['type'] == self::TYPE_INTEGER
) {
// Booleans are detected as 8 bit INTEGER.
$newSpec['type'] = self::TYPE_INTEGER;
if (array_key_exists('length', $newSpec)) {
$newSpec['length'] = 8;
}
}
return parent::_isDifferent($oldSpec, $newSpec);
}
}
39 changes: 39 additions & 0 deletions src/Column/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,43 @@ protected function _setComment()
{
// Columnn comments are not supported by SQLite.
}

/** {@inheritdoc} */
protected function _isDifferent($oldSpec, $newSpec)
{
if (array_key_exists('type', $newSpec)) {
$oldType = $oldSpec['type'];
$newType = $newSpec['type'];
if (
$oldType == self::TYPE_INTEGER and
$newType == self::TYPE_INTEGER and
array_key_exists('length', $newSpec)
) {
// Integers always have length set to NULL.
$newSpec['length'] = null;
} elseif (
$oldType == self::TYPE_INTEGER and
$newType == self::TYPE_BOOL
) {
// Booleans are detected as INTEGER.
$newSpec['type'] = self::TYPE_INTEGER;
} elseif (
$oldType == self::TYPE_CLOB and
($newType == self::TYPE_TIMESTAMP or $newType == self::TYPE_DATE)
) {
// Timestamps and dates are detected as CLOB.
$newSpec['type'] = self::TYPE_CLOB;
} elseif (
$oldType == self::TYPE_FLOAT and
$newType == self::TYPE_DECIMAL
) {
// Decimals are detected as FLOAT.
$newSpec['type'] = self::TYPE_FLOAT;
if (array_key_exists('length', $newSpec)) {
$newSpec['length'] = null;
}
}
}
return parent::_isDifferent($oldSpec, $newSpec);
}
}

0 comments on commit 59bd08a

Please sign in to comment.