mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 09:45:04 +01:00
102 lines
2.4 KiB
JavaScript
102 lines
2.4 KiB
JavaScript
/* eslint-disable no-console */
|
|
require('dotenv').config();
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const chalk = require('chalk');
|
|
const { execSync } = require('child_process');
|
|
const semver = require('semver');
|
|
|
|
if (process.env.SKIP_DB_CHECK) {
|
|
console.log('Skipping database check.');
|
|
process.exit(0);
|
|
}
|
|
|
|
function getDatabaseType(url = process.env.DATABASE_URL) {
|
|
const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
|
|
|
|
if (type === 'postgres') {
|
|
return 'postgresql';
|
|
}
|
|
|
|
return type;
|
|
}
|
|
|
|
const databaseType = getDatabaseType();
|
|
const prisma = new PrismaClient();
|
|
|
|
function success(msg) {
|
|
console.log(chalk.greenBright(`✓ ${msg}`));
|
|
}
|
|
|
|
function error(msg) {
|
|
console.log(chalk.redBright(`✗ ${msg}`));
|
|
}
|
|
|
|
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 checkDatabaseVersion(databaseType) {
|
|
const query = await prisma.$queryRaw`select version() version`;
|
|
const version = semver.valid(semver.coerce(query[0].version));
|
|
|
|
const minVersion = databaseType === 'postgresql' ? '9.4.0' : '5.6.0';
|
|
|
|
if (semver.lt(version, minVersion)) {
|
|
throw new Error(
|
|
`Database version is not compatible. Please upgrade ${databaseType} version to ${minVersion} or greater`,
|
|
);
|
|
}
|
|
|
|
success('Database version check successful.');
|
|
}
|
|
|
|
async function checkV1Tables() {
|
|
try {
|
|
await prisma.$queryRaw`select * from account limit 1`;
|
|
|
|
console.log('Umami v1 tables detected. To upgrade from v1 to v2 run');
|
|
console.log('npx @umami/migrate-v1-v2');
|
|
|
|
process.exit(0);
|
|
} catch (e) {
|
|
// Ignore
|
|
}
|
|
}
|
|
|
|
async function applyMigration() {
|
|
console.log(execSync('prisma migrate deploy').toString());
|
|
|
|
success('Database is up to date.');
|
|
}
|
|
|
|
(async () => {
|
|
let err = false;
|
|
for (let fn of [checkEnv, checkConnection, checkDatabaseVersion, checkV1Tables, applyMigration]) {
|
|
try {
|
|
fn.name === 'checkDatabaseVersion' ? await fn(databaseType) : await fn();
|
|
} catch (e) {
|
|
error(e.message);
|
|
err = true;
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
if (err) {
|
|
process.exit(1);
|
|
}
|
|
}
|
|
}
|
|
})();
|