Implement version checking

master v0.1
Dessa Simpson 2020-11-01 13:47:47 -07:00
parent 8e47cd9ea9
commit 767cfbf488
4 changed files with 38 additions and 2 deletions

11
db/00-version.sql Normal file
View File

@ -0,0 +1,11 @@
CREATE TABLE version (
major INT NOT NULL,
minor INT NOT NULL,
PRIMARY KEY (major,minor)
);
CREATE OR REPLACE FUNCTION get_version() RETURNS VARCHAR
AS $$SELECT major || '.' || minor FROM version $$
LANGUAGE SQL;
INSERT INTO version (major,minor) VALUES (0,1);

View File

@ -11,6 +11,7 @@ import pgSessionStore from "connect-pg-simple";
import fetch, { Response as FetchResponse } from "node-fetch";
import db from "./db";
import errorHandler from "./errors";
import * as version from "./version";
console.log("Starting at " + new Date().toISOString());
@ -345,6 +346,7 @@ async function processBannedUsers() {
setTimeout(processBannedUsers,600000+Math.floor(Math.random()*600000))
app.listen(config.port, () => {
// Check version then listen
version.checkVersion().then(_ => app.listen(config.port, () => {
console.log(`Listening on port ${config.port}`);
});
}));

View File

@ -96,3 +96,8 @@ export const getValidStates = {
name: "getValidStates",
text: "SELECT * FROM states"
}
export const getDbVersion = {
name: "getDbVersion",
text: "SELECT get_version()"
}

18
src/version.ts Normal file
View File

@ -0,0 +1,18 @@
import * as queries from "./queries";
import pg from "pg";
import db from "./db";
var versionMajor = 0;
var versionMinor = 1;
var versionPatch = 0;
export function getVersion() {
return `${versionMajor}.${versionMinor}.${versionPatch}`
}
export async function checkVersion() {
var dbver = await db.query(queries.getDbVersion).then((result: pg.QueryResult) => result.rows[0]['get_version']);
if (dbver != `${versionMajor}.${versionMinor}`) {
throw new Error("Version mismatch!\nApplication version: " + getVersion() + "\nDatabase version: " + dbver)
}
}