mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-05-10 04:01:37 +02:00
Setup and migrate to Prisma (#456)
This commit is contained in:
parent
129d121364
commit
51d378e4cb
30 changed files with 605 additions and 273 deletions
83
src/scripts/migrate-and-start.ts
Normal file
83
src/scripts/migrate-and-start.ts
Normal file
|
@ -0,0 +1,83 @@
|
|||
// This script applies Prisma migrations
|
||||
// and then starts Muse.
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
import {execa, ExecaError} from 'execa';
|
||||
import {promises as fs} from 'fs';
|
||||
import Prisma from '@prisma/client';
|
||||
import ora from 'ora';
|
||||
import {startBot} from '../index.js';
|
||||
import logBanner from '../utils/log-banner.js';
|
||||
import {createDatabasePath} from '../utils/create-database-url.js';
|
||||
import {DATA_DIR} from '../services/config.js';
|
||||
|
||||
const client = new Prisma.PrismaClient();
|
||||
|
||||
const migrateFromSequelizeToPrisma = async () => {
|
||||
await execa('prisma', ['migrate', 'resolve', '--applied', '20220101155430_migrate_from_sequelize'], {preferLocal: true});
|
||||
};
|
||||
|
||||
const doesUserHaveExistingDatabase = async () => {
|
||||
try {
|
||||
await fs.access(createDatabasePath(DATA_DIR));
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const hasDatabaseBeenMigratedToPrisma = async () => {
|
||||
try {
|
||||
await client.$queryRaw`SELECT COUNT(id) FROM _prisma_migrations`;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Prisma.Prisma.PrismaClientKnownRequestError && error.code === 'P2010') {
|
||||
// Table doesn't exist
|
||||
return false;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
(async () => {
|
||||
// Banner
|
||||
logBanner();
|
||||
|
||||
const spinner = ora('Applying database migrations...').start();
|
||||
|
||||
if (await doesUserHaveExistingDatabase()) {
|
||||
if (!(await hasDatabaseBeenMigratedToPrisma())) {
|
||||
try {
|
||||
await migrateFromSequelizeToPrisma();
|
||||
} catch (error) {
|
||||
if ((error as ExecaError).stderr) {
|
||||
spinner.fail('Failed to apply database migrations (going from Sequelize to Prisma):');
|
||||
console.error((error as ExecaError).stderr);
|
||||
process.exit(1);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await execa('prisma', ['migrate', 'deploy'], {preferLocal: true});
|
||||
} catch (error: unknown) {
|
||||
if ((error as ExecaError).stderr) {
|
||||
spinner.fail('Failed to apply database migrations:');
|
||||
console.error((error as ExecaError).stderr);
|
||||
process.exit(1);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
spinner.succeed('Database migrations applied.');
|
||||
|
||||
await startBot();
|
||||
})();
|
13
src/scripts/run-with-database-url.ts
Normal file
13
src/scripts/run-with-database-url.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import {DATA_DIR} from '../services/config.js';
|
||||
import createDatabaseUrl from '../utils/create-database-url.js';
|
||||
import {execa} from 'execa';
|
||||
|
||||
process.env.DATABASE_URL = createDatabaseUrl(DATA_DIR);
|
||||
|
||||
(async () => {
|
||||
await execa(process.argv[2], process.argv.slice(3), {
|
||||
preferLocal: true,
|
||||
stderr: process.stderr,
|
||||
stdout: process.stdout,
|
||||
});
|
||||
})();
|
9
src/scripts/start.ts
Normal file
9
src/scripts/start.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
// This script is mainly used during development.
|
||||
// Starts Muse without applying database migrations.
|
||||
import {startBot} from '../index.js';
|
||||
import logBanner from '../utils/log-banner.js';
|
||||
|
||||
(async () => {
|
||||
logBanner();
|
||||
await startBot();
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue