Check if foreign key references unknown database table
authorMatthias Schmidt <gravatronics@live.com>
Tue, 13 Apr 2021 14:01:45 +0000 (16:01 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 13 Apr 2021 14:01:45 +0000 (16:01 +0200)
wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 87fc5051e438ac2f29cbda716c656f3746517aad..202065ff00cc47b1ff78942656e249a877096d02 100644 (file)
@@ -1149,6 +1149,24 @@ class DatabaseTableChangeProcessor {
                                                }
                                        }
                                }
+
+                               foreach ($table->getForeignKeys() as $foreignKey) {
+                                       $referencedTableExists = in_array($foreignKey->getReferencedTable(), $this->existingTableNames);
+                                       foreach ($this->tables as $processedTable) {
+                                               if ($processedTable->getName() === $foreignKey->getReferencedTable()) {
+                                                       $referencedTableExists = !$processedTable->willBeDropped();
+                                               }
+                                       }
+                                       
+                                       if (!$referencedTableExists) {
+                                               $errors[] = [
+                                                       'columnNames' => implode(',', $foreignKey->getColumns()),
+                                                       'referencedTableName' => $foreignKey->getReferencedTable(),
+                                                       'tableName' => $table->getName(),
+                                                       'type' => 'unknownTableInForeignKey',
+                                               ];
+                                       }
+                               }
                        }
                }
                
index bf17f297e50439b5d26f6c84e9087cee6b3f1725..31f1a66b1a4fbd81368a612fca04039ec10c757a 100644 (file)
@@ -1984,6 +1984,8 @@ Die Datenbestände werden sorgfältig gepflegt, aber es ist nicht ausgeschlossen
                Der Primärindex {$error[tableName]} ({$error[columnNames]}) enthält die Spalte {$error[columnName]}, deren Werte NULL sein können.
        {else if $error[type] === 'foreignForeignKeyDrop'}
                Der Fremdschlüssel {$error[tableName]} ({$error[columnNames]}) gehört zu einem anderen Paket und kann deshalb nicht gelöscht werden.
+       {else if $error[type] === 'unknownTableInForeignKey'}
+               Der Fremdschlüssel {$error[tableName]} ({$error[columnNames]}) referenziert eine unbekannte Tabelle {$error[referencedTableName]}.
        {else}
                Unbekannter Fehler.
        {/if}
index d3a1c9d5062b29ea97a2a3df2b66da3f95d6255c..c30f3cfb9c65e60c479762881f4f5e8e48b8571c 100644 (file)
@@ -1968,6 +1968,8 @@ If you have <strong>already bought the licenses for the listed apps</strong>, th
                The primary index {$error[tableName]} ({$error[columnNames]}) contains the column {$error[columnName]} whose values can be NULL.
        {else if $error[type] === 'foreignForeignKeyDrop'}
                The foreign key {$error[tableName]} ({$error[columnNames]}) belongs to a different package and thus cannot be dropped.
+       {else if $error[type] === 'unknownTableInForeignKey'}
+               The foreign key {$error[tableName]} ({$error[columnNames]}) references an unknown table {$error[referencedTableName]}.
        {else}
                Unknown error.
        {/if}