Skip to content

Commit

Permalink
db: Allow DELETE FROM Devices, cascade Media and EventsCam
Browse files Browse the repository at this point in the history
This is to get out of the tricky situation when removing a camera may
fail if a new Media or EventsCam record appeared during the removal.
Removal entails deleting recordings from the filesystem, which can take
substantial time, and we have actually got report of this problem.
  • Loading branch information
andrey-utkin committed Sep 21, 2024
1 parent 6a84d1e commit 46c04e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion misc/sql/installed_db_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3000012
3010400
11 changes: 11 additions & 0 deletions misc/sql/mysql-upgrade/3010400/delete-device-cascade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

echo '
ALTER TABLE EventsCam DROP CONSTRAINT `EventsCam_ibfk_3`;
ALTER TABLE EventsCam ADD CONSTRAINT `EventsCam_ibfk_3` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE Media DROP CONSTRAINT `Media_ibfk_1`;
ALTER TABLE Media ADD CONSTRAINT `Media_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;
' | mysql -h"$host" -D"$dbname" -u"$user" -p"$password"

exit 0
4 changes: 2 additions & 2 deletions misc/sql/schema_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ CREATE TABLE `EventsCam` (
KEY `time_index` (`time`) USING BTREE,
CONSTRAINT `EventsCam_ibfk_1` FOREIGN KEY (`media_id`) REFERENCES `Media` (`id`) ON DELETE CASCADE,
CONSTRAINT `EventsCam_ibfk_2` FOREIGN KEY (`level_id`) REFERENCES `EventLevels` (`id`),
CONSTRAINT `EventsCam_ibfk_3` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE,
CONSTRAINT `EventsCam_ibfk_3` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `EventsCam_ibfk_4` FOREIGN KEY (`type_id`) REFERENCES `EventTypesCam` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down Expand Up @@ -227,7 +227,7 @@ CREATE TABLE `Media` (
`archive` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `device_id` (`device_id`),
CONSTRAINT `Media_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE
CONSTRAINT `Media_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `Devices` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `OnvifEvents`;
Expand Down

0 comments on commit 46c04e5

Please sign in to comment.