Last active
April 8, 2020 16:31
-
-
Save erikhansen/41bbc22d501fecb36892d49b41c204d3 to your computer and use it in GitHub Desktop.
magento/magento2 issue 19597 patch. See https://github.com/magento/magento2/issues/19597#issuecomment-573526850 for context
This file contains hidden or 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
diff --git a/Declaration/Schema/Comparator.php b/Declaration/Schema/Comparator.php | |
--- a/Setup/Declaration/Schema/Comparator.php | |
+++ b/Setup/Declaration/Schema/Comparator.php | |
@@ -24,7 +24,38 @@ | |
*/ | |
public function compare(ElementInterface $first, ElementInterface $second) | |
{ | |
+ // Code below pulled from https://github.com/magento/magento2/issues/19597#issuecomment-463611130 | |
+ //Ugly patch (MariaDB compatibility) | |
+ $firstParams = $first->getDiffSensitiveParams(); | |
+ $secondParams = $second->getDiffSensitiveParams(); | |
+ | |
+ $eqValues = [ | |
+ ['field' => 'default','values' => ['CURRENT_TIMESTAMP', 'current_timestamp()']], | |
+ ['field' => 'default','values' => ['NULL', '', NULL]], | |
+ ]; | |
+ | |
+ //standard values | |
+ foreach($eqValues as $eqValue){ | |
+ $field = $eqValue['field']; | |
+ $values = $eqValue['values']; | |
+ | |
+ if(array_key_exists($field,$firstParams) && in_array($secondParams[$field],$values) | |
+ && array_key_exists($field,$secondParams) && in_array($secondParams[$field],$values) | |
+ ){ | |
+ $firstParams['default'] = $values[0]; | |
+ $secondParams['default'] = $values[0]; | |
+ continue; | |
+ } | |
+ | |
+ } | |
+ | |
+ //enclosing quotes | |
+ if(array_key_exists('default',$secondParams) | |
+ && preg_match("/\\'.*\\'/", $secondParams['default'])){ | |
+ $firstParams['default'] = $secondParams['default']; | |
+ } | |
+ | |
return get_class($first) === get_class($second) && | |
- $first->getDiffSensitiveParams() === $second->getDiffSensitiveParams(); | |
+ $firstParams === $secondParams; | |
} | |
} | |
diff --git a/Setup/ExternalFKSetup.php b/Setup/ExternalFKSetup.php | |
index 4247b7b1a..42642f747 100644 | |
--- a/Setup/ExternalFKSetup.php | |
+++ b/Setup/ExternalFKSetup.php | |
@@ -82,8 +82,54 @@ class ExternalFKSetup | |
$this->setup->getTable($this->entityTable) | |
); | |
if (!$entityTableInfo[$this->entityColumn]['PRIMARY']) { | |
- $this->dropOldForeignKey(); | |
- $this->addForeignKeys(); | |
+ | |
+ // find the foreign keys on the entityTable | |
+ | |
+ $entityForeignKeys = $this->setup->getConnection()->getForeignKeys( | |
+ $this->setup->getTable($this->entityTable) | |
+ ); | |
+ $entityForeignKeys = array_filter( | |
+ $entityForeignKeys, | |
+ function ($key) { | |
+ return $key['COLUMN_NAME'] == $this->entityColumn; | |
+ } | |
+ ); | |
+ | |
+ $targetForeignKeys = $this->setup->getConnection()->getForeignKeys( | |
+ $this->setup->getTable($this->externalTable) | |
+ ); | |
+ | |
+ // for each found key, create one on the targetTable | |
+ | |
+ foreach ($entityForeignKeys as $entityForeignKey) { | |
+ | |
+ $targetForeignKeyName = $this->setup->getFkName( | |
+ $this->externalTable, | |
+ $this->externalColumn, | |
+ $this->setup->getTablePlaceholder($entityForeignKey['REF_TABLE_NAME']), | |
+ $entityForeignKey['REF_COLUMN_NAME'] | |
+ ); | |
+ | |
+ // check if the foreign key on the targetTable already exists | |
+ | |
+ foreach ($targetForeignKeys as $targetForeignKey) { | |
+ if ($targetForeignKey['FK_NAME'] === $targetForeignKeyName) { | |
+ // skip adding | |
+ continue 2; | |
+ } | |
+ } | |
+ | |
+ // add foreign key on targetTable based on the entityTable FK | |
+ | |
+ $this->setup->getConnection()->addForeignKey( | |
+ $targetForeignKeyName, | |
+ $this->setup->getTable($this->externalTable), | |
+ $this->externalColumn, | |
+ $entityForeignKey['REF_TABLE_NAME'], | |
+ $entityForeignKey['REF_COLUMN_NAME'], | |
+ $this->onDelete | |
+ ); | |
+ } | |
} else { | |
$this->addDefaultForeignKey(); | |
} | |
diff --git a/Setup/Declaration/Schema/Diff/TableDiff.php b/Setup/Declaration/Schema/Diff/TableDiff.php | |
index 87045df86..6814ce627 100644 | |
--- a/Setup/Declaration/Schema/Diff/TableDiff.php | |
+++ b/Setup/Declaration/Schema/Diff/TableDiff.php | |
@@ -203,6 +203,14 @@ class TableDiff | |
} | |
//Elements that should be removed | |
+ if ($elementType == self::CONSTRAINT_DIFF_TYPE) { | |
+ foreach (array_keys($generatedElements) as $elementName) { | |
+ if (preg_match('/_SEQUENCE_VAL$/', $elementName)) { | |
+ // remove auto-generated extra constraints | |
+ unset($generatedElements[$elementName]); | |
+ } | |
+ } | |
+ } | |
if ($this->diffManager->shouldBeRemoved($generatedElements)) { | |
$diff = $this->diffManager->registerRemoval($diff, $generatedElements); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment