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');
|
2023-01-20 23:21:11 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
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();
|
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-01-20 23:21:11 +01:00
|
|
|
async function checkDatabaseVersion(databaseType) {
|
|
|
|
const query = await prisma.$queryRaw`select version() version`;
|
|
|
|
const version = semver.valid(semver.coerce(query[0].version));
|
2022-06-22 10:50:33 +02:00
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
const minVersion = databaseType === 'postgresql' ? '9.4.0' : '5.6.0';
|
2022-06-22 21:12:22 +02:00
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
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`,
|
2023-01-20 23:21:11 +01:00
|
|
|
);
|
2022-06-22 10:50:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
success('Database version check successful.');
|
2022-06-22 10:50:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
async function checkV1Tables() {
|
|
|
|
try {
|
|
|
|
await prisma.$queryRaw`select * from account limit 1`;
|
2022-06-22 10:50:33 +02:00
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
console.log('Umami v1 tables detected. To upgrade from v1 to v2 run');
|
|
|
|
console.log('npx @umami/migrate-v1-v2');
|
2022-06-22 10:50:33 +02:00
|
|
|
|
2023-01-20 23:21:11 +01:00
|
|
|
process.exit(0);
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
2022-06-22 10:50:33 +02:00
|
|
|
}
|
2023-01-20 23:21:11 +01: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;
|
2023-01-20 23:21:11 +01:00
|
|
|
for (let fn of [checkEnv, checkConnection, checkDatabaseVersion, checkV1Tables, applyMigration]) {
|
2022-06-22 10:50:33 +02:00
|
|
|
try {
|
2023-01-20 23:21:11 +01:00
|
|
|
fn.name === 'checkDatabaseVersion' ? await fn(databaseType) : await fn();
|
2022-06-22 10:50:33 +02:00
|
|
|
} catch (e) {
|
2023-01-20 23:21:11 +01:00
|
|
|
error(e.message);
|
2022-06-22 10:50:33 +02:00
|
|
|
err = true;
|
|
|
|
} finally {
|
|
|
|
await prisma.$disconnect();
|
|
|
|
if (err) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|