fix: add postgres backend

changes:
- Add postgres backend
- Modified or added table attributes.
  UpdateDate, ForeignKeys, Booleans
- Fix test for sqlite and postgres. Compare json instead the struct.
This commit is contained in:
2020-06-01 22:36:57 +02:00
parent a1c28a0a2e
commit 4931c63c10
46 changed files with 822 additions and 245 deletions

View File

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS devices (
device_id CHAR(36) CONSTRAINT pk_devices PRIMARY KEY,
device_name VARCHAR(64) NOT NULL,
device_location VARCHAR(64),
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_date TIMESTAMP
);

View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS humidities (
id CHAR(36) CONSTRAINT pk_humidities PRIMARY KEY,
value NUMERIC(10,3) NOT NULL,
date TIMESTAMP NOT NULL,
sensor_id CHAR(36) NOT NULL,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_date TIMESTAMP
);
ALTER TABLE humidities
ADD FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS pressures (
id CHAR(36) CONSTRAINT pk_pressures PRIMARY KEY,
value NUMERIC(10,3) NOT NULL,
date TIMESTAMP NOT NULL,
sensor_id CHAR(36) NOT NULL,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_date TIMESTAMP
);
ALTER TABLE pressures
ADD FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

View File

@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS sensors (
sensor_id CHAR(36) CONSTRAINT pk_sensors PRIMARY KEY,
sensor_name VARCHAR(64) NOT NULL,
sensor_location VARCHAR(64),
wire_id VARCHAR(64),
i2c_bus VARCHAR(255),
i2c_address VARCHAR(12),
gpio_number VARCHAR(6),
sensor_model VARCHAR(16) NOT NULL,
sensor_enabled BOOLEAN DEFAULT TRUE NOT NULL,
tick_duration VARCHAR(6) NOT NULL,
device_id CHAR(36) NOT NULL,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_date TIMESTAMP
);
ALTER TABLE sensors
ADD FOREIGN KEY (device_id)
REFERENCES devices(device_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS temperatures (
id CHAR(36) CONSTRAINT pk_temperatures PRIMARY KEY,
value NUMERIC(10,3) NOT NULL,
date TIMESTAMP NOT NULL,
sensor_id CHAR(36) NOT NULL,
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_date TIMESTAMP
);
ALTER TABLE temperatures
ADD FOREIGN KEY (sensor_id)
REFERENCES sensors(sensor_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

View File

@ -1,2 +1,2 @@
DELETE FROM sensors
DELETE FROM devices
WHERE device_id = $1;

View File

@ -2,6 +2,7 @@ INSERT INTO devices (
device_id,
device_name,
device_location,
creation_date
creation_date,
update_date
)
VALUES ($1, $2, $3, $4);
VALUES ($1, $2, $3, $4, $5);

View File

@ -1,10 +1,9 @@
INSERT INTO humidities (
humidity_id,
humidity_value,
humidity_from_date,
humidity_till_date,
id,
value,
date,
sensor_id,
creation_date,
update_date
)
VALUES ($1, $2, $3, $4, $5, $6, $7);
VALUES ($1, $2, $3, $4, $5, $6);

View File

@ -1,10 +1,9 @@
INSERT INTO pressures (
pressure_id,
pressure_value,
pressure_from_date,
pressure_till_date,
id,
value,
date,
sensor_id,
creation_date,
update_date
)
VALUES ($1, $2, $3, $4, $5, $6, $7);
VALUES ($1, $2, $3, $4, $5, $6);

View File

@ -8,7 +8,9 @@ INSERT INTO sensors (
gpio_number,
sensor_model,
sensor_enabled,
tick_duration,
device_id,
creation_date
creation_date,
update_date
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);

View File

@ -1,10 +1,9 @@
INSERT INTO temperatures (
temperature_id,
temperature_value,
temperature_from_date,
temperature_till_date,
id,
value,
date,
sensor_id,
creation_date,
update_date
)
VALUES ($1, $2, $3, $4, $5, $6, $7);
VALUES ($1, $2, $3, $4, $5, $6);

View File

@ -2,7 +2,8 @@ SELECT
device_id,
device_name,
device_location,
creation_date
creation_date,
update_date
FROM
devices
WHERE device_id = $1;

View File

@ -2,6 +2,9 @@ SELECT
device_id,
device_name,
device_location,
creation_date
creation_date,
update_date
FROM
devices;
devices
ORDER BY
device_id ASC;

View File

@ -0,0 +1,9 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
humidities;

View File

@ -0,0 +1,11 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
humidities
WHERE
humidity_id = $1

View File

@ -0,0 +1,11 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
pressures
WHERE
pressure_id = $1

View File

@ -0,0 +1,9 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
pressures;

View File

@ -8,8 +8,10 @@ SELECT
gpio_number,
sensor_model,
sensor_enabled,
tick_duration,
device_id,
creation_date
creation_date,
update_date
FROM
sensors
WHERE

View File

@ -8,7 +8,11 @@ SELECT
gpio_number,
sensor_model,
sensor_enabled,
tick_duration,
device_id,
creation_date
creation_date,
update_date
FROM
sensors;
sensors
ORDER BY
sensor_id ASC;

View File

@ -0,0 +1,11 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
temperatures
WHERE
temperature_id = $1

View File

@ -0,0 +1,9 @@
SELECT
id,
value,
date,
sensor_id,
creation_date,
update_date
FROM
temperatures

View File

@ -1,8 +1,8 @@
UPDATE device
UPDATE devices
SET
device_id = $1,
device_name = $2,
device_location = $3,
creation_date = $4
device_name = $1,
device_location = $2,
creation_date = $3,
update_date = $4
WHERE
device_id = $5,
device_id = $5

View File

@ -1,15 +1,16 @@
UPDATE sensors
SET
sensor_id = $1,
sensor_name = $2,
sensor_location = $3,
wire_id = $4,
i2c_bus = $5,
i2c_address = $6,
gpio_number = $7,
sensor_model = $8,
sensor_enabled = $9,
sensor_name = $1,
sensor_location = $2,
wire_id = $3,
i2c_bus = $4,
i2c_address = $5,
gpio_number = $6,
sensor_model = $7,
sensor_enabled = $8,
tick_duration = $9,
device_id = $10,
creation_date = $11
creation_date = $11,
update_date = $12
WHERE
sensor_id = $12;
sensor_id = $13;