mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
add data migration script
This commit is contained in:
parent
0a731e3408
commit
c24f8cb985
80
db/postgresql/migration_v2.sql
Normal file
80
db/postgresql/migration_v2.sql
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- 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;
|
||||||
|
|
||||||
|
-- pageview
|
||||||
|
INSERT INTO website_event
|
||||||
|
(event_id, website_id, session_id, created_at, url, referrer, event_type)
|
||||||
|
SELECT gen_random_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 = s.session_id
|
||||||
|
JOIN v1_website w
|
||||||
|
ON w.website_id = s.website_id;
|
||||||
|
|
||||||
|
-- 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,
|
||||||
|
1 event_type,
|
||||||
|
e.event_name,
|
||||||
|
ed.event_data
|
||||||
|
FROM v1_event e
|
||||||
|
JOIN v1_session s
|
||||||
|
ON s.session_id = s.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;
|
@ -60,9 +60,10 @@ async function checkV2Tables() {
|
|||||||
console.log('Adding v2 tables...');
|
console.log('Adding v2 tables...');
|
||||||
|
|
||||||
// run v2 prisma migration steps
|
// run v2 prisma migration steps
|
||||||
await runInitMigration();
|
await runSqlFile('../prisma/migrations/01_init/migration.sql');
|
||||||
console.log(execSync('prisma migrate resolve --applied 01_init').toString());
|
console.log(execSync('prisma migrate resolve --applied 01_init').toString());
|
||||||
console.log(execSync('prisma migrate deploy').toString());
|
console.log(execSync('prisma migrate deploy').toString());
|
||||||
|
await runSqlFile('../db/postgresql/migration_v2.sql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,11 +119,9 @@ async function dropIndexes() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runInitMigration() {
|
async function runSqlFile(filePath) {
|
||||||
try {
|
try {
|
||||||
const rawSql = await fs.promises.readFile(
|
const rawSql = await fs.promises.readFile(path.join(__dirname, filePath));
|
||||||
path.join(__dirname, '../prisma/migrations/01_init/migration.sql'),
|
|
||||||
);
|
|
||||||
|
|
||||||
const sqlStatements = rawSql
|
const sqlStatements = rawSql
|
||||||
.toString()
|
.toString()
|
||||||
@ -136,11 +135,12 @@ async function runInitMigration() {
|
|||||||
for (const sql of sqlStatements) {
|
for (const sql of sqlStatements) {
|
||||||
await prisma.$executeRawUnsafe(sql);
|
await prisma.$executeRawUnsafe(sql);
|
||||||
}
|
}
|
||||||
|
filePath;
|
||||||
|
|
||||||
success('Ran 01_init migration.');
|
success(`Ran sql file ${filePath}.`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
throw new Error('Failed to run 01_init migration.');
|
throw new Error(`Failed to run sql file ${filePath}.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user