2023-01-03 01:34:58 +01:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
require('dotenv').config();
|
2023-01-03 21:50:08 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2023-01-03 01:34:58 +01:00
|
|
|
const { PrismaClient } = require('@prisma/client');
|
|
|
|
const chalk = require('chalk');
|
2023-01-03 21:50:08 +01:00
|
|
|
const { execSync } = require('child_process');
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
|
|
function success(msg) {
|
|
|
|
console.log(chalk.greenBright(`✓ ${msg}`));
|
|
|
|
}
|
|
|
|
|
2023-01-03 21:50:08 +01:00
|
|
|
function error(msg) {
|
|
|
|
console.log(chalk.redBright(`✗ ${msg}`));
|
|
|
|
}
|
2023-01-03 01:34:58 +01: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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 21:50:08 +01:00
|
|
|
async function checkV1Tables() {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
2023-01-03 18:32:32 +01:00
|
|
|
await prisma.$queryRaw`select * from account limit 1`;
|
2023-01-03 01:34:58 +01:00
|
|
|
|
2023-01-03 21:50:08 +01:00
|
|
|
success('Database v1 tables found.');
|
|
|
|
console.log('Preparing v1 tables for migration');
|
|
|
|
|
|
|
|
// alter v1 tables
|
|
|
|
await dropKeys();
|
|
|
|
await renameTables();
|
|
|
|
await dropIndexes();
|
2023-01-03 01:34:58 +01:00
|
|
|
} catch (e) {
|
2023-01-03 21:50:08 +01:00
|
|
|
error('Database v1 tables not found.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkV2Tables() {
|
|
|
|
try {
|
|
|
|
await prisma.$queryRaw`select * from website_event limit 1`;
|
|
|
|
|
|
|
|
success('Database v2 tables found.');
|
|
|
|
} catch (e) {
|
|
|
|
error('Database v2 tables not found.');
|
|
|
|
console.log('Adding v2 tables...');
|
|
|
|
|
|
|
|
// run v2 prisma migration steps
|
|
|
|
await runInitMigration();
|
|
|
|
console.log(execSync('prisma migrate resolve --applied 01_init').toString());
|
|
|
|
console.log(execSync('prisma migrate deploy').toString());
|
2023-01-03 01:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dropKeys() {
|
|
|
|
try {
|
|
|
|
// drop keys
|
2023-01-03 01:47:48 +01:00
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "_prisma_migrations" DROP CONSTRAINT IF EXISTS "_prisma_migrations_pkey" cascade;`;
|
2023-01-03 01:34:58 +01:00
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "account" DROP CONSTRAINT IF EXISTS "account_pkey" cascade;`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "event" DROP CONSTRAINT IF EXISTS "event_pkey" cascade;`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "session" DROP CONSTRAINT IF EXISTS "session_pkey" cascade;`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "website" DROP CONSTRAINT IF EXISTS "website_pkey" cascade;`;
|
|
|
|
|
|
|
|
success('Dropped v1 database keys.');
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Failed to drop v1 database keys.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renameTables() {
|
|
|
|
try {
|
|
|
|
// rename tables
|
2023-01-03 01:58:52 +01:00
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "_prisma_migrations" RENAME TO "v1_prisma_migrations";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "account" RENAME TO "v1_account";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "event" RENAME TO "v1_event";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "event_data" RENAME TO "v1_event_data";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "pageview" RENAME TO "v1_pageview";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "session" RENAME TO "v1_session";`;
|
|
|
|
await prisma.$executeRaw`ALTER TABLE IF EXISTS "website" RENAME TO "v1_website";`;
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
success('Renamed v1 database tables.');
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Failed to rename v1 database tables.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function dropIndexes() {
|
|
|
|
try {
|
|
|
|
// drop indexes
|
2023-01-03 01:53:20 +01:00
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "user_user_id_key";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "user_username_key";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "session_session_id_key";`;
|
2023-01-03 01:34:58 +01:00
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "session_created_at_idx";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "session_website_id_idx";`;
|
2023-01-03 01:53:20 +01:00
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "website_website_id_key";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "website_share_id_key";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "website_created_at_idx";`;
|
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "website_share_id_idx";`;
|
2023-01-03 21:50:08 +01:00
|
|
|
await prisma.$executeRaw`DROP INDEX IF EXISTS "website_user_id_idx";`;
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
success('Dropped v1 database indexes.');
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Failed to drop v1 database indexes.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 21:50:08 +01:00
|
|
|
async function runInitMigration() {
|
2023-01-03 18:32:32 +01:00
|
|
|
try {
|
2023-01-03 21:50:08 +01:00
|
|
|
const rawSql = await fs.promises.readFile(
|
|
|
|
path.join(__dirname, '../prisma/migrations/01_init/migration.sql'),
|
|
|
|
);
|
|
|
|
|
|
|
|
const sqlStatements = rawSql
|
|
|
|
.toString()
|
|
|
|
.split('\n')
|
|
|
|
.filter(line => !line.startsWith('--')) // remove comments-only lines
|
|
|
|
.join('\n')
|
|
|
|
.replace(/\r\n|\n|\r/g, ' ') // remove newlines
|
|
|
|
.replace(/\s+/g, ' ') // excess white space
|
|
|
|
.split(';');
|
|
|
|
|
|
|
|
for (const sql of sqlStatements) {
|
|
|
|
await prisma.$executeRawUnsafe(sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
success('Ran 01_init migration.');
|
2023-01-03 18:32:32 +01:00
|
|
|
} catch (e) {
|
2023-01-03 21:50:08 +01:00
|
|
|
console.error(e);
|
|
|
|
throw new Error('Failed to run 01_init migration.');
|
2023-01-03 18:32:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 01:34:58 +01:00
|
|
|
(async () => {
|
|
|
|
let err = false;
|
2023-01-03 21:50:08 +01:00
|
|
|
for (let fn of [checkEnv, checkConnection, checkV1Tables, checkV2Tables]) {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(`✗ ${e.message}`));
|
|
|
|
err = true;
|
|
|
|
} finally {
|
|
|
|
await prisma.$disconnect();
|
|
|
|
if (err) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|