PKGBUILD/pkg/repository/postgres/ddl/8_table_measured_values.down.sql

96 lines
2.0 KiB
MySQL
Raw Normal View History

-- Remove constraints to table measured_values
ALTER TABLE humidities
DROP CONSTRAINT fk_humidites_measured_value_id;
ALTER TABLE pressures
DROP CONSTRAINT fk_pressures_measured_value_id;
ALTER TABLE temperatures
DROP CONSTRAINT fk_temperatures_measured_value_id;
-- Rename measured_value_id to sensor_id
ALTER TABLE humidities
RENAME COLUMN measured_value_id TO sensor_id;
ALTER TABLE pressures
RENAME COLUMN measured_value_id TO sensor_id;
ALTER TABLE temperatures
RENAME COLUMN measured_value_id TO sensor_id;
-- Add new column measures_value_id with data from sensor_id
ALTER TABLE humidities
ADD COLUMN measured_value_id CHAR(36);
UPDATE humidities
SET measured_value_id=sensor_id;
ALTER TABLE pressures
ADD COLUMN measured_value_id CHAR(36);
UPDATE pressures
SET measured_value_id=sensor_id;
ALTER TABLE temperatures
ADD COLUMN measured_value_id CHAR(36);
UPDATE temperatures
SET measured_value_id=sensor_id;
-- Update sensor_id's
UPDATE humidities h
SET sensor_id = (
SELECT m.sensor_id
FROM measured_values m
WHERE m.id = h.measured_value_id
);
UPDATE pressures p
SET sensor_id = (
SELECT m.sensor_id
FROM measured_values m
WHERE m.id = p.measured_value_id
);
UPDATE temperatures t
SET sensor_id = (
SELECT m.sensor_id
FROM measured_values m
WHERE m.id = t.measured_value_id
);
-- Delete sensor_id columns
ALTER TABLE humidities
DROP COLUMN measured_value_id;
ALTER TABLE pressures
DROP COLUMN measured_value_id;
ALTER TABLE temperatures
DROP COLUMN measured_value_id;
-- Add foreign keys
ALTER TABLE humidities
ADD CONSTRAINT fk_humidites_sensor_id
FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE pressures
ADD CONSTRAINT fk_pressures_sensor_id
FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE temperatures
ADD CONSTRAINT fk_temperatures_sensor_id
FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
-- drop measured values table
DROP TABLE measured_values CASCADE;