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:
7
pkg/repository/db/postgres/createTableDevices.sql
Normal file
7
pkg/repository/db/postgres/createTableDevices.sql
Normal 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
|
||||
);
|
14
pkg/repository/db/postgres/createTableHumidities.sql
Normal file
14
pkg/repository/db/postgres/createTableHumidities.sql
Normal 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;
|
14
pkg/repository/db/postgres/createTablePressures.sql
Normal file
14
pkg/repository/db/postgres/createTablePressures.sql
Normal 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;
|
21
pkg/repository/db/postgres/createTableSensors.sql
Normal file
21
pkg/repository/db/postgres/createTableSensors.sql
Normal 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;
|
14
pkg/repository/db/postgres/createTableTemperatures.sql
Normal file
14
pkg/repository/db/postgres/createTableTemperatures.sql
Normal 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;
|
@ -1,2 +1,2 @@
|
||||
DELETE FROM sensors
|
||||
DELETE FROM devices
|
||||
WHERE device_id = $1;
|
@ -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);
|
@ -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);
|
@ -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);
|
@ -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);
|
@ -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);
|
@ -2,7 +2,8 @@ SELECT
|
||||
device_id,
|
||||
device_name,
|
||||
device_location,
|
||||
creation_date
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
devices
|
||||
WHERE device_id = $1;
|
@ -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;
|
9
pkg/repository/db/postgres/selectHumidities.sql
Normal file
9
pkg/repository/db/postgres/selectHumidities.sql
Normal file
@ -0,0 +1,9 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
humidities;
|
11
pkg/repository/db/postgres/selectHumidity.sql
Normal file
11
pkg/repository/db/postgres/selectHumidity.sql
Normal file
@ -0,0 +1,11 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
humidities
|
||||
WHERE
|
||||
humidity_id = $1
|
11
pkg/repository/db/postgres/selectPressure.sql
Normal file
11
pkg/repository/db/postgres/selectPressure.sql
Normal file
@ -0,0 +1,11 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
pressures
|
||||
WHERE
|
||||
pressure_id = $1
|
9
pkg/repository/db/postgres/selectPressures.sql
Normal file
9
pkg/repository/db/postgres/selectPressures.sql
Normal file
@ -0,0 +1,9 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
pressures;
|
@ -8,8 +8,10 @@ SELECT
|
||||
gpio_number,
|
||||
sensor_model,
|
||||
sensor_enabled,
|
||||
tick_duration,
|
||||
device_id,
|
||||
creation_date
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
sensors
|
||||
WHERE
|
||||
|
@ -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;
|
11
pkg/repository/db/postgres/selectTemperature.sql
Normal file
11
pkg/repository/db/postgres/selectTemperature.sql
Normal file
@ -0,0 +1,11 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
temperatures
|
||||
WHERE
|
||||
temperature_id = $1
|
9
pkg/repository/db/postgres/selectTemperatures.sql
Normal file
9
pkg/repository/db/postgres/selectTemperatures.sql
Normal file
@ -0,0 +1,9 @@
|
||||
SELECT
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
temperatures
|
@ -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
|
@ -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;
|
Reference in New Issue
Block a user