fix(orm): try and sync database after loading models

pull/4/head
Cameron Clough 2022-03-24 13:51:18 +00:00
parent a5b5d90702
commit 7a86cef5a5
No known key found for this signature in database
GPG Key ID: BFB3B74B026ED43F
4 changed files with 61 additions and 57 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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,49 +13,40 @@ function runAsyncWrapper(callback) {
};
}
const tasks = [];
const app = express();
export default async () => {
const logger = log4js.getLogger();
const app = express();
storageController.initializeStorage();
tasks.push(storageController.updateTotalStorageUsed());
app.use(cors({
origin: ['http://localhost:3000', 'https://connect.retropilot.org'],
credentials: true,
}));
app.use(cookieParser());
// 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(router);
app.use('/favicon.ico', express.static('static/favicon.ico'));
app.use(process.env.BASE_DRIVE_DOWNLOAD_PATH_MAPPING, express.static(process.env.STORAGE_PATH));
app.use('/.well-known', express.static('.well-known'));
app.use('/cabana', express.static('cabana/'));
app.get('/', async (req, res) => {
res.redirect('/useradmin');
});
app.use(cors({
origin: ['http://localhost:3000', 'https://connect.retropilot.org'],
credentials: true,
}));
app.use(cookieParser());
app.get('*', runAsyncWrapper(async (req, res) => {
logger.error(`HTTP.GET unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`);
res.status(404);
res.send('Not Implemented');
}));
app.use(router);
app.post('*', runAsyncWrapper(async (req, res) => {
logger.error(`HTTP.POST unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`);
res.status(404);
res.send('Not Implemented');
}));
app.use('/favicon.ico', express.static('static/favicon.ico'));
app.use(process.env.BASE_DRIVE_DOWNLOAD_PATH_MAPPING, express.static(process.env.STORAGE_PATH));
app.use('/.well-known', express.static('.well-known'));
app.use('/cabana', express.static('cabana/'));
app.get('/', async (req, res) => {
res.redirect('/useradmin');
});
app.get('*', runAsyncWrapper(async (req, res) => {
logger.error(`HTTP.GET unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`);
res.status(404);
res.send('Not Implemented');
}));
app.post('*', runAsyncWrapper(async (req, res) => {
logger.error(`HTTP.POST unhandled request: ${controllers.helpers.simpleStringify(req)}, ${controllers.helpers.simpleStringify(res)}`);
res.status(404);
res.send('Not Implemented');
}));
export default Promise.all(tasks).then(() => app);
return app;
};

View File

@ -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}`);
});