Clean up sensors and related tables and add constraints #3745

Merged
merged 2 commits into from Jun 30, 2016

Projects

None yet

3 participants

@murrant
Contributor
murrant commented Jun 29, 2016

Remove sensors for non existent devices and delete unused sensors_to_state_indexes, state_indexes, and state_translations
Change sensors.device_id to unsigned to match devices.device_id
Add foreign keys to sensors and sensors_to_state_indexes for cascading deletes

229 rows affected. (Query took 0.2732 seconds.)

DELETE FROM `sensors_to_state_indexes` WHERE `sensors_to_state_indexes`.`sensor_id` NOT IN (SELECT DISTINCT `sensors`.`sensor_id` FROM `sensors` INNER JOIN `devices` ON `sensors`.`device_id` = `devices`.`device_id`)

384 rows affected. (Query took 0.0410 seconds.)

DELETE FROM `sensors` WHERE `sensors`.`device_id` NOT IN (SELECT `device_id` FROM `devices`)

4 rows affected. (Query took 0.1045 seconds.)

DELETE FROM `state_indexes` WHERE `state_indexes`.`state_index_id` NOT IN (SELECT `sensors_to_state_indexes`.`state_index_id` FROM `sensors_to_state_indexes`)

26 rows affected. (Query took 0.0275 seconds.)

DELETE FROM `state_translations` WHERE `state_translations`.`state_index_id` NOT IN (SELECT `state_indexes`.`state_index_id` FROM `state_indexes`)

MySQL returned an empty result set (i.e. zero rows). (Query took 1.1063 seconds.)

ALTER TABLE `sensors` CHANGE `device_id` `device_id` INT(11) UNSIGNED NOT NULL DEFAULT '0'

MySQL returned an empty result set (i.e. zero rows). (Query took 1.2088 seconds.)

ALTER TABLE `sensors` ADD CONSTRAINT `sensors_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`device_id`) ON DELETE CASCADE

MySQL returned an empty result set (i.e. zero rows). (Query took 1.1473 seconds.)

ALTER TABLE `sensors_to_state_indexes` ADD CONSTRAINT `sensors_to_state_indexes_sensor_id_foreign` FOREIGN KEY (`sensor_id`) REFERENCES `sensors` (`sensor_id`) ON DELETE CASCADE
@murrant murrant Clean up sensors and related tables and add constraints
Remove sensors for non existent devices and delete unused sensors_to_state_indexes, state_indexes, and state_translations
Change sensors.device_id to unsigned to match devices.device_id
Add foreign keys to sensors and sensors_to_state_indexes for cascading deletes
efadac6
@murrant murrant added the Schema label Jun 29, 2016
@laf
Member
laf commented Jun 30, 2016

+1 from me:

mysql> DELETE FROM `sensors_to_state_indexes` WHERE `sensors_to_state_indexes`.`sensor_id` NOT IN (SELECT DISTINCT `sensors`.`sensor_id` FROM `sensors` INNER JOIN `devices` ON `sensors`.`device_id` = `devices`.`device_id`);
Query OK, 16 rows affected (0.02 sec)

mysql> DELETE FROM `sensors` WHERE `sensors`.`device_id` NOT IN (SELECT `device_id` FROM `devices`);
Query OK, 21 rows affected (0.01 sec)

mysql> DELETE FROM `state_indexes` WHERE `state_indexes`.`state_index_id` NOT IN (SELECT `sensors_to_state_indexes`.`state_index_id` FROM `sensors_to_state_indexes`);
Query OK, 0 rows affected (0.00 sec)

mysql> DELETE FROM `state_translations` WHERE `state_translations`.`state_index_id` NOT IN (SELECT `state_indexes`.`state_index_id` FROM `state_indexes`);
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE `sensors` CHANGE `device_id` `device_id` INT(11) UNSIGNED NOT NULL DEFAULT '0';
Query OK, 6311 rows affected (1.63 sec)
Records: 6311  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `sensors` ADD CONSTRAINT `sensors_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`device_id`) ON DELETE CASCADE;
Query OK, 6311 rows affected (0.95 sec)
Records: 6311  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `sensors_to_state_indexes` ADD CONSTRAINT `sensors_to_state_indexes_sensor_id_foreign` FOREIGN KEY (`sensor_id`) REFERENCES `sensors` (`sensor_id`) ON DELETE CASCADE;
Query OK, 1750 rows affected (0.14 sec)
Records: 1750  Duplicates: 0  Warnings: 0
@murrant murrant Rename sql to 121
f07c286
@laf laf merged commit c684228 into librenms:master Jun 30, 2016

2 checks passed

Auto-Deploy Build finished.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@murrant murrant deleted the murrant:sensor_cleanup branch Jul 1, 2016
@murrant murrant restored the murrant:sensor_cleanup branch Jul 1, 2016
@murrant murrant deleted the murrant:sensor_cleanup branch Jul 1, 2016
@murrant murrant referenced this pull request Jul 6, 2016
Closed

Can't delete device #3783

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment