2024-05-16 03:37:00 +00:00
|
|
|
import type { Database } from "sql.js";
|
|
|
|
|
2024-05-16 04:37:23 +00:00
|
|
|
const migrations = [m01_initial];
|
|
|
|
|
|
|
|
export default async function executeMigrations(db: Database) {
|
|
|
|
// Check last migration status
|
|
|
|
const migrationsWithNames = migrations.map<
|
|
|
|
[string, (_: Database) => Promise<void>]
|
|
|
|
>((func) => [func.name, func]);
|
|
|
|
let startMigrationAt = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const rows = await db.exec("SELECT name FROM _migrations LIMIT 1");
|
|
|
|
if (rows.length < 1) throw new Error("wtf");
|
|
|
|
const migrationStatus = rows?.[0];
|
|
|
|
const lastMigrationName = migrationStatus.values[0][0];
|
|
|
|
console.log("status", migrationStatus);
|
|
|
|
if (lastMigrationName) {
|
|
|
|
const foundIndex = migrationsWithNames.findIndex(
|
|
|
|
([name]) => name === migrationStatus.values[0][0],
|
|
|
|
);
|
|
|
|
if (foundIndex >= 0) startMigrationAt = foundIndex + 1;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Don't have the table
|
|
|
|
await db.run("BEGIN TRANSACTION");
|
|
|
|
await db.run(`
|
|
|
|
CREATE TABLE IF NOT EXISTS _migrations (name TEXT PRIMARY KEY);
|
|
|
|
INSERT INTO _migrations (name) VALUES (NULL);
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS _appDataVersion (version INTEGER PRIMARY KEY);
|
2024-05-16 06:02:44 +00:00
|
|
|
INSERT INTO _appDataVersion (version) VALUES (0);
|
2024-05-16 04:37:23 +00:00
|
|
|
`);
|
|
|
|
await db.exec("COMMIT TRANSACTION");
|
|
|
|
console.log("Created table.");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(migrationsWithNames);
|
|
|
|
console.log(startMigrationAt);
|
|
|
|
|
|
|
|
const migrationsToRun = migrationsWithNames.slice(startMigrationAt);
|
|
|
|
console.log(`Running ${migrationsToRun.length} migrations...`);
|
|
|
|
for (const [name, migration] of migrationsToRun) {
|
|
|
|
console.log("Running migration", name);
|
|
|
|
await db.exec("BEGIN TRANSACTION");
|
|
|
|
await migration(db);
|
|
|
|
await db.exec("UPDATE _migrations SET name = $name", { $name: name });
|
|
|
|
await db.exec("COMMIT TRANSACTION");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function m01_initial(db: Database) {
|
|
|
|
db.exec(`
|
|
|
|
CREATE TABLE charts (
|
2024-05-16 06:02:44 +00:00
|
|
|
chart_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
artist TEXT,
|
|
|
|
title TEXT,
|
|
|
|
difficulty TEXT,
|
|
|
|
banner_image TEXT
|
2024-05-16 04:37:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE scores (
|
2024-05-16 06:02:44 +00:00
|
|
|
score_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
chart_id INTEGER,
|
|
|
|
|
|
|
|
FOREIGN KEY (chart_id) REFERENCES charts(chart_id)
|
2024-05-16 04:37:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE banners (
|
2024-05-16 06:02:44 +00:00
|
|
|
name TEXT PRIMARY KEY
|
2024-05-16 04:37:23 +00:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
}
|