add mysql migration_v2 file, refactor migrate-db

This commit is contained in:
Francis Cao 2023-01-13 11:54:22 -08:00
parent 96de650a67
commit 65fe29c385
3 changed files with 95 additions and 4 deletions

85
db/mysql/migration_v2.sql Normal file
View File

@ -0,0 +1,85 @@
-- account
DELETE FROM `user`
WHERE username = 'admin';
INSERT INTO `user`
(user_id, username, password, role, created_at, updated_at, deleted_at)
SELECT account_uuid,
username,
password,
CASE WHEN is_admin = true THEN 'admin' ELSE 'user' END,
created_at,
updated_at,
NULL
FROM v1_account
WHERE NOT EXISTS (SELECT 1 FROM `user`);
-- website
INSERT INTO website
(website_id, name, domain, share_id, rev_id, user_id, team_id, created_at)
SELECT website_uuid,
name,
domain,
share_id,
0 rev_id,
a.account_uuid,
NULL team_id,
a.created_at
FROM v1_website w
JOIN v1_account a
ON a.user_id = w.user_id
WHERE NOT EXISTS (SELECT 1 FROM website);
-- session
INSERT INTO session
(session_id, website_id, hostname, browser, os, device, screen, language, country)
SELECT session_uuid,
w.website_uuid,
hostname,
browser,
os,
device,
screen,
language,
country
FROM v1_session s
JOIN v1_website w
ON w.website_id = s.website_id
WHERE NOT EXISTS (SELECT 1 FROM session);
-- pageview
INSERT INTO website_event
(event_id, website_id, session_id, created_at, url, referrer, event_type)
SELECT uuid() event_id,
w.website_uuid,
s.session_uuid,
p.created_at,
p.url,
p.referrer,
1 event_type
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
WHERE NOT EXISTS (SELECT 1 FROM website_event WHERE event_type = 1);
-- event / event_data
INSERT INTO website_event
(event_id, website_id, session_id, created_at, url, event_type, event_name, event_data)
SELECT e.event_uuid,
w.website_uuid,
s.session_uuid,
e.created_at,
e.url,
2 event_type,
e.event_name,
ed.event_data
FROM v1_event e
JOIN v1_session s
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
WHERE NOT EXISTS (SELECT 1 FROM website_event WHERE event_type = 2);

View File

@ -106,3 +106,6 @@ CREATE TABLE `team_user` (
INDEX `team_user_user_id_idx`(`user_id`),
PRIMARY KEY (`team_user_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateAdminUser
INSERT INTO account (username, password, is_admin) values ('admin', '$2b$10$BUli0c.muyCW1ErNJc3jL.vFRFtFJWrT8/GcR4A.sUdCznaXiqFXa', true);

View File

@ -59,8 +59,8 @@ async function checkV1Tables(databaseType) {
await dropV1Keys();
}
await renameV1Tables(databaseType);
await dropV1Indexes(databaseType);
await renameV1Tables(databaseType);
}
// check for V1 renamed tables
@ -162,11 +162,12 @@ async function dropV1Indexes(databaseType) {
]);
} else {
await prisma.$transaction([
prisma.$executeRaw`DROP INDEX session_session_uuid_key ON session;`,
prisma.$executeRaw`ALTER TABLE session DROP FOREIGN KEY session_website_id_fkey;`,
prisma.$executeRaw`DROP INDEX session_created_at_idx ON session;`,
prisma.$executeRaw`DROP INDEX session_website_id_idx ON session;`,
prisma.$executeRaw`ALTER TABLE website DROP FOREIGN KEY website_user_id_fkey;`,
prisma.$executeRaw`DROP INDEX website_user_id_idx ON website;`,
prisma.$executeRaw`DROP INDEX website_share_id_key ON website;`,
prisma.$executeRaw`DROP INDEX website_website_uuid_key ON website;`,
]);
}
@ -226,7 +227,9 @@ async function runSqlFile(filePath) {
.split(';');
for (const sql of sqlStatements) {
await prisma.$executeRawUnsafe(sql);
if (sql.length > 0) {
await prisma.$executeRawUnsafe(sql);
}
}
filePath;