blob: 41197e47ad60fccd08e3c857203d019d2f675e00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// 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 createDatabaseUrl, {createDatabasePath} from '../utils/create-database-url.js';
import {DATA_DIR} from '../services/config.js';
const client = new Prisma.PrismaClient();
process.env.DATABASE_URL = process.env.DATABASE_URL ?? createDatabaseUrl(DATA_DIR);
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();
})();
|