mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 10:18:56 +02:00
Only drop the foreign keys during migration from legacy Part-DB DBs if they really exist
This should fix issue #260
This commit is contained in:
parent
577b841ee0
commit
2717d7d311
2 changed files with 54 additions and 11 deletions
|
@ -141,6 +141,27 @@ abstract class AbstractMultiPlatformMigration extends AbstractMigration
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a foreign key on a table exists in the database.
|
||||
* This method is only supported for MySQL/MariaDB databases yet!
|
||||
* @param string $table
|
||||
* @param string $fk_name
|
||||
* @return bool Returns true, if the foreign key exists
|
||||
* @throws Exception
|
||||
*/
|
||||
public function doesFKExists(string $table, string $fk_name): bool
|
||||
{
|
||||
$db_type = $this->getDatabaseType();
|
||||
if ($db_type !== 'mysql') {
|
||||
throw new \RuntimeException('This method is only supported for MySQL/MariaDB databases!');
|
||||
}
|
||||
|
||||
$sql = "SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = '$fk_name' AND TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'FOREIGN KEY'";
|
||||
$result = $this->connection->fetchOne($sql);
|
||||
|
||||
return $result > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the database type of the used database.
|
||||
* @return string|null Returns 'mysql' for MySQL/MariaDB and 'sqlite' for SQLite. Returns null if unknown type
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue