mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
Added database check.
This commit is contained in:
parent
3122bab419
commit
ac3017b2e4
6 changed files with 104 additions and 40 deletions
|
|
@ -1,16 +1,94 @@
|
|||
require('dotenv').config();
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const prisma = new PrismaClient();
|
||||
const chalk = require('chalk');
|
||||
const spawn = require('cross-spawn');
|
||||
|
||||
async function check() {
|
||||
await prisma.account.findMany({ limit: 1 });
|
||||
let message = '';
|
||||
|
||||
function success(msg) {
|
||||
console.log(chalk.greenBright(`✓ ${msg}`));
|
||||
}
|
||||
|
||||
check()
|
||||
.catch(e => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
async function checkEnv() {
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error('DATABASE_URL is not defined.');
|
||||
} else {
|
||||
success('DATABASE_URL is defined.');
|
||||
}
|
||||
}
|
||||
|
||||
async function checkConnection() {
|
||||
try {
|
||||
await prisma.$connect();
|
||||
|
||||
success('Database connection successful.');
|
||||
} catch (e) {
|
||||
throw new Error('Unable to connect to the database.');
|
||||
}
|
||||
}
|
||||
|
||||
async function checkTables() {
|
||||
try {
|
||||
await prisma.account.findFirst();
|
||||
|
||||
success('Database tables found.');
|
||||
} catch (e) {
|
||||
message = `To update your database, you need to run:\n${chalk.bold.whiteBright(
|
||||
'yarn update-db',
|
||||
)}`;
|
||||
throw new Error('Database tables not found.');
|
||||
}
|
||||
}
|
||||
|
||||
async function run(cmd, args) {
|
||||
const buffer = [];
|
||||
const proc = spawn(cmd, args);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
proc.stdout.on('data', data => buffer.push(data));
|
||||
|
||||
proc.on('error', () => {
|
||||
reject(new Error('Failed to run Prisma.'));
|
||||
});
|
||||
|
||||
proc.on('exit', () => resolve(buffer.join('')));
|
||||
});
|
||||
}
|
||||
|
||||
async function checkMigrations() {
|
||||
const output = await run('prisma', ['migrate', 'status']);
|
||||
|
||||
const missingMigrations = output.includes('Following migration have not yet been applied');
|
||||
const notManaged = output.includes('The current database is not managed');
|
||||
|
||||
if (notManaged) {
|
||||
const cmd = output.match(/yarn prisma migrate resolve --applied ".*"/g);
|
||||
|
||||
message = `You need to update your database by running:\n${chalk.bold.whiteBright(cmd[0])}`;
|
||||
throw new Error('Database is out of date.');
|
||||
} else if (missingMigrations) {
|
||||
message = output;
|
||||
throw new Error('Database is out of date.');
|
||||
}
|
||||
|
||||
success('Database is up to date.');
|
||||
}
|
||||
|
||||
(async () => {
|
||||
let err = false;
|
||||
for (let fn of [checkEnv, checkConnection, checkTables, checkMigrations]) {
|
||||
try {
|
||||
await fn();
|
||||
} catch (e) {
|
||||
console.log(chalk.red(`✗ ${e.message}`));
|
||||
err = true;
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
if (err) {
|
||||
console.log(message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ require('dotenv').config();
|
|||
const fse = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
function getDatabase() {
|
||||
function getDatabaseType() {
|
||||
const type =
|
||||
process.env.DATABASE_TYPE ||
|
||||
(process.env.DATABASE_URL && process.env.DATABASE_URL.split(':')[0]);
|
||||
|
|
@ -14,7 +14,7 @@ function getDatabase() {
|
|||
return type;
|
||||
}
|
||||
|
||||
const databaseType = getDatabase();
|
||||
const databaseType = getDatabaseType();
|
||||
|
||||
if (!databaseType || !['mysql', 'postgresql'].includes(databaseType)) {
|
||||
throw new Error('Missing or invalid database');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue