-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from BeMySlaveDarlin/renaming-column-on-morph…
…-table ISSUE-97: Renaming column does not drops column on morphTable
- Loading branch information
Showing
3 changed files
with
298 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Migrations. | ||
* | ||
* (c) Phalcon Team <team@phalcon.io> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Migrations\Db; | ||
|
||
use Phalcon\Db\ColumnInterface; | ||
|
||
class FieldDefinition | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var ColumnInterface | ||
*/ | ||
private $currentColumn; | ||
|
||
/** | ||
* @var FieldDefinition|null | ||
*/ | ||
private $previousField; | ||
|
||
/** | ||
* @var FieldDefinition|null | ||
*/ | ||
private $nextField; | ||
|
||
public function __construct( | ||
ColumnInterface $column, | ||
?FieldDefinition $previousField = null, | ||
?FieldDefinition $nextField = null | ||
) { | ||
$this->name = $column->getName(); | ||
$this->currentColumn = $column; | ||
$this->previousField = $previousField; | ||
$this->nextField = $nextField; | ||
} | ||
|
||
public function setPrevious(?FieldDefinition $field = null): void | ||
{ | ||
$this->previousField = $field; | ||
} | ||
|
||
public function setNext(?FieldDefinition $field = null): void | ||
{ | ||
$this->nextField = $field; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getColumn(): ColumnInterface | ||
{ | ||
return $this->currentColumn; | ||
} | ||
|
||
public function getPrevious(): ?FieldDefinition | ||
{ | ||
return $this->previousField ?? null; | ||
} | ||
|
||
public function getNext(): ?FieldDefinition | ||
{ | ||
return $this->nextField ?? null; | ||
} | ||
|
||
/** | ||
* @param FieldDefinition[] $externalFieldset Set of another field definitions to compare field between | ||
* | ||
* @return self|null | ||
*/ | ||
public function getPairedDefinition(array $externalFieldset): ?FieldDefinition | ||
{ | ||
if (isset($externalFieldset[$this->getName()])) { | ||
return $externalFieldset[$this->getName()]; | ||
} | ||
|
||
$possiblePairedField = null; | ||
if (null !== $this->previousField) { | ||
$prevField = $externalFieldset[$this->previousField->getName()] ?? null; | ||
if (null !== $prevField) { | ||
$possiblePairedField = $prevField->getNext(); | ||
} | ||
} | ||
if (null === $possiblePairedField && null !== $this->nextField) { | ||
$nextField = $externalFieldset[$this->nextField->getName()] ?? null; | ||
if (null !== $nextField) { | ||
$possiblePairedField = $nextField->getPrevious(); | ||
} | ||
} | ||
|
||
if (null === $possiblePairedField) { | ||
return null; | ||
} | ||
|
||
if ($this->isChangedData($possiblePairedField)) { | ||
return null; | ||
} | ||
|
||
return $possiblePairedField; | ||
} | ||
|
||
public function isChanged(FieldDefinition $comparingFieldDefinition): bool | ||
{ | ||
return $this->isChangedName($comparingFieldDefinition) || $this->isChangedData($comparingFieldDefinition); | ||
} | ||
|
||
public function isChangedName(FieldDefinition $comparingFieldDefinition): bool | ||
{ | ||
return $this->currentColumn->getName() !== $comparingFieldDefinition->getColumn()->getName(); | ||
} | ||
|
||
public function isChangedData(FieldDefinition $comparingFieldDefinition): bool | ||
{ | ||
return $this->currentColumn->getType() !== $comparingFieldDefinition->getColumn()->getType() || | ||
$this->currentColumn->getSize() !== $comparingFieldDefinition->getColumn()->getSize() || | ||
$this->currentColumn->isNotNull() !== $comparingFieldDefinition->getColumn()->isNotNull() || | ||
$this->currentColumn->getDefault() !== $comparingFieldDefinition->getColumn()->getDefault() || | ||
$this->currentColumn->isUnsigned() !== $comparingFieldDefinition->getColumn()->isUnsigned(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.