fix(orm): try and sync database after loading models
parent
a5b5d90702
commit
7a86cef5a5
|
@ -7,6 +7,8 @@ import DriveSegments from './drive_segments.model';
|
|||
import Drives from './drives.model';
|
||||
import OAuthAccounts from './oauth_accounts.model';
|
||||
|
||||
import orm from './orm';
|
||||
|
||||
export {
|
||||
Accounts,
|
||||
AthenaActionLog,
|
||||
|
@ -17,3 +19,5 @@ export {
|
|||
Drives,
|
||||
OAuthAccounts,
|
||||
};
|
||||
|
||||
export default orm;
|
||||
|
|
|
@ -11,14 +11,4 @@ const sequelize = new Sequelize({
|
|||
|
||||
sequelize.options.logging = () => {};
|
||||
|
||||
/**
|
||||
* Synchronise the database (create new tables) to match the models defined
|
||||
* above.
|
||||
*
|
||||
* WARNING: If force is set, sequelize will delete columns and create new ones
|
||||
* if their types have changed!
|
||||
* Use sequelize-cli and migrations instead!
|
||||
*/
|
||||
sequelize.sync({ force: process.env.DB_FORCE_SYNC === 'true' });
|
||||
|
||||
export default sequelize;
|
||||
|
|
|
@ -3,12 +3,8 @@ import cors from 'cors';
|
|||
import express from 'express';
|
||||
import log4js from 'log4js';
|
||||
|
||||
import storageController from './controllers/storage';
|
||||
import controllers from './controllers';
|
||||
import router from './router';
|
||||
import { Accounts, Devices, Drives } from '../models';
|
||||
|
||||
const logger = log4js.getLogger();
|
||||
|
||||
function runAsyncWrapper(callback) {
|
||||
return function wrapper(req, res, next) {
|
||||
|
@ -17,20 +13,10 @@ function runAsyncWrapper(callback) {
|
|||
};
|
||||
}
|
||||
|
||||
const tasks = [];
|
||||
export default async () => {
|
||||
const logger = log4js.getLogger();
|
||||
const app = express();
|
||||
|
||||
storageController.initializeStorage();
|
||||
tasks.push(storageController.updateTotalStorageUsed());
|
||||
|
||||
// debug: print out some info from the database
|
||||
Promise.all([Accounts.findAll(), Devices.findAll(), Drives.findAll()])
|
||||
.then(([accounts, devices, drives]) => {
|
||||
logger.info(`Found ${accounts.length} accounts`);
|
||||
logger.info(`Found ${devices.length} devices`);
|
||||
logger.info(`Found ${drives.length} drives`);
|
||||
});
|
||||
|
||||
app.use(cors({
|
||||
origin: ['http://localhost:3000', 'https://connect.retropilot.org'],
|
||||
credentials: true,
|
||||
|
@ -62,4 +48,5 @@ app.post('*', runAsyncWrapper(async (req, res) => {
|
|||
res.send('Not Implemented');
|
||||
}));
|
||||
|
||||
export default Promise.all(tasks).then(() => app);
|
||||
return app;
|
||||
};
|
||||
|
|
|
@ -3,13 +3,36 @@ import http from 'http';
|
|||
import log4js from 'log4js';
|
||||
|
||||
import app from './app';
|
||||
import storageController from './controllers/storage';
|
||||
import orm, { Accounts, Devices, Drives } from '../models';
|
||||
|
||||
export default async () => {
|
||||
const server = await app;
|
||||
|
||||
const logger = log4js.getLogger();
|
||||
const httpServer = http.createServer(server);
|
||||
|
||||
storageController.initializeStorage();
|
||||
await storageController.updateTotalStorageUsed();
|
||||
|
||||
/**
|
||||
* Synchronise the database (create new tables) to match the models defined
|
||||
* above.
|
||||
*
|
||||
* WARNING: If force is set, sequelize will delete columns and create new ones
|
||||
* if their types have changed!
|
||||
* Use sequelize-cli and migrations instead!
|
||||
*/
|
||||
const options = { force: process.env.DB_FORCE_SYNC === 'true' };
|
||||
await orm.sync(options);
|
||||
logger.info('Database synced', options);
|
||||
|
||||
// debug: print out some info from the database
|
||||
Promise.all([Accounts.findAll(), Devices.findAll(), Drives.findAll()])
|
||||
.then(([accounts, devices, drives]) => {
|
||||
logger.info(`Found ${accounts.length} accounts`);
|
||||
logger.info(`Found ${devices.length} devices`);
|
||||
logger.info(`Found ${drives.length} drives`);
|
||||
});
|
||||
|
||||
const httpServer = http.createServer(await app());
|
||||
httpServer.listen(process.env.HTTP_PORT, () => {
|
||||
logger.info(`RetroPilot Server listening at ${process.env.BASE_URL}`);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue