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');
|
2019-12-08 08:06:21 +01:00
|
|
|
const fs = require('fs');
|
2019-09-28 02:33:13 +02:00
|
|
|
const force = typeof process.argv[2] !== 'undefined' && process.argv[2] === 'force';
|
2019-12-08 08:06:21 +01:00
|
|
|
|
|
|
|
const Sequelize = require('sequelize');
|
2019-09-19 01:15:06 +02:00
|
|
|
let siteConfig;
|
|
|
|
try {
|
2019-12-08 08:06:21 +01:00
|
|
|
siteConfig = require('../config.json');
|
2019-09-19 01:15:06 +02:00
|
|
|
} 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);
|
2020-01-14 23:26:39 +01:00
|
|
|
const models = require('./models')(sequelize);
|
2019-09-19 01:15:06 +02:00
|
|
|
|
2019-12-08 08:06:21 +01:00
|
|
|
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}...`);
|
2020-01-14 23:26:39 +01:00
|
|
|
migration.migrateDb(installedDbVersion, sequelize, models);
|
2019-12-08 08:06:21 +01:00
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Installing database tables${force ? ', dropping existing ones first' : ''}...`);
|
2019-09-28 02:33:13 +02:00
|
|
|
sequelize.sync({ force }).then(() => {
|
2020-01-14 23:26:39 +01:00
|
|
|
console.log(`Tables installed! Creating Permission Levels...`);
|
|
|
|
return models.PermissionLevel.bulkCreate([
|
|
|
|
{
|
|
|
|
id: 100,
|
|
|
|
name: 'db.privacyLevel.private',
|
|
|
|
description: 'db.privacyLevel.privateDescription',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 66,
|
|
|
|
name: 'db.privacyLevel.friends',
|
|
|
|
description: 'db.privacyLevel.friendsDescription',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 33,
|
|
|
|
name: 'db.privacyLevel.followers',
|
|
|
|
description: 'db.privacyLevel.followersDescription',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
name: 'db.privacyLevel.public',
|
|
|
|
description: 'db.privacyLevel.publicDescription',
|
|
|
|
},
|
|
|
|
]).catch((err) => {
|
|
|
|
console.error('Could not create Permission Levels:\n', err);
|
|
|
|
});
|
|
|
|
}).then(() => {
|
2019-09-27 02:31:32 +02:00
|
|
|
sequelize.close();
|
2020-01-14 23:26:39 +01:00
|
|
|
console.log(`Permission Levels created! Writing database version to ${dbVersionPath}...`);
|
2019-12-08 08:06:21 +01:00
|
|
|
fs.writeFile(dbVersionPath, migration.dbVersion, err => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
console.log('Done!');
|
|
|
|
});
|
|
|
|
});
|