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-04 19:07:58 +01:00
|
|
|
const prompts = require('prompts');
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
function getDatabaseType(url = process.env.DATABASE_URL) {
|
|
|
|
const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
|
2023-01-11 01:03:44 +01:00
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
if (type === 'postgres') {
|
|
|
|
return 'postgresql';
|
|
|
|
}
|
2023-01-11 01:03:44 +01:00
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
return type;
|
|
|
|
}
|
2023-01-11 01:03:44 +01:00
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
const databaseType = getDatabaseType();
|
2023-01-11 01:03:44 +01:00
|
|
|
|
2023-01-03 01:34:58 +01:00
|
|
|
function success(msg) {
|
|
|
|
console.log(chalk.greenBright(`✓ ${msg}`));
|
|
|
|
}
|
|
|
|
|
2023-01-11 01:03:44 +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-12 01:19:20 +01:00
|
|
|
async function checkV1Tables(databaseType) {
|
2023-01-11 00:01:41 +01:00
|
|
|
const updateV1 =
|
|
|
|
await prisma.$queryRaw`select * from _prisma_migrations where migration_name = '04_add_uuid' and finished_at IS NOT NULL`;
|
|
|
|
|
|
|
|
if (updateV1.length > 0) {
|
2023-01-03 21:50:08 +01:00
|
|
|
console.log('Preparing v1 tables for migration');
|
|
|
|
|
|
|
|
// alter v1 tables
|
2023-01-12 01:19:20 +01:00
|
|
|
if (databaseType === 'postgresql') {
|
|
|
|
await dropV1Keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
await dropV1Indexes(databaseType);
|
2023-01-13 20:54:22 +01:00
|
|
|
await renameV1Tables(databaseType);
|
2023-01-11 00:01:41 +01:00
|
|
|
}
|
2023-01-10 18:54:16 +01:00
|
|
|
|
2023-01-11 00:01:41 +01:00
|
|
|
// check for V1 renamed tables
|
|
|
|
try {
|
|
|
|
await prisma.$queryRaw`select * from v1_account limit 1`;
|
|
|
|
|
|
|
|
success('Database v1 tables ready for migration.');
|
2023-01-03 01:34:58 +01:00
|
|
|
} catch (e) {
|
2023-01-11 00:01:41 +01:00
|
|
|
throw new Error('Database v1 tables not found.');
|
2023-01-03 21:50:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkV2Tables() {
|
|
|
|
try {
|
|
|
|
await prisma.$queryRaw`select * from website_event limit 1`;
|
|
|
|
|
|
|
|
success('Database v2 tables found.');
|
|
|
|
} catch (e) {
|
2023-01-10 18:54:16 +01:00
|
|
|
console.log('Database v2 tables not found.');
|
2023-01-03 21:50:08 +01:00
|
|
|
console.log('Adding v2 tables...');
|
|
|
|
|
|
|
|
// run v2 prisma migration steps
|
2023-01-03 22:16:29 +01:00
|
|
|
await runSqlFile('../prisma/migrations/01_init/migration.sql');
|
2023-01-03 21:50:08 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 01:03:44 +01:00
|
|
|
async function migrateData() {
|
|
|
|
console.log('Starting v2 data migration. Please do no cancel this process, it may take a while.');
|
|
|
|
await runSqlFile('../db/postgresql/migration_v2.sql');
|
|
|
|
|
|
|
|
success('Data migration from V1 to V2 tables completed.');
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:07:58 +01:00
|
|
|
async function dropV1Keys() {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
|
|
|
// drop keys
|
2023-01-09 22:14:30 +01:00
|
|
|
await prisma.$transaction([
|
2023-01-10 18:54:16 +01:00
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "_prisma_migrations" DROP CONSTRAINT IF EXISTS "_prisma_migrations_pkey" cascade;`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "account" DROP CONSTRAINT IF EXISTS "account_pkey" cascade;`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "event" DROP CONSTRAINT IF EXISTS "event_pkey" cascade;`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "session" DROP CONSTRAINT IF EXISTS "session_pkey" cascade;`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "website" DROP CONSTRAINT IF EXISTS "website_pkey" cascade;`,
|
2023-01-09 22:14:30 +01:00
|
|
|
]);
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
success('Dropped v1 database keys.');
|
|
|
|
} catch (e) {
|
2023-01-11 00:01:41 +01:00
|
|
|
console.log(e);
|
|
|
|
throw new Error('Failed to drop v1 database keys.');
|
2023-01-03 01:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
async function renameV1Tables(databaseType) {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
|
|
|
// rename tables
|
2023-01-12 01:19:20 +01:00
|
|
|
if (databaseType === 'postgresql') {
|
|
|
|
await prisma.$transaction([
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "_prisma_migrations" RENAME TO "v1_prisma_migrations";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "account" RENAME TO "v1_account";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "event" RENAME TO "v1_event";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "event_data" RENAME TO "v1_event_data";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "pageview" RENAME TO "v1_pageview";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "session" RENAME TO "v1_session";`,
|
|
|
|
prisma.$executeRaw`ALTER TABLE IF EXISTS "website" RENAME TO "v1_website";`,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
await prisma.$transaction([
|
|
|
|
prisma.$executeRaw`RENAME TABLE _prisma_migrations TO v1_prisma_migrations;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE account TO v1_account;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE event TO v1_event;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE event_data TO v1_event_data;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE pageview TO v1_pageview;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE session TO v1_session;`,
|
|
|
|
prisma.$executeRaw`RENAME TABLE website TO v1_website;`,
|
|
|
|
]);
|
|
|
|
}
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
success('Renamed v1 database tables.');
|
|
|
|
} catch (e) {
|
2023-01-11 00:01:41 +01:00
|
|
|
console.log(e);
|
|
|
|
throw new Error('Failed to rename v1 database tables.');
|
2023-01-03 01:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:19:20 +01:00
|
|
|
async function dropV1Indexes(databaseType) {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
|
|
|
// drop indexes
|
2023-01-12 01:19:20 +01:00
|
|
|
if (databaseType === 'postgresql') {
|
|
|
|
await prisma.$transaction([
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "session_session_id_key";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "session_created_at_idx";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "session_website_id_idx";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "website_website_id_key";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "website_share_id_key";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "website_created_at_idx";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "website_share_id_idx";`,
|
|
|
|
prisma.$executeRaw`DROP INDEX IF EXISTS "website_user_id_idx";`,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
await prisma.$transaction([
|
2023-01-13 20:54:22 +01:00
|
|
|
prisma.$executeRaw`ALTER TABLE session DROP FOREIGN KEY session_website_id_fkey;`,
|
2023-01-12 01:19:20 +01:00
|
|
|
prisma.$executeRaw`DROP INDEX session_created_at_idx ON session;`,
|
|
|
|
prisma.$executeRaw`DROP INDEX session_website_id_idx ON session;`,
|
2023-01-13 20:54:22 +01:00
|
|
|
prisma.$executeRaw`ALTER TABLE website DROP FOREIGN KEY website_user_id_fkey;`,
|
|
|
|
prisma.$executeRaw`DROP INDEX website_user_id_idx ON website;`,
|
2023-01-12 01:19:20 +01:00
|
|
|
prisma.$executeRaw`DROP INDEX website_share_id_key ON website;`,
|
|
|
|
]);
|
|
|
|
}
|
2023-01-03 01:34:58 +01:00
|
|
|
|
|
|
|
success('Dropped v1 database indexes.');
|
|
|
|
} catch (e) {
|
2023-01-11 00:01:41 +01:00
|
|
|
console.log(e);
|
|
|
|
throw new Error('Failed to drop v1 database indexes.');
|
2023-01-03 01:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:07:58 +01:00
|
|
|
async function deleteV1TablesPrompt() {
|
|
|
|
const response = await prompts({
|
|
|
|
type: 'text',
|
|
|
|
name: 'value',
|
|
|
|
message: 'Do you want to delete V1 database tables? (Y/N)',
|
2023-01-11 00:01:41 +01:00
|
|
|
validate: value =>
|
|
|
|
value.toUpperCase() !== 'Y' && value.toUpperCase() !== 'N' ? `Please enter Y or N.` : true,
|
2023-01-04 19:07:58 +01:00
|
|
|
});
|
|
|
|
|
2023-01-11 00:01:41 +01:00
|
|
|
if (response.value.toUpperCase() == 'Y') {
|
2023-01-04 19:07:58 +01:00
|
|
|
await deleteV1Tables();
|
|
|
|
}
|
2023-01-04 20:15:19 +01:00
|
|
|
|
|
|
|
success('Migration successfully completed.');
|
2023-01-04 19:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteV1Tables() {
|
|
|
|
try {
|
|
|
|
// drop tables
|
2023-01-09 22:14:30 +01:00
|
|
|
await prisma.$transaction([
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_prisma_migrations";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_account";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_event";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_event_data";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_pageview";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_session";`,
|
|
|
|
prisma.$executeRaw`DROP TABLE IF EXISTS "v1_website";`,
|
|
|
|
]);
|
2023-01-04 19:07:58 +01:00
|
|
|
|
|
|
|
success('Dropped v1 database tables.');
|
|
|
|
} catch (e) {
|
2023-01-11 01:03:44 +01:00
|
|
|
throw new Error('Failed to drop v1 database tables.');
|
2023-01-04 19:07:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:16:29 +01:00
|
|
|
async function runSqlFile(filePath) {
|
2023-01-03 18:32:32 +01:00
|
|
|
try {
|
2023-01-03 22:16:29 +01:00
|
|
|
const rawSql = await fs.promises.readFile(path.join(__dirname, filePath));
|
2023-01-03 21:50:08 +01:00
|
|
|
|
|
|
|
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) {
|
2023-01-13 20:54:22 +01:00
|
|
|
if (sql.length > 0) {
|
|
|
|
await prisma.$executeRawUnsafe(sql);
|
|
|
|
}
|
2023-01-03 21:50:08 +01:00
|
|
|
}
|
2023-01-03 22:16:29 +01:00
|
|
|
filePath;
|
2023-01-03 21:50:08 +01:00
|
|
|
|
2023-01-03 22:16:29 +01:00
|
|
|
success(`Ran sql file ${filePath}.`);
|
2023-01-03 18:32:32 +01:00
|
|
|
} catch (e) {
|
2023-01-04 20:03:28 +01:00
|
|
|
console.log(e);
|
2023-01-03 22:16:29 +01:00
|
|
|
throw new Error(`Failed to run sql file ${filePath}.`);
|
2023-01-03 18:32:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 01:34:58 +01:00
|
|
|
(async () => {
|
|
|
|
let err = false;
|
2023-01-11 01:03:44 +01:00
|
|
|
for (let fn of [
|
|
|
|
checkEnv,
|
|
|
|
checkConnection,
|
|
|
|
checkV1Tables,
|
|
|
|
checkV2Tables,
|
|
|
|
migrateData,
|
|
|
|
deleteV1TablesPrompt,
|
|
|
|
]) {
|
2023-01-03 01:34:58 +01:00
|
|
|
try {
|
2023-01-12 01:19:20 +01:00
|
|
|
fn.name === 'checkV1Tables' ? await fn(databaseType) : await fn();
|
2023-01-03 01:34:58 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(`✗ ${e.message}`));
|
|
|
|
err = true;
|
|
|
|
} finally {
|
|
|
|
await prisma.$disconnect();
|
|
|
|
if (err) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|