1
0
mirror of https://github.com/kremalicious/umami.git synced 2025-02-14 21:10:34 +01:00

add logic for database type

This commit is contained in:
Francis Cao 2023-01-11 16:19:20 -08:00
parent ac2b79c5cf
commit 96de650a67

@ -9,17 +9,17 @@ const prompts = require('prompts');
const prisma = new PrismaClient(); const prisma = new PrismaClient();
// function getDatabaseType(url = process.env.DATABASE_URL) { function getDatabaseType(url = process.env.DATABASE_URL) {
// const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]); const type = process.env.DATABASE_TYPE || (url && url.split(':')[0]);
// if (type === 'postgres') { if (type === 'postgres') {
// return 'postgresql'; return 'postgresql';
// } }
// return type; return type;
// } }
// const databaseType = getDatabaseType(); const databaseType = getDatabaseType();
function success(msg) { function success(msg) {
console.log(chalk.greenBright(`${msg}`)); console.log(chalk.greenBright(`${msg}`));
@ -47,7 +47,7 @@ async function checkConnection() {
} }
} }
async function checkV1Tables() { async function checkV1Tables(databaseType) {
const updateV1 = const updateV1 =
await prisma.$queryRaw`select * from _prisma_migrations where migration_name = '04_add_uuid' and finished_at IS NOT NULL`; await prisma.$queryRaw`select * from _prisma_migrations where migration_name = '04_add_uuid' and finished_at IS NOT NULL`;
@ -55,9 +55,12 @@ async function checkV1Tables() {
console.log('Preparing v1 tables for migration'); console.log('Preparing v1 tables for migration');
// alter v1 tables // alter v1 tables
if (databaseType === 'postgresql') {
await dropV1Keys(); await dropV1Keys();
await renameV1Tables(); }
await dropV1Indexes();
await renameV1Tables(databaseType);
await dropV1Indexes(databaseType);
} }
// check for V1 renamed tables // check for V1 renamed tables
@ -111,9 +114,10 @@ async function dropV1Keys() {
} }
} }
async function renameV1Tables() { async function renameV1Tables(databaseType) {
try { try {
// rename tables // rename tables
if (databaseType === 'postgresql') {
await prisma.$transaction([ await prisma.$transaction([
prisma.$executeRaw`ALTER TABLE IF EXISTS "_prisma_migrations" RENAME TO "v1_prisma_migrations";`, 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 "account" RENAME TO "v1_account";`,
@ -123,6 +127,17 @@ async function renameV1Tables() {
prisma.$executeRaw`ALTER TABLE IF EXISTS "session" RENAME TO "v1_session";`, prisma.$executeRaw`ALTER TABLE IF EXISTS "session" RENAME TO "v1_session";`,
prisma.$executeRaw`ALTER TABLE IF EXISTS "website" RENAME TO "v1_website";`, 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;`,
]);
}
success('Renamed v1 database tables.'); success('Renamed v1 database tables.');
} catch (e) { } catch (e) {
@ -131,12 +146,11 @@ async function renameV1Tables() {
} }
} }
async function dropV1Indexes() { async function dropV1Indexes(databaseType) {
try { try {
// drop indexes // drop indexes
if (databaseType === 'postgresql') {
await prisma.$transaction([ await prisma.$transaction([
prisma.$executeRaw`DROP INDEX IF EXISTS "user_user_id_key";`,
prisma.$executeRaw`DROP INDEX IF EXISTS "user_username_key";`,
prisma.$executeRaw`DROP INDEX IF EXISTS "session_session_id_key";`, 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_created_at_idx";`,
prisma.$executeRaw`DROP INDEX IF EXISTS "session_website_id_idx";`, prisma.$executeRaw`DROP INDEX IF EXISTS "session_website_id_idx";`,
@ -145,8 +159,16 @@ async function dropV1Indexes() {
prisma.$executeRaw`DROP INDEX IF EXISTS "website_created_at_idx";`, 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_share_id_idx";`,
prisma.$executeRaw`DROP INDEX IF EXISTS "website_user_id_idx";`, prisma.$executeRaw`DROP INDEX IF EXISTS "website_user_id_idx";`,
prisma.$executeRaw`DROP INDEX IF EXISTS "website_user_id_idx2";`,
]); ]);
} else {
await prisma.$transaction([
prisma.$executeRaw`DROP INDEX session_session_uuid_key ON session;`,
prisma.$executeRaw`DROP INDEX session_created_at_idx ON session;`,
prisma.$executeRaw`DROP INDEX session_website_id_idx ON session;`,
prisma.$executeRaw`DROP INDEX website_share_id_key ON website;`,
prisma.$executeRaw`DROP INDEX website_website_uuid_key ON website;`,
]);
}
success('Dropped v1 database indexes.'); success('Dropped v1 database indexes.');
} catch (e) { } catch (e) {
@ -226,7 +248,7 @@ async function runSqlFile(filePath) {
deleteV1TablesPrompt, deleteV1TablesPrompt,
]) { ]) {
try { try {
await fn(); fn.name === 'checkV1Tables' ? await fn(databaseType) : await fn();
} catch (e) { } catch (e) {
console.log(chalk.red(`${e.message}`)); console.log(chalk.red(`${e.message}`));
err = true; err = true;