From 7aeb83fde3eebd6fefc202c40375f75506a3d997 Mon Sep 17 00:00:00 2001 From: Avris Date: Mon, 12 Oct 2020 11:46:26 +0200 Subject: [PATCH] #68 proper db migrations --- .gitignore | 2 ++ Makefile | 2 ++ migrations/001-legacy.sql | 19 +++++++++++++++++++ routes/nouns.vue | 2 +- server/db.js | 7 +++++++ server/initDb.js | 35 ----------------------------------- server/migrate.js | 8 ++++++++ server/notify.js | 8 ++------ server/nouns.js | 8 ++------ 9 files changed, 43 insertions(+), 48 deletions(-) create mode 100644 migrations/001-legacy.sql create mode 100644 server/db.js delete mode 100644 server/initDb.js create mode 100644 server/migrate.js diff --git a/.gitignore b/.gitignore index 04927aff..a8d7aa7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ /data /locale/*/db.sqlite +/db.sqlite + /daemonise.json /daemonise.log diff --git a/Makefile b/Makefile index 14d754d7..079bf5c9 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ install: yarn + node server/migrate.js run: yarn dev @@ -12,6 +13,7 @@ stop: deploy: install yarn build + node server/migrate.js switch: -rm data diff --git a/migrations/001-legacy.sql b/migrations/001-legacy.sql new file mode 100644 index 00000000..5c85dcce --- /dev/null +++ b/migrations/001-legacy.sql @@ -0,0 +1,19 @@ +-- Up + +CREATE TABLE nouns ( + id TEXT NOT NULL PRIMARY KEY, + masc TEXT NOT NULL, + fem TEXT NOT NULL, + neutr TEXT NOT NULL, + mascPl TEXT NOT NULL, + femPl TEXT NOT NULL, + neutrPl TEXT NOT NULL, + approved INTEGER NOT NULL, + base_id TEXT +); + +INSERT INTO nouns VALUES ('01EEKD4RYFWXG63Y563HF0RP56', 'pan', 'pani', 'panu|państwo', 'panowie', 'panie', 'państwo', 1, null); + +-- Down + +DROP TABLE nouns; diff --git a/routes/nouns.vue b/routes/nouns.vue index 25fd4a03..cba91c30 100644 --- a/routes/nouns.vue +++ b/routes/nouns.vue @@ -8,7 +8,7 @@
nouns.intro - +
diff --git a/server/db.js b/server/db.js new file mode 100644 index 00000000..bc924ecc --- /dev/null +++ b/server/db.js @@ -0,0 +1,7 @@ +const sqlite = require('sqlite'); +const sqlite3 = require('sqlite3'); + +module.exports = _ => sqlite.open({ + filename: __dirname + '/../db.sqlite', + driver: sqlite3.Database, +}); diff --git a/server/initDb.js b/server/initDb.js deleted file mode 100644 index 3d0995b1..00000000 --- a/server/initDb.js +++ /dev/null @@ -1,35 +0,0 @@ -const sqlite = require('sqlite'); -const sqlite3 = require('sqlite3'); -const ulid = require('ulid').ulid; - -const fixtures = [ - ['pan', 'pani', 'panu|państwo', 'panowie', 'panie', 'państwo'], - ['ojciec', 'matka', 'rodzic', 'ojcowie', 'matki', 'rodzice'], - ['filolog', 'filolożka', 'filologum', 'filologowie', 'filolożki', 'filologa'], -]; - -async function initDb() { - const db = await sqlite.open({ - filename: __dirname + '/../data/db.sqlite', - driver: sqlite3.Database, - }); - - await db.exec(`DROP TABLE IF EXISTS nouns`); - await db.exec(`CREATE TABLE nouns ( - id TEXT NOT NULL PRIMARY KEY, - masc TEXT NOT NULL, - fem TEXT NOT NULL, - neutr TEXT NOT NULL, - mascPl TEXT NOT NULL, - femPl TEXT NOT NULL, - neutrPl TEXT NOT NULL, - approved INTEGER NOT NULL, - base_id TEXT - )`); - - for (let fixture of fixtures) { - await db.get('INSERT OR REPLACE INTO nouns VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', ulid(), ...fixture, 1, null); - } -} - -initDb(); diff --git a/server/migrate.js b/server/migrate.js new file mode 100644 index 00000000..5d7e48f6 --- /dev/null +++ b/server/migrate.js @@ -0,0 +1,8 @@ +const dbConnection = require('./db'); + +async function migrate() { + const db = await dbConnection(); + await db.migrate({migrationsPath: __dirname + '/../migrations'}) +} + +migrate(); diff --git a/server/notify.js b/server/notify.js index ae4bac4a..2a6faf6d 100644 --- a/server/notify.js +++ b/server/notify.js @@ -1,13 +1,9 @@ -const sqlite = require('sqlite'); -const sqlite3 = require('sqlite3'); +const dbConnection = require('./db'); const mailer = require('mailer'); require('dotenv').config({ path:__dirname + '/../.env' }); async function notify() { - const db = await sqlite.open({ - filename: __dirname + '/../data/db.sqlite', - driver: sqlite3.Database, - }); + const db = await dbConnection(); const awaitingModeration = (await db.get(`SELECT count(*) as c FROM nouns WHERE approved = 0`)).c; if (!awaitingModeration) { diff --git a/server/nouns.js b/server/nouns.js index 9ef64e8b..c309f4f0 100644 --- a/server/nouns.js +++ b/server/nouns.js @@ -1,5 +1,4 @@ -const sqlite = require('sqlite'); -const sqlite3 = require('sqlite3'); +const dbConnection = require('./db'); const SQL = require('sql-template-strings'); import { ulid } from 'ulid' @@ -64,10 +63,7 @@ const isTroll = (body) => { } export default async function (req, res, next) { - const db = await sqlite.open({ - filename: __dirname + '/../data/db.sqlite', - driver: sqlite3.Database, - }); + const db = await dbConnection(); const [url, queryString] = req.url.split('?'); const query = parseQuery(queryString || '');