From 4675793c24e993013ed6953332937a499aaf8d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 29 Sep 2019 19:29:15 +0200 Subject: [PATCH] Add IGNORE to INSERT statements in migrations to prevent exceptions. MySQL 5.7 or higher uses strict mode by default, and we would get an error during migration without that keyword. --- src/Migrations/Version1.php | 9 +++++++-- src/Migrations/Version20190902140506.php | 4 ++-- src/Migrations/Version20190924113252.php | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Migrations/Version1.php b/src/Migrations/Version1.php index 4074bd82..c1a15fab 100644 --- a/src/Migrations/Version1.php +++ b/src/Migrations/Version1.php @@ -79,6 +79,11 @@ final class Version1 extends AbstractMigration $this->addSql('ALTER TABLE `pricedetails` ADD CONSTRAINT FK_C68C4459398D64AA FOREIGN KEY (id_currency) REFERENCES currencies (id)'); $this->addSql('ALTER TABLE `groups` ADD CONSTRAINT FK_F06D3970727ACA70 FOREIGN KEY (parent_id) REFERENCES `groups` (id)'); + /** + * IGNORE is important here, or we get errors on MySQL 5.7 or higher. + * TODO: Maybe find a better way (like explicitly define default value), so we can use the strict mode. + */ + //Create table for user logs: $sql = $updateSteps[] = "CREATE TABLE `log` ". "( `id` INT NOT NULL AUTO_INCREMENT , `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ,". @@ -94,7 +99,7 @@ final class Version1 extends AbstractMigration //Create first groups and users: //Add needed groups. $sql = <<<'EOD' - INSERT INTO `groups` + INSERT IGNORE INTO `groups` (`id`,`name`,`parent_id`,`comment`,`perms_system`,`perms_groups`, `perms_users`, `perms_self`,`perms_system_config`,`perms_system_database`, @@ -122,7 +127,7 @@ EOD; $this->addSql($sql); $admin_pw = "$2y$10$36AnqCBS.YnHlVdM4UQ0oOCV7BjU7NmE0qnAVEex65AyZw1cbcEjq"; $sql = <<addSql( - 'INSERT INTO part_lots (id_part, id_store_location, amount, instock_unknown, last_modified, datetime_added) ' . + 'INSERT IGNORE INTO part_lots (id_part, id_store_location, amount, instock_unknown, last_modified, datetime_added) ' . 'SELECT parts.id, parts.id_storelocation, parts.instock, 0, NOW(), NOW() FROM parts ' . 'WHERE parts.instock >= 0' ); //Migrate part locations for parts with unknown instock $this->addSql( - 'INSERT INTO part_lots (id_part, id_store_location, amount, instock_unknown, last_modified, datetime_added) ' . + 'INSERT IGNORE INTO part_lots (id_part, id_store_location, amount, instock_unknown, last_modified, datetime_added) ' . 'SELECT parts.id, parts.id_storelocation, 0, 1, NOW(), NOW() FROM parts ' . 'WHERE parts.instock = -2' ); diff --git a/src/Migrations/Version20190924113252.php b/src/Migrations/Version20190924113252.php index 29a78149..4f24a927 100644 --- a/src/Migrations/Version20190924113252.php +++ b/src/Migrations/Version20190924113252.php @@ -42,7 +42,7 @@ final class Version20190924113252 extends AbstractMigration $this->addSql('ALTER TABLE attachments ADD original_filename VARCHAR(255) DEFAULT NULL, CHANGE filename path VARCHAR(255) NOT NULL'); //Before we drop the filename and filename_3d properties we have to migrate the data to attachments - $this->addSql("INSERT INTO `attachment_types` (`id`, `name`, `parent_id`, `comment`, `datetime_added`, `last_modified`, `filetype_filter`) + $this->addSql("INSERT IGNORE INTO `attachment_types` (`id`, `name`, `parent_id`, `comment`, `datetime_added`, `last_modified`, `filetype_filter`) VALUES (1000, 'Footprints', NULL, 'Created during automatic migration of the footprint files.', current_timestamp(), current_timestamp(), 'apng, bmp, gif, ico, cur, jpg, jpeg, jfif, pjpeg, pjp, png, svg, webp'), (1001, 'Footprints (3D)', NULL, 'Created during automatic migration of the footprint files.', current_timestamp(), current_timestamp(), 'x3d') @@ -50,14 +50,14 @@ final class Version20190924113252 extends AbstractMigration //Add a attachment for each footprint attachment $this->addSql( - 'INSERT INTO attachments (element_id, type_id, name, class_name, path, last_modified, datetime_added) ' . + 'INSERT IGNORE INTO attachments (element_id, type_id, name, class_name, path, last_modified, datetime_added) ' . "SELECT footprints.id, 1000, 'Footprint', 'Footprint', REPLACE(footprints.filename, '%BASE%/img/footprints', '%FOOTPRINTS%' ), NOW(), NOW() FROM footprints " . "WHERE footprints.filename <> ''" ); //Do the same for 3D Footprints $this->addSql( - 'INSERT INTO attachments (element_id, type_id, name, class_name, path, last_modified, datetime_added) ' . + 'INSERT IGNORE INTO attachments (element_id, type_id, name, class_name, path, last_modified, datetime_added) ' . "SELECT footprints.id, 1001, 'Footprint 3D', 'Footprint', REPLACE(footprints.filename_3d, '%BASE%/models', '%FOOTPRINTS3D%' ), NOW(), NOW() FROM footprints " . "WHERE footprints.filename_3d <> ''" );