Add framework for database setup and migration
This commit is contained in:
parent
b9378a2356
commit
cdcaef11c1
|
@ -8,3 +8,4 @@ dev/
|
||||||
config.json
|
config.json
|
||||||
*.sqlite*
|
*.sqlite*
|
||||||
*.db
|
*.db
|
||||||
|
.dbversion
|
|
@ -14,6 +14,7 @@
|
||||||
"build": "npm run process-images && npm run bundle",
|
"build": "npm run process-images && npm run bundle",
|
||||||
"bundle": "parcel build app/index.html --out-dir public --no-source-maps --no-cache",
|
"bundle": "parcel build app/index.html --out-dir public --no-source-maps --no-cache",
|
||||||
"process-images": "node ./process-images.js",
|
"process-images": "node ./process-images.js",
|
||||||
|
"setup-db": "node ./server/sequelize/setup-database.js",
|
||||||
"clear": "rimraf public/{*,.*}"
|
"clear": "rimraf public/{*,.*}"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const dbVersion = '0.0.0';
|
||||||
|
|
||||||
|
function migrateDb(oldVersion, sequelize) {
|
||||||
|
// if (oldVersion < targetVersion) {
|
||||||
|
// migrate db here
|
||||||
|
// }
|
||||||
|
return sequelize;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
dbVersion,
|
||||||
|
migrateDb,
|
||||||
|
};
|
|
@ -1,12 +1,13 @@
|
||||||
// Eventually, I'd like this to be run through Commander so it can confirm things before just overwriting stuff.
|
// Eventually, I'd like this to be run through Commander so it can confirm things before just overwriting stuff.
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const Sequelize = require('sequelize');
|
const fs = require('fs');
|
||||||
const getModels = require('./server/sequelize/getModels');
|
|
||||||
const force = typeof process.argv[2] !== 'undefined' && process.argv[2] === 'force';
|
const force = typeof process.argv[2] !== 'undefined' && process.argv[2] === 'force';
|
||||||
|
|
||||||
|
const Sequelize = require('sequelize');
|
||||||
let siteConfig;
|
let siteConfig;
|
||||||
try {
|
try {
|
||||||
siteConfig = require('./server/config.json');
|
siteConfig = require('../config.json');
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error('Please copy `config.example.json` to `config.json` and fill it with your server\'s data.');
|
console.error('Please copy `config.example.json` to `config.json` and fill it with your server\'s data.');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -37,9 +38,34 @@ switch (siteConfig.db_engine) {
|
||||||
|
|
||||||
const sequelize = new Sequelize(sequelizeConfig);
|
const sequelize = new Sequelize(sequelizeConfig);
|
||||||
|
|
||||||
const Models = getModels(sequelize);
|
const migration = require('./migration');
|
||||||
|
const dbVersionPath = path.resolve(__dirname, './.dbversion');
|
||||||
|
if (!force) {
|
||||||
|
if (fs.existsSync(dbVersionPath)) {
|
||||||
|
const installedDbVersion = fs.readFileSync(dbVersionPath);
|
||||||
|
if (installedDbVersion < migration.dbVersion) {
|
||||||
|
console.log(`Migrating from ${installedDbVersion} to ${migration.dbVersion}...`);
|
||||||
|
migration.migrateDb(installedDbVersion, sequelize);
|
||||||
|
return fs.writeFile(dbVersionPath, migration.dbVersion, err => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
console.log('Migration complete!');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (installedDbVersion == migration.dbVersion) {
|
||||||
|
return console.log(`No database setup needed: installed version ${installedDbVersion} is the current version.`);
|
||||||
|
}
|
||||||
|
console.log(`Skipping database migration: installed version ${installedDbVersion} is not less than current version ${migration.dbVersion}.`);
|
||||||
|
return console.log('Please check your installation and make sure you have the right server files downloaded.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Models = require('./getModels')(sequelize);
|
||||||
|
|
||||||
|
console.log(`Installing database tables${force ? ', dropping existing ones first' : ''}...`);
|
||||||
sequelize.sync({ force }).then(() => {
|
sequelize.sync({ force }).then(() => {
|
||||||
|
console.log('Tables installed! Adding Status Types...')
|
||||||
const promises = [ // Default status types to use in Statuses
|
const promises = [ // Default status types to use in Statuses
|
||||||
{ name: 'update' },
|
{ name: 'update' },
|
||||||
{ name: 'progress' },
|
{ name: 'progress' },
|
||||||
|
@ -49,4 +75,11 @@ sequelize.sync({ force }).then(() => {
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
sequelize.close();
|
sequelize.close();
|
||||||
|
console.log(`Status Types installed! Writing database version to ${dbVersionPath}...`);
|
||||||
|
fs.writeFile(dbVersionPath, migration.dbVersion, err => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
console.log('Done!');
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue