Skip to content

Commit

Permalink
fix: error in migration CoreBundle, IF EXISTS is not a valid MySQL 8 …
Browse files Browse the repository at this point in the history
…statement

Error: An exception occurred while executing 'ALTER TABLE `object_metadata_14` DROP FOREIGN KEY IF EXISTS `fk_object_metadata_14__o_id`':
   SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
   check the manual that corresponds to your MySQL server version for the right syntax to use near '
   IF EXISTS `fk_object_metadata_14__o_id`' at line 1
Caused by: "IF EXISTS" is valid in MariaDB but not in MySQL 8
Resolved with: checking existence via metadata
  • Loading branch information
youwe-petervanderwal authored and dvesh3 committed Aug 21, 2023
1 parent c521787 commit de564ab
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions bundles/CoreBundle/Migrations/Version20230616085142.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function up(Schema $schema): void

if (!$metaDataTable->hasColumn(self::AUTO_ID)) {
if ($recreateForeignKey = $metaDataTable->hasForeignKey($foreignKeyName)) {
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY IF EXISTS `'.$foreignKeyName.'`');
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY `' . $foreignKeyName . '`');
}

if ($metaDataTable->hasPrimaryKey()) {
Expand Down Expand Up @@ -105,16 +105,19 @@ public function down(Schema $schema): void

if ($metaDataTable->hasColumn(self::AUTO_ID)) {
if ($recreateForeignKey = $metaDataTable->hasForeignKey($foreignKeyName)) {
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY IF EXISTS `'.$foreignKeyName.'`');
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY `' . $foreignKeyName . '`');
}

$this->addSql('ALTER TABLE `' . $tableName . '` DROP COLUMN `' . self::AUTO_ID . '`');
$this->addSql(
'ALTER TABLE `' . $tableName . '` ADD PRIMARY KEY (' . self::PK_COLUMNS . ')'
);
$this->addSql(
'ALTER TABLE `' . $tableName . '` DROP INDEX IF EXISTS `' . self::UNIQUE_KEY_NAME . '`'
);

if ($metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) {
$this->addSql(
'ALTER TABLE `' . $tableName . '` DROP INDEX `' . self::UNIQUE_KEY_NAME . '`'
);
}

if ($recreateForeignKey) {
$this->addSql(
Expand Down

0 comments on commit de564ab

Please sign in to comment.