2019-09-27 02:31:32 +02:00
|
|
|
// Eventually, I'd like this to be run through Commander so it can confirm things before just overwriting stuff.
|
|
|
|
|
2019-09-19 01:15:06 +02:00
|
|
|
const path = require('path');
|
|
|
|
const Sequelize = require('sequelize');
|
2019-09-27 04:03:15 +02:00
|
|
|
const getSequelizeModels = require('./server/getSequelizeModels');
|
2019-09-28 02:33:13 +02:00
|
|
|
const force = typeof process.argv[2] !== 'undefined' && process.argv[2] === 'force';
|
2019-09-19 01:15:06 +02:00
|
|
|
let siteConfig;
|
|
|
|
try {
|
|
|
|
siteConfig = require('./server/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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sequelizeConfig = {
|
|
|
|
dialect: siteConfig.db_engine,
|
|
|
|
};
|
|
|
|
switch (siteConfig.db_engine) {
|
|
|
|
case 'sqlite': {
|
|
|
|
sequelizeConfig.storage = typeof siteConfig.sqlite_location !== 'undefined'
|
2019-09-27 02:52:34 +02:00
|
|
|
? (
|
|
|
|
siteConfig.sqlite_location.substr(0, 1) === '.' // If relative path, make relative to ./server
|
|
|
|
? path.resolve('./server/', siteConfig.sqlite_location)
|
|
|
|
: path.resolve(siteConfig.sqlite_location)
|
|
|
|
)
|
|
|
|
: path.resolve(__dirname, './server/database.sqlite');
|
2019-09-19 01:15:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
sequelizeConfig.host = siteConfig.db_host;
|
|
|
|
sequelizeConfig.port = siteConfig.db_port;
|
|
|
|
sequelizeConfig.database = siteConfig.db_database;
|
|
|
|
sequelizeConfig.username = siteConfig.db_username;
|
|
|
|
sequelizeConfig.password = siteConfig.db_password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const sequelize = new Sequelize(sequelizeConfig);
|
|
|
|
|
2019-09-27 04:03:15 +02:00
|
|
|
const Models = getSequelizeModels(sequelize);
|
2019-09-19 01:15:06 +02:00
|
|
|
|
2019-09-28 02:33:13 +02:00
|
|
|
sequelize.sync({ force }).then(() => {
|
2019-09-27 02:31:32 +02:00
|
|
|
const promises = [ // Default status types to use in Statuses
|
|
|
|
{ name: 'update' },
|
|
|
|
{ name: 'progress' },
|
|
|
|
{ name: 'rating' },
|
|
|
|
{ name: 'review' },
|
2019-09-27 04:03:15 +02:00
|
|
|
].map(statusType => Models.StatusType.create(statusType).catch(() => console.log(statusType.name, 'already exists')));
|
2019-09-27 02:31:32 +02:00
|
|
|
return Promise.all(promises);
|
|
|
|
}).then(() => {
|
|
|
|
sequelize.close();
|
|
|
|
});
|