mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-27 17:24:12 -05:00
Update Environment to TimescaleDB
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System.Reflection;
|
||||
using Dapper;
|
||||
using Dapper;
|
||||
using DbUp;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Npgsql;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||
|
||||
@@ -9,12 +9,13 @@ public class Database(IConfiguration configuration)
|
||||
{
|
||||
private string GetConnectionString()
|
||||
{
|
||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
|
||||
{
|
||||
DataSource = $"{configuration["Environment:Database:Host"]},{configuration["Environment:Database:Port"]}",
|
||||
UserID = configuration["Environment:Database:User"],
|
||||
Host = configuration["Environment:Database:Host"],
|
||||
Port = configuration.GetValue<int>("Environment:Database:Port"),
|
||||
Username = configuration["Environment:Database:User"],
|
||||
Password = configuration["Environment:Database:Password"],
|
||||
InitialCatalog = configuration["Environment:Database:Name"],
|
||||
Database = configuration["Environment:Database:Name"],
|
||||
TrustServerCertificate = bool.Parse(configuration["Environment:Database:TrustServerCertificate"] ?? "false")
|
||||
};
|
||||
|
||||
@@ -25,16 +26,16 @@ public class Database(IConfiguration configuration)
|
||||
{
|
||||
var connectionString = GetConnectionString();
|
||||
|
||||
DbUp.EnsureDatabase.For.SqlDatabase(connectionString);
|
||||
DbUp.EnsureDatabase.For.PostgresqlDatabase(connectionString);
|
||||
|
||||
var upgradeEngine = DeployChanges.To.SqlDatabase(connectionString).WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), s => s.Contains(".Schema.")).LogToConsole().Build();
|
||||
var upgradeEngine = DeployChanges.To.PostgresqlDatabase(connectionString).WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), s => s.Contains(".Schema.")).LogToConsole().Build();
|
||||
|
||||
upgradeEngine.PerformUpgrade();
|
||||
}
|
||||
|
||||
private SqlConnection CreateConnection()
|
||||
private NpgsqlConnection CreateConnection()
|
||||
{
|
||||
var connection = new SqlConnection(GetConnectionString());
|
||||
var connection = new NpgsqlConnection(GetConnectionString());
|
||||
connection.Open();
|
||||
|
||||
return connection;
|
||||
|
||||
@@ -1,25 +1,30 @@
|
||||
BEGIN TRANSACTION
|
||||
|
||||
INSERT Reading
|
||||
(Timestamp, Name, Model, Temperature, Pressure, Humidity, Luminance, GasResistance, ColorTemperature, AirQualityIndex)
|
||||
SELECT
|
||||
@Timestamp,
|
||||
@Name,
|
||||
@Model,
|
||||
@Temperature,
|
||||
@Pressure,
|
||||
@Humidity,
|
||||
@Luminance,
|
||||
@GasResistance,
|
||||
@ColorTemperature,
|
||||
@AirQualityIndex
|
||||
WHERE NOT EXISTS
|
||||
(
|
||||
SELECT
|
||||
1
|
||||
FROM
|
||||
Reading WITH (UPDLOCK, SERIALIZABLE)
|
||||
WHERE Timestamp = @Timestamp AND Name = @Name AND Model = @Model
|
||||
)
|
||||
|
||||
COMMIT TRANSACTION
|
||||
INSERT INTO
|
||||
reading
|
||||
(
|
||||
time,
|
||||
name,
|
||||
model,
|
||||
temperature,
|
||||
pressure,
|
||||
humidity,
|
||||
luminance,
|
||||
gas_resistance,
|
||||
color_temperature,
|
||||
air_quality_index
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@Timestamp,
|
||||
@Name,
|
||||
@Model,
|
||||
@Temperature,
|
||||
@Pressure,
|
||||
@Humidity,
|
||||
@Luminance,
|
||||
@GasResistance,
|
||||
@ColorTemperature,
|
||||
@AirQualityIndex
|
||||
)
|
||||
ON CONFLICT
|
||||
ON CONSTRAINT reading_pk
|
||||
DO NOTHING
|
||||
@@ -1,14 +1,17 @@
|
||||
CREATE TABLE Reading
|
||||
(
|
||||
Timestamp datetimeoffset NOT NULL,
|
||||
Name nvarchar(50) NOT NULL,
|
||||
Model nvarchar(50) NOT NULL,
|
||||
Temperature decimal(5, 2) NOT NULL,
|
||||
Pressure decimal(6, 2) NOT NULL,
|
||||
Humidity decimal(5, 2) NOT NULL,
|
||||
Luminance int NOT NULL,
|
||||
GasResistance int NOT NULL,
|
||||
ColorTemperature int NOT NULL,
|
||||
AirQualityIndex decimal(4, 1) NOT NULL,
|
||||
CONSTRAINT reading_pk PRIMARY KEY (Timestamp, Name, Model)
|
||||
);
|
||||
CREATE TABLE
|
||||
reading (
|
||||
time timestamptz NOT NULL,
|
||||
name text NOT NULL,
|
||||
model text NOT NULL,
|
||||
temperature DECIMAL NOT NULL,
|
||||
pressure DECIMAL NOT NULL,
|
||||
humidity DECIMAL NOT NULL,
|
||||
luminance INT NOT NULL,
|
||||
gas_resistance INT NOT NULL,
|
||||
color_temperature INT NOT NULL,
|
||||
air_quality_index DECIMAL NOT NULL,
|
||||
CONSTRAINT reading_pk PRIMARY KEY (time, name, model)
|
||||
);
|
||||
|
||||
SELECT
|
||||
create_hypertable('reading', by_range('time'));
|
||||
Reference in New Issue
Block a user