Skip to content

Commit

Permalink
Remove meta table and read version from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
robflop committed Jun 18, 2019
1 parent 2b097ea commit 7014d44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 50 deletions.
70 changes: 32 additions & 38 deletions dbUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const databaseVersions = [
{
targetVersion: '8.0.0',
queries: [
`CREATE TABLE IF NOT EXISTS meta (
version TEXT NOT NULL
);`,
'INSERT OR IGNORE INTO meta ( version ) VALUES ( "8.0.0" );',
`CREATE TABLE IF NOT EXISTS sounds_temp (
id INTEGER PRIMARY KEY,
filename TEXT NOT NULL UNIQUE,
Expand Down Expand Up @@ -63,6 +59,7 @@ const databaseVersions = [
( "wahaha", "Wahaha", "Season 2", 0, "megumin" );
`,
'UPDATE sounds SET source = "no-source" WHERE source IS NULL;',
'UPDATE sounds SET source = "no-source" WHERE source = "";',
'UPDATE sounds SET displayname = NULL where filename = "realname";',
'UPDATE sounds SET displayname = "Itai!" where filename = "itai";',
'UPDATE sounds SET displayname = "Yamero!" where filename = "yamero";',
Expand All @@ -84,6 +81,12 @@ const databaseVersions = [
'ALTER TABLE statistics_temp RENAME TO statistics;'
]
},
{
targetVersion: '8.0.1',
queries: [
'DROP TABLE meta;'
]
}
];

const { Database } = require('sqlite3');
Expand All @@ -93,7 +96,8 @@ const { copyFile } = require('fs');
const { databasePath } = require('./src/config.json');

const db = new Database(join('src', databasePath));
let currentVersion, newVersion, force;
let { version: currentVersion } = require('./package.json');
let newVersion, force;

const newVersionCLI = process.argv.filter(arg => arg.includes('newVersion='))[0];
const currentVersionCLI = process.argv.filter(arg => arg.includes('currentVersion='))[0];
Expand All @@ -103,7 +107,6 @@ if (newVersionCLI) newVersion = newVersionCLI.substr(newVersionCLI.indexOf('=')
if (currentVersionCLI) currentVersion = currentVersionCLI.substr(currentVersionCLI.indexOf('=') + 1).trim();
if (forceCLI) force = true;


console.log('This is the megumin.love database migration tool.');
console.log('------------------------------------');
console.log('Creating database backup.');
Expand All @@ -115,39 +118,30 @@ copyFile(db.filename, `${db.filename}.bak`, err => {
}
});

if (!currentVersion) {
db.get('SELECT version FROM meta', [], (selectErr, data) => {
if (selectErr || !data || (data && !data.version)) currentVersion = 'Unknown';
else currentVersion = data.version;
if (!currentVersion) currentVersion = 'Unknown';

console.log('------------------------------------');
console.log(`Detected database version: ${currentVersion}`);

if (currentVersion === 'Unknown') {
console.log('\nPlease enter your version of megumin.love before the update (e.g. 6.0.2 or 7.1.0).');

const currentVersionInput = createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'Starting database version: '
});

currentVersionInput.prompt();

currentVersionInput.on('line', ver => {
currentVersion = ver.trim();
currentVersionInput.close();
});

currentVersionInput.on('close', () => {
if (!newVersion) promptNewVersion();
else executeUpdateQueries();
});
}
else {
if (!newVersion) promptNewVersion(); // eslint-disable-line no-lonely-if
else executeUpdateQueries();
}
console.log('------------------------------------');
console.log(`Detected database version: ${currentVersion}`);

if (currentVersion === 'Unknown') {
console.log('\nPlease enter your version of megumin.love before the update (e.g. 6.0.2 or 7.1.0).');

const currentVersionInput = createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'Starting database version: '
});

currentVersionInput.prompt();

currentVersionInput.on('line', ver => {
currentVersion = ver.trim();
currentVersionInput.close();
});

currentVersionInput.on('close', () => {
if (!newVersion) promptNewVersion();
else executeUpdateQueries();
});
}
else {
Expand Down
Binary file modified src/db/default.db
Binary file not shown.
14 changes: 2 additions & 12 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const { join } = require('path');
const { readdirSync, unlink, rename, copyFile, existsSync, mkdir } = require('fs');
const Logger = require('./resources/js/Logger');
const config = require('./config.json');
const { version: packageVersion } = require('../package.json');
const { version } = require('../package.json');

let counter = 0, daily = 0, weekly = 0, monthly = 0, yearly = 0, average = 0, fetchedDaysAmount = 1;
let sounds = [], statistics = [], chartData = [], milestones = [], version = '';
let sounds = [], statistics = [], chartData = [], milestones = [];

// On-boot database interaction
const db = new Database(config.databasePath, () => {
Expand All @@ -23,16 +23,6 @@ const db = new Database(config.databasePath, () => {
});

db.serialize(() => {
db.get('SELECT version FROM meta', [], (selectErr, row) => {
if (!row || !row.version) {
version = packageVersion;
return Logger.warn('No version number found, substituted by package.json value. You should consider fixing this in the database.');
}
version = row.version;

return Logger.info('Version number loaded.');
});

db.get('SELECT counter FROM main_counter', [], (selectErr, row) => {
if (!row || row.counter === undefined) {
db.run('INSERT INTO main_counter ( counter ) VALUES ( 0 )');
Expand Down

0 comments on commit 7014d44

Please sign in to comment.