summaryrefslogtreecommitdiff
path: root/nodejs/database/db.js
blob: 78fa0ddd3f0353658fb26e0ad9346caf699c5f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import fs from 'fs';
import Database from 'better-sqlite3';

const allMigrations = [
	'database/000_init.sql',
	'database/001_systems.sql',
	'database/002_ships.sql',
	'database/003_surveys.sql',
];

const db = new Database(
	process.env.NODE_ENV === 'test' ? 'test.db' : 'spacetraders.db',
	process.env.NODE_ENV === 'development' ? { verbose: console.log } : null
);
db.pragma('foreign_keys = ON');
db.pragma('journal_mode = WAL');

function init() {
	db.transaction(function migrate() {
		let version;
		try {
			version = db.prepare('SELECT version FROM schema_version').get().version;
		} catch {
			version = 0;
		}
		if (version === allMigrations.length) return;
		while (version < allMigrations.length) {
			db.exec(fs.readFileSync(allMigrations[version], 'utf8'));
			version++;
		}
		db.exec(`DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (${version});`);
	})();
}

export function reset() {
	const indices = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'index';`).all();
	const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table';`).all();
	const triggers = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'trigger';`).all();
	const views = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'view';`).all();
	indices.forEach(elt => db.exec(`DROP INDEX ${elt.name};`));
	tables.forEach(elt => db.exec(`DROP TABLE ${elt.name};`));
	triggers.forEach(elt => db.exec(`DROP TRIGGER ${elt.name};`));
	views.forEach(elt => db.exec(`DROP VIEW ${elt.name};`));
	db.exec(`VACUUM;`);
	init();
}

init();

export default db;