From 0205551a5662c357867a04386277e8103fd28e23 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 20 Aug 2023 13:38:37 +0200 Subject: [PATCH] Fix migrations to work with latest MariaDB patch versions see PR #1213 --- .../resources/db/migration/V0_7_7__update.sql | 38 +++---- .../resources/db/migration/V0_8_6__update.sql | 101 +++++++++++++++++- 2 files changed, 119 insertions(+), 20 deletions(-) diff --git a/src/main/resources/db/migration/V0_7_7__update.sql b/src/main/resources/db/migration/V0_7_7__update.sql index 164eaa0f9..eb0702060 100644 --- a/src/main/resources/db/migration/V0_7_7__update.sql +++ b/src/main/resources/db/migration/V0_7_7__update.sql @@ -1,3 +1,9 @@ +ALTER TABLE `connector` + DROP FOREIGN KEY `FK_chargeBoxId_c`; +ALTER TABLE `reservation` + DROP FOREIGN KEY `FK_chargeBoxId_r`, + DROP FOREIGN KEY `FK_idTag_r`; + ALTER TABLE `chargebox` CHANGE COLUMN `chargeBoxId` `chargeBoxId` VARCHAR(255) NOT NULL COMMENT '' , CHANGE COLUMN `endpoint_address` `endpoint_address` VARCHAR(255) NULL DEFAULT NULL COMMENT '' , @@ -14,9 +20,6 @@ CHANGE COLUMN `meterType` `meterType` VARCHAR(255) NULL DEFAULT NULL COMMENT '' CHANGE COLUMN `meterSerialNumber` `meterSerialNumber` VARCHAR(255) NULL DEFAULT NULL COMMENT '' , CHANGE COLUMN `diagnosticsStatus` `diagnosticsStatus` VARCHAR(255) NULL DEFAULT NULL COMMENT '' ; - -ALTER TABLE `connector` -DROP FOREIGN KEY `FK_chargeBoxId_c`; ALTER TABLE `connector` CHANGE COLUMN `chargeBoxId` `chargeBoxId` VARCHAR(255) NOT NULL COMMENT '' ; ALTER TABLE `connector` @@ -42,10 +45,6 @@ CHANGE COLUMN `errorCode` `errorCode` VARCHAR(255) NULL DEFAULT NULL COMMENT '' CHANGE COLUMN `errorInfo` `errorInfo` VARCHAR(255) NULL DEFAULT NULL COMMENT '' , CHANGE COLUMN `vendorErrorCode` `vendorErrorCode` VARCHAR(255) NULL DEFAULT NULL COMMENT '' ; - -ALTER TABLE `reservation` -DROP FOREIGN KEY `FK_chargeBoxId_r`, -DROP FOREIGN KEY `FK_idTag_r`; ALTER TABLE `reservation` CHANGE COLUMN `idTag` `idTag` VARCHAR(255) NOT NULL COMMENT '' , CHANGE COLUMN `chargeBoxId` `chargeBoxId` VARCHAR(255) NOT NULL COMMENT '' , @@ -54,11 +53,6 @@ ALTER TABLE `reservation` ADD CONSTRAINT `FK_chargeBoxId_r` FOREIGN KEY (`chargeBoxId`) REFERENCES `chargebox` (`chargeBoxId`) - ON DELETE CASCADE - ON UPDATE NO ACTION, -ADD CONSTRAINT `FK_idTag_r` -FOREIGN KEY (`idTag`) -REFERENCES `user` (`idTag`) ON DELETE CASCADE ON UPDATE NO ACTION; @@ -68,12 +62,6 @@ ALTER TABLE `transaction` CHANGE COLUMN `idTag` `idTag` VARCHAR(255) NOT NULL COMMENT '' , CHANGE COLUMN `startValue` `startValue` VARCHAR(255) NULL DEFAULT NULL COMMENT '' , CHANGE COLUMN `stopValue` `stopValue` VARCHAR(255) NULL DEFAULT NULL COMMENT '' ; -ALTER TABLE `transaction` -ADD CONSTRAINT `FK_idTag_t` -FOREIGN KEY (`idTag`) -REFERENCES `user` (`idTag`) - ON DELETE CASCADE - ON UPDATE NO ACTION; ALTER TABLE `user` DROP FOREIGN KEY `FK_user_parentIdTag`; @@ -86,3 +74,17 @@ FOREIGN KEY (`parentIdTag`) REFERENCES `user` (`idTag`) ON DELETE NO ACTION ON UPDATE NO ACTION; + +ALTER TABLE `transaction` + ADD CONSTRAINT `FK_idTag_t` + FOREIGN KEY (`idTag`) + REFERENCES `user` (`idTag`) + ON DELETE CASCADE + ON UPDATE NO ACTION; + +ALTER TABLE `reservation` + ADD CONSTRAINT `FK_idTag_r` + FOREIGN KEY (`idTag`) + REFERENCES `user` (`idTag`) + ON DELETE CASCADE + ON UPDATE NO ACTION; diff --git a/src/main/resources/db/migration/V0_8_6__update.sql b/src/main/resources/db/migration/V0_8_6__update.sql index 2dfcf0f3f..1153f4407 100644 --- a/src/main/resources/db/migration/V0_8_6__update.sql +++ b/src/main/resources/db/migration/V0_8_6__update.sql @@ -18,10 +18,49 @@ LOCK TABLES ocpp_tag WRITE, reservation WRITE, settings WRITE, + schema_version WRITE, `transaction` WRITE, `user` WRITE; -SET FOREIGN_KEY_CHECKS = 0; +# alter table charge_box +# drop foreign key FK_charge_box_address_apk; + +# alter table connector +# drop foreign key FK_connector_charge_box_cbid; + +alter table connector_meter_value + drop foreign key FK_pk_cm; + +alter table connector_meter_value + drop foreign key FK_tid_cm; + +alter table connector_status + drop foreign key FK_cs_pk; + +alter table ocpp_tag + drop foreign key FK_ocpp_tag_parent_id_tag; + +alter table reservation + drop foreign key FK_reservation_charge_box_cbid; + +alter table reservation + drop foreign key FK_reservation_ocpp_tag_id_tag; + +alter table reservation + drop foreign key FK_transaction_pk_r; + +alter table transaction + drop foreign key FK_connector_pk_t; + +alter table transaction + drop foreign key FK_transaction_ocpp_tag_id_tag; + +alter table user + drop foreign key FK_user_address_apk; + +alter table user + drop foreign key FK_user_ocpp_tag_otpk; + ALTER TABLE charge_box CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE address CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; @@ -34,7 +73,65 @@ ALTER TABLE settings CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE `transaction` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE `user` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; -SET FOREIGN_KEY_CHECKS = 1; +alter table charge_box + add constraint FK_charge_box_address_apk + foreign key (address_pk) references stevedb.address (address_pk) + on delete set null; + +alter table connector + add constraint FK_connector_charge_box_cbid + foreign key (charge_box_id) references stevedb.charge_box (charge_box_id) + on delete cascade; + +alter table stevedb.connector_meter_value + add constraint FK_pk_cm + foreign key (connector_pk) references stevedb.connector (connector_pk) + on delete cascade; + +alter table stevedb.connector_meter_value + add constraint FK_tid_cm + foreign key (transaction_pk) references stevedb.transaction (transaction_pk) + on delete set null; + +alter table stevedb.connector_status + add constraint FK_cs_pk + foreign key (connector_pk) references stevedb.connector (connector_pk) + on delete cascade; +alter table stevedb.ocpp_tag + add constraint FK_ocpp_tag_parent_id_tag + foreign key (parent_id_tag) references stevedb.ocpp_tag (id_tag); +alter table stevedb.reservation + add constraint FK_reservation_charge_box_cbid + foreign key (charge_box_id) references stevedb.charge_box (charge_box_id) + on delete cascade; + +alter table stevedb.reservation + add constraint FK_reservation_ocpp_tag_id_tag + foreign key (id_tag) references stevedb.ocpp_tag (id_tag) + on delete cascade; + +alter table stevedb.reservation + add constraint FK_transaction_pk_r + foreign key (transaction_pk) references stevedb.transaction (transaction_pk); + +alter table stevedb.transaction + add constraint FK_connector_pk_t + foreign key (connector_pk) references stevedb.connector (connector_pk) + on delete cascade; + +alter table stevedb.transaction + add constraint FK_transaction_ocpp_tag_id_tag + foreign key (id_tag) references stevedb.ocpp_tag (id_tag) + on delete cascade; +alter table stevedb.user + add constraint FK_user_address_apk + foreign key (address_pk) references stevedb.address (address_pk) + on delete set null; + +alter table stevedb.user + add constraint FK_user_ocpp_tag_otpk + foreign key (ocpp_tag_pk) references stevedb.ocpp_tag (ocpp_tag_pk) + on delete set null; UNLOCK TABLES;