summaryrefslogtreecommitdiff
path: root/database/db.js
blob: 6deddcc2ec12fd70f517eb22cd071feba598cd6d (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
import fs from 'fs';
import Database from 'better-sqlite3';

const allMigrations = [
	'database/000_init.sql',
	'database/001_systems.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.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 default db;