umami/scripts/check-db.js

106 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-10-03 20:14:19 +02:00
/* eslint-disable no-console */
2022-06-19 09:07:01 +02:00
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
2022-06-22 10:50:33 +02:00
const chalk = require('chalk');
2022-06-28 07:09:47 +02:00
const { execSync } = require('child_process');
const semver = require('semver');
2022-06-19 09:07:01 +02:00
2022-08-25 09:49:47 +02:00
if (process.env.SKIP_DB_CHECK) {
console.log('Skipping database check.');
process.exit(0);
}
function getDatabaseType(url = process.env.DATABASE_URL) {
2023-04-27 15:38:07 +02:00
const type = url && url.split(':')[0];
if (type === 'postgres') {
return 'postgresql';
}
return type;
}
2022-06-28 08:08:58 +02:00
const prisma = new PrismaClient();
2022-06-22 10:50:33 +02:00
function success(msg) {
console.log(chalk.greenBright(`${msg}`));
}
2022-08-05 22:07:20 +02:00
function error(msg) {
console.log(chalk.redBright(`${msg}`));
}
2022-06-22 10:50:33 +02:00
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.');
}
2022-06-19 09:07:01 +02:00
}
2023-04-27 15:38:07 +02:00
async function checkDatabaseVersion() {
2023-04-18 16:17:34 +02:00
const query = await prisma.$queryRaw`select version() as version`;
const version = semver.valid(semver.coerce(query[0].version));
2022-06-22 10:50:33 +02:00
2023-04-27 15:38:07 +02:00
const databaseType = getDatabaseType();
2023-04-18 20:27:55 +02:00
const minVersion = databaseType === 'postgresql' ? '9.4.0' : '5.7.0';
if (semver.lt(version, minVersion)) {
throw new Error(
2023-01-23 19:06:52 +01:00
`Database version is not compatible. Please upgrade ${databaseType} version to ${minVersion} or greater`,
);
2022-06-22 10:50:33 +02:00
}
success('Database version check successful.');
2022-06-22 10:50:33 +02:00
}
async function checkV1Tables() {
try {
// check for v1 migrations before v2 release date
const record =
await prisma.$queryRaw`select * from _prisma_migrations where started_at < '2023-04-17'`;
if (record.length > 0) {
error(
'Umami v1 tables detected. For how to upgrade from v1 to v2 go to https://umami.is/docs/migrate-v1-v2.',
);
process.exit(1);
}
} catch (e) {
// Ignore
2022-06-22 10:50:33 +02:00
}
}
async function applyMigration() {
console.log(execSync('prisma migrate deploy').toString());
2022-06-22 10:50:33 +02:00
success('Database is up to date.');
}
(async () => {
let err = false;
for (let fn of [checkEnv, checkConnection, checkDatabaseVersion, checkV1Tables, applyMigration]) {
2022-06-22 10:50:33 +02:00
try {
2023-04-27 15:38:07 +02:00
await fn();
2022-06-22 10:50:33 +02:00
} catch (e) {
error(e.message);
2022-06-22 10:50:33 +02:00
err = true;
} finally {
await prisma.$disconnect();
if (err) {
process.exit(1);
}
}
}
})();