fix: postgres columns with timezone
Add timezone for the columns creation_date and update_date, otherwise it's difficult to compare the times correctly in the unit test. Furthermore the default value of the creation_date will now be defined by the postgres implementation of the database interface.
This commit is contained in:
@ -28,7 +28,7 @@ func (postgres *Postgres) DeleteDevices(ctx context.Context, deviceIDs ...string
|
||||
queryFile := "deleteDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
@ -58,7 +58,7 @@ func (postgres *Postgres) DeleteSensors(ctx context.Context, sensorIDs ...string
|
||||
queryFile := "deleteSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
@ -88,7 +88,7 @@ func (postgres *Postgres) InsertDevices(ctx context.Context, devices ...*types.D
|
||||
queryFile := "insertDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
@ -103,7 +103,18 @@ func (postgres *Postgres) InsertDevices(ctx context.Context, devices ...*types.D
|
||||
defer stmt.Close()
|
||||
|
||||
for _, device := range devices {
|
||||
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
|
||||
|
||||
if device.CreationDate.Equal(time.Time{}) {
|
||||
device.CreationDate = time.Now()
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(
|
||||
&device.ID,
|
||||
&device.Name,
|
||||
&device.Location,
|
||||
&device.CreationDate,
|
||||
&device.UpdateDate,
|
||||
)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("Failed to execute statement: %v", err)
|
||||
@ -133,7 +144,7 @@ func (postgres *Postgres) InsertMeasuredValues(ctx context.Context, measuredValu
|
||||
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error {
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
@ -143,6 +154,11 @@ func (postgres *Postgres) InsertMeasuredValues(ctx context.Context, measuredValu
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
|
||||
if measuredValue.CreationDate.Equal(time.Time{}) {
|
||||
measuredValue.CreationDate = time.Now()
|
||||
}
|
||||
|
||||
_, err := stmt.Exec(
|
||||
&measuredValue.ID,
|
||||
&measuredValue.Value,
|
||||
@ -190,7 +206,7 @@ func (postgres *Postgres) InsertSensors(ctx context.Context, sensors ...*types.S
|
||||
queryFile := "insertSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
@ -205,6 +221,11 @@ func (postgres *Postgres) InsertSensors(ctx context.Context, sensors ...*types.S
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensor := range sensors {
|
||||
|
||||
if sensor.CreationDate.Equal(time.Time{}) {
|
||||
sensor.CreationDate = time.Now()
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(
|
||||
&sensor.ID,
|
||||
&sensor.Name,
|
||||
@ -251,7 +272,7 @@ func (postgres *Postgres) SelectDevice(ctx context.Context, id string) (*types.D
|
||||
queryFile := "selectDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -281,7 +302,7 @@ func (postgres *Postgres) SelectDevices(ctx context.Context) ([]*types.Device, e
|
||||
queryFile := "selectDevices.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -338,7 +359,7 @@ func (postgres *Postgres) SelectHumidity(ctx context.Context, id string) (*types
|
||||
queryFile := "selectHumidity.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -372,7 +393,7 @@ func (postgres *Postgres) SelectHumidities(ctx context.Context) ([]*types.Measur
|
||||
queryFile := "selectHumidities.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -439,7 +460,7 @@ func (postgres *Postgres) SelectPressure(ctx context.Context, id string) (*types
|
||||
queryFile := "selectPressure.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -473,7 +494,7 @@ func (postgres *Postgres) SelectPressures(ctx context.Context) ([]*types.Measure
|
||||
queryFile := "selectPressures.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -503,7 +524,7 @@ func (postgres *Postgres) SelectSensor(ctx context.Context, id string) (*types.S
|
||||
queryFile := "selectSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -533,7 +554,7 @@ func (postgres *Postgres) SelectSensors(ctx context.Context) ([]*types.Sensor, e
|
||||
queryFile := "selectSensors.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -599,7 +620,7 @@ func (postgres *Postgres) SelectTemperature(ctx context.Context, id string) (*ty
|
||||
queryFile := "selectTemperature.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -633,7 +654,7 @@ func (postgres *Postgres) SelectTemperatures(ctx context.Context) ([]*types.Meas
|
||||
queryFile := "selectTemperatures.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return nil, fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
@ -663,7 +684,7 @@ func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.D
|
||||
queryFile := "updateDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
@ -703,7 +724,7 @@ func (postgres *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.S
|
||||
queryFile := "updateSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
return fmt.Errorf("Postgres-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
|
@ -2,6 +2,6 @@ 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
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
@ -1,10 +1,10 @@
|
||||
CREATE TABLE IF NOT EXISTS humidities (
|
||||
id CHAR(36) CONSTRAINT pk_humidities PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
ALTER TABLE humidities
|
||||
|
@ -1,10 +1,10 @@
|
||||
CREATE TABLE IF NOT EXISTS pressures (
|
||||
id CHAR(36) CONSTRAINT pk_pressures PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
ALTER TABLE pressures
|
||||
|
@ -10,8 +10,8 @@ CREATE TABLE IF NOT EXISTS sensors (
|
||||
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
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
ALTER TABLE sensors
|
||||
|
@ -1,10 +1,10 @@
|
||||
CREATE TABLE IF NOT EXISTS temperatures (
|
||||
id CHAR(36) CONSTRAINT pk_temperatures PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
ALTER TABLE temperatures
|
||||
|
@ -15,4 +15,6 @@ SELECT
|
||||
FROM
|
||||
sensors
|
||||
WHERE
|
||||
sensor_id = $1;
|
||||
sensor_id = $1
|
||||
ORDER BY
|
||||
sensor_name ASC;
|
@ -144,7 +144,7 @@ func testBackend(t *testing.T, repo *repository.Repository) {
|
||||
expectedSensors = []*types.Sensor{
|
||||
{
|
||||
ID: "0f8b88b0-c20d-42b2-ab51-b09ca99c0752",
|
||||
Name: "e1fbdbe9-cebf-42ed-8065-bf4882ccf76b",
|
||||
Name: "01fbdbe9-cebf-42ed-8065-bf4882ccf76b",
|
||||
Location: "6d5b5450-1f87-47cb-b185-f64c35fae3c1",
|
||||
GPIONumber: "GPIO14",
|
||||
Model: "DHT11",
|
||||
@ -206,7 +206,7 @@ func testBackend(t *testing.T, repo *repository.Repository) {
|
||||
sensors, err = repo.GetSensors("DHT11", "DS18B20")
|
||||
require.NoError(err)
|
||||
require.Len(sensors, 2)
|
||||
require.ElementsMatch(expectedSensors[0:2], sensors)
|
||||
require.JSONEq(jsonEncoder(expectedSensors[0:2]), jsonEncoder(sensors[0:2]))
|
||||
|
||||
// Test: GetSensor
|
||||
sensor, err := repo.GetSensor(expectedSensors[0].ID)
|
||||
@ -340,7 +340,7 @@ func jsonEncoder(v interface{}) string {
|
||||
}
|
||||
|
||||
func timeNow(require *require.Assertions) *time.Time {
|
||||
now, err := time.Parse("2006-01-02 15:04:05.999999Z", time.Now().Format("2006-01-02 15:04:05.999999Z"))
|
||||
now, err := time.Parse("2006-01-02 15:04:05.999999-07", time.Now().Format("2006-01-02 15:04:05.999999-07"))
|
||||
require.NoError(err)
|
||||
return &now
|
||||
}
|
||||
|
Reference in New Issue
Block a user