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.
This commit is contained in:
Jan Böhmer 2019-09-29 19:29:15 +02:00
parent da46257757
commit 4675793c24
3 changed files with 12 additions and 7 deletions

View file

@ -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 = <<<EOD
INSERT INTO `users`
INSERT IGNORE INTO `users`
(`id`,`name`,`password`,`first_name`,`last_name`,`department`,
`email`,
`need_pw_change`,`group_id`,`perms_system`,`perms_groups`,

View file

@ -46,14 +46,14 @@ final class Version20190902140506 extends AbstractMigration
/** Migrate the part locations for parts with known 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, 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'
);

View file

@ -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 <> ''"
);