From de68b356e93ad364290fb43a3fc7151a18d7028f Mon Sep 17 00:00:00 2001 From: fbp Date: Mon, 14 Aug 2023 14:14:14 +0200 Subject: [PATCH] Added database migrations --- .vscode/launch.json | 11 ++++++++++- index.js | 2 ++ migrations.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 migrations.js diff --git a/.vscode/launch.json b/.vscode/launch.json index d2772d1..9cea3f7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,11 +7,20 @@ { "type": "node", "request": "launch", - "name": "Launch Program", + "name": "Run", "skipFiles": [ "/**" ], "program": "${workspaceFolder}/index.js" + }, + { + "type": "node", + "request": "launch", + "name": "Migrations", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/migrations.js" } ] } \ No newline at end of file diff --git a/index.js b/index.js index fc11198..e7b8627 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ require("dotenv").config(); const mqtt = require("mqtt"); const express = require("express"); +const db = require("./db"); + const { Armed, Disarmed, Triggered, Arming, Disarming } = require("./state"); const app = express(); diff --git a/migrations.js b/migrations.js new file mode 100644 index 0000000..b6e4bd6 --- /dev/null +++ b/migrations.js @@ -0,0 +1,48 @@ +require("dotenv").config(); + +const db = require("./db"); + +db.query(` +DROP TABLE IF EXISTS public.temperature_measurements; +DROP TABLE IF EXISTS public.light_measurements; +DROP TABLE IF EXISTS public.events; + +DROP TYPE IF EXISTS public."EventType"; + +CREATE TYPE public."EventType" AS ENUM +( + 'Armed', + 'Arming', + 'Disarmed', + 'Disarming', + 'Triggered' +); + +CREATE TABLE public.temperature_measurements +( + id SERIAL NOT NULL PRIMARY KEY, + deviceId INTEGER NOT NULL, + value DOUBLE PRECISION NOT NULL, + timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); + +CREATE TABLE public.light_measurements +( + id SERIAL NOT NULL PRIMARY KEY, + deviceId INTEGER NOT NULL, + value DOUBLE PRECISION NOT NULL, + timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); + +CREATE TABLE public.events +( + id SERIAL NOT NULL PRIMARY KEY, + deviceId INTEGER, + type "EventType" NOT NULL, + timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); +`) +.then(()=>{ + db.end(); + process.exit(0); +}) \ No newline at end of file