mirror of
https://github.com/towfiqi/serpbear
synced 2025-06-26 18:15:54 +00:00
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { Sequelize } from 'sequelize';
|
|
import { Umzug, SequelizeStorage } from 'umzug';
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import db from '../../database/database';
|
|
import verifyUser from '../../utils/verifyUser';
|
|
|
|
type MigrationGetResponse = {
|
|
hasMigrations: boolean,
|
|
}
|
|
|
|
type MigrationPostResponse = {
|
|
migrated: boolean,
|
|
erroor?: string
|
|
}
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const authorized = verifyUser(req, res);
|
|
if (authorized === 'authorized' && req.method === 'GET') {
|
|
await db.sync();
|
|
return getMigrationStatus(req, res);
|
|
}
|
|
if (authorized === 'authorized' && req.method === 'POST') {
|
|
return migrateDatabase(req, res);
|
|
}
|
|
return res.status(401).json({ error: authorized });
|
|
}
|
|
|
|
const getMigrationStatus = async (req: NextApiRequest, res: NextApiResponse<MigrationGetResponse>) => {
|
|
const sequelize = new Sequelize({ dialect: 'sqlite', storage: './data/database.sqlite', logging: false });
|
|
const umzug = new Umzug({
|
|
migrations: { glob: 'database/migrations/*.js' },
|
|
context: sequelize.getQueryInterface(),
|
|
storage: new SequelizeStorage({ sequelize }),
|
|
logger: undefined,
|
|
});
|
|
const migrations = await umzug.pending();
|
|
// console.log('migrations :', migrations);
|
|
// const migrationsExceuted = await umzug.executed();
|
|
return res.status(200).json({ hasMigrations: migrations.length > 0 });
|
|
};
|
|
|
|
const migrateDatabase = async (req: NextApiRequest, res: NextApiResponse<MigrationPostResponse>) => {
|
|
const sequelize = new Sequelize({ dialect: 'sqlite', storage: './data/database.sqlite', logging: false });
|
|
const umzug = new Umzug({
|
|
migrations: { glob: 'database/migrations/*.js' },
|
|
context: sequelize.getQueryInterface(),
|
|
storage: new SequelizeStorage({ sequelize }),
|
|
logger: undefined,
|
|
});
|
|
const migrations = await umzug.up();
|
|
console.log('[Updated] migrations :', migrations);
|
|
return res.status(200).json({ migrated: true });
|
|
};
|