Skip to content

Commit de564ab

Browse files
youwe-petervanderwaldvesh3
authored andcommitted
fix: error in migration CoreBundle, IF EXISTS is not a valid MySQL 8 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
1 parent c521787 commit de564ab

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

bundles/CoreBundle/Migrations/Version20230616085142.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function up(Schema $schema): void
5454

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

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

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

111111
$this->addSql('ALTER TABLE `' . $tableName . '` DROP COLUMN `' . self::AUTO_ID . '`');
112112
$this->addSql(
113113
'ALTER TABLE `' . $tableName . '` ADD PRIMARY KEY (' . self::PK_COLUMNS . ')'
114114
);
115-
$this->addSql(
116-
'ALTER TABLE `' . $tableName . '` DROP INDEX IF EXISTS `' . self::UNIQUE_KEY_NAME . '`'
117-
);
115+
116+
if ($metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) {
117+
$this->addSql(
118+
'ALTER TABLE `' . $tableName . '` DROP INDEX `' . self::UNIQUE_KEY_NAME . '`'
119+
);
120+
}
118121

119122
if ($recreateForeignKey) {
120123
$this->addSql(

0 commit comments

Comments
 (0)