mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-19 03:29:37 +02:00
Database migrations
This commit is contained in:
parent
1625932e52
commit
84bd641cf2
8 changed files with 282 additions and 53 deletions
51
db/index.js
Normal file
51
db/index.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
const { Sequelize } = require('sequelize');
|
||||
const { join } = require('path');
|
||||
const fs = require('fs');
|
||||
const Umzug = require('umzug');
|
||||
|
||||
const Logger = require('../utils/Logger');
|
||||
const logger = new Logger();
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: './data/db.sqlite',
|
||||
logging: false,
|
||||
});
|
||||
|
||||
const umzug = new Umzug({
|
||||
migrations: {
|
||||
path: join(__dirname, './migrations'),
|
||||
params: [sequelize.getQueryInterface()],
|
||||
},
|
||||
storage: 'sequelize',
|
||||
storageOptions: {
|
||||
sequelize,
|
||||
},
|
||||
});
|
||||
|
||||
const connectDB = async () => {
|
||||
try {
|
||||
if (fs.existsSync('data/db.sqlite')) {
|
||||
fs.copyFileSync('data/db.sqlite', 'data/backup_db.sqlite');
|
||||
}
|
||||
|
||||
await sequelize.authenticate();
|
||||
logger.log('Connected to database');
|
||||
|
||||
// migrations
|
||||
const pendingMigrations = await umzug.pending();
|
||||
|
||||
if (pendingMigrations.length > 0) {
|
||||
logger.log('Executing pending migrations');
|
||||
await umzug.up();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.log(`Unable to connect to the database: ${error.message}`, 'ERROR');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
connectDB,
|
||||
sequelize,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue