#68 proper db migrations

This commit is contained in:
Avris 2020-10-12 11:46:26 +02:00
parent 5acdfe0c07
commit 7aeb83fde3
9 changed files with 43 additions and 48 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
/data
/locale/*/db.sqlite
/db.sqlite
/daemonise.json
/daemonise.log

View File

@ -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

19
migrations/001-legacy.sql Normal file
View File

@ -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;

View File

@ -8,7 +8,7 @@
<section>
<T>nouns.intro</T>
<Share :title="$t('nouns.headerLonger')"/>
<Share :title="$t('nouns.headerLong')"/>
</section>
<Separator icon="book-open"/>

7
server/db.js Normal file
View File

@ -0,0 +1,7 @@
const sqlite = require('sqlite');
const sqlite3 = require('sqlite3');
module.exports = _ => sqlite.open({
filename: __dirname + '/../db.sqlite',
driver: sqlite3.Database,
});

View File

@ -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();

8
server/migrate.js Normal file
View File

@ -0,0 +1,8 @@
const dbConnection = require('./db');
async function migrate() {
const db = await dbConnection();
await db.migrate({migrationsPath: __dirname + '/../migrations'})
}
migrate();

View File

@ -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) {

View File

@ -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 || '');