update checkV1Tables check

This commit is contained in:
Francis Cao 2023-01-10 15:01:41 -08:00
parent 9cc0588175
commit b05b18e736
2 changed files with 30 additions and 27 deletions

View File

@ -11,7 +11,8 @@ SELECT account_uuid,
created_at,
updated_at,
NULL
FROM v1_account;
FROM v1_account
WHERE NOT EXISTS (SELECT 1 FROM "user");
-- website
INSERT INTO website
@ -26,7 +27,8 @@ SELECT website_uuid,
a.created_at
FROM v1_website w
JOIN v1_account a
ON a.user_id = w.user_id;
ON a.user_id = w.user_id
WHERE NOT EXISTS (SELECT 1 FROM website);
-- session
INSERT INTO session
@ -42,7 +44,8 @@ SELECT session_uuid,
country
FROM v1_session s
JOIN v1_website w
ON w.website_id = s.website_id;
ON w.website_id = s.website_id
WHERE NOT EXISTS (SELECT 1 FROM session);
-- pageview
INSERT INTO website_event
@ -58,7 +61,8 @@ FROM v1_pageview p
JOIN v1_session s
ON s.session_id = p.session_id
JOIN v1_website w
ON w.website_id = s.website_id;
ON w.website_id = s.website_id
WHERE NOT EXISTS (SELECT 1 FROM website_event WHERE event_type = 1);
-- event / event_data
INSERT INTO website_event
@ -77,4 +81,5 @@ ON s.session_id = e.session_id
JOIN v1_website w
ON w.website_id = s.website_id
LEFT JOIN v1_event_data ed
ON ed.event_id = e.event_id;
ON ed.event_id = e.event_id
WHERE NOT EXISTS (SELECT 1 FROM website_event WHERE event_type = 2);

View File

@ -36,28 +36,25 @@ async function checkConnection() {
}
async function checkV1Tables() {
try {
await prisma.$transaction([
prisma.$queryRaw`select * from _prisma_migrations where migration_name = '04_add_uuid' and finished_at IS NOT NULL`,
prisma.$queryRaw`select * from account limit 1`,
]);
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) {
console.log('Preparing v1 tables for migration');
// alter v1 tables
await dropV1Keys();
await renameV1Tables();
await dropV1Indexes();
}
success('Database v1 tables prepared for migration.');
// check for V1 renamed tables
try {
await prisma.$queryRaw`select * from v1_account limit 1`;
success('Database v1 tables ready for migration.');
} catch (e) {
// check for V1 renamed tables
try {
await prisma.$queryRaw`select * from v1_account limit 1`;
success('Database v1 tables ready for migration.');
} catch (e) {
throw new Error('Database v1 tables not found.');
}
throw new Error('Database v1 tables not found.');
}
}
@ -94,8 +91,8 @@ async function dropV1Keys() {
success('Dropped v1 database keys.');
} catch (e) {
error('Failed to drop v1 database keys.');
process.exit(1);
console.log(e);
throw new Error('Failed to drop v1 database keys.');
}
}
@ -114,8 +111,8 @@ async function renameV1Tables() {
success('Renamed v1 database tables.');
} catch (e) {
error('Failed to rename v1 database tables.');
process.exit(1);
console.log(e);
throw new Error('Failed to rename v1 database tables.');
}
}
@ -138,8 +135,8 @@ async function dropV1Indexes() {
success('Dropped v1 database indexes.');
} catch (e) {
error('Failed to drop v1 database indexes.');
process.exit(1);
console.log(e);
throw new Error('Failed to drop v1 database indexes.');
}
}
@ -148,10 +145,11 @@ async function deleteV1TablesPrompt() {
type: 'text',
name: 'value',
message: 'Do you want to delete V1 database tables? (Y/N)',
validate: value => (value !== 'Y' && value !== 'N' ? `Please enter Y or N.` : true),
validate: value =>
value.toUpperCase() !== 'Y' && value.toUpperCase() !== 'N' ? `Please enter Y or N.` : true,
});
if (response.value === 'Y') {
if (response.value.toUpperCase() == 'Y') {
await deleteV1Tables();
}