Add framework for database setup and migration
This commit is contained in:
parent
b9378a2356
commit
cdcaef11c1
|
@ -8,3 +8,4 @@ dev/
|
|||
config.json
|
||||
*.sqlite*
|
||||
*.db
|
||||
.dbversion
|
|
@ -14,6 +14,7 @@
|
|||
"build": "npm run process-images && npm run bundle",
|
||||
"bundle": "parcel build app/index.html --out-dir public --no-source-maps --no-cache",
|
||||
"process-images": "node ./process-images.js",
|
||||
"setup-db": "node ./server/sequelize/setup-database.js",
|
||||
"clear": "rimraf public/{*,.*}"
|
||||
},
|
||||
"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.
|
||||
|
||||
const path = require('path');
|
||||
const Sequelize = require('sequelize');
|
||||
const getModels = require('./server/sequelize/getModels');
|
||||
const fs = require('fs');
|
||||
const force = typeof process.argv[2] !== 'undefined' && process.argv[2] === 'force';
|
||||
|
||||
const Sequelize = require('sequelize');
|
||||
let siteConfig;
|
||||
try {
|
||||
siteConfig = require('./server/config.json');
|
||||
siteConfig = require('../config.json');
|
||||
} catch (ex) {
|
||||
console.error('Please copy `config.example.json` to `config.json` and fill it with your server\'s data.');
|
||||
process.exit(1);
|
||||
|
@ -37,9 +38,34 @@ switch (siteConfig.db_engine) {
|
|||
|
||||
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(() => {
|
||||
console.log('Tables installed! Adding Status Types...')
|
||||
const promises = [ // Default status types to use in Statuses
|
||||
{ name: 'update' },
|
||||
{ name: 'progress' },
|
||||
|
@ -49,4 +75,11 @@ sequelize.sync({ force }).then(() => {
|
|||
return Promise.all(promises);
|
||||
}).then(() => {
|
||||
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