Added database migrations

main
fbp 2 years ago
parent ce1d093bb1
commit de68b356e9

@ -7,11 +7,20 @@
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"name": "Run",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js"
},
{
"type": "node",
"request": "launch",
"name": "Migrations",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/migrations.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();

@ -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);
})
Loading…
Cancel
Save