Prepare for DB migartions
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Database as BunSqliteDatabase } from "bun:sqlite";
|
||||
import { AttachmentId, PieceId, RepertoireId, RequestId, SessionId, Sha256_Bin, UserId } from "common";
|
||||
import { HashSet } from "effect";
|
||||
import { ColumnType, CompiledQuery, CreateTableBuilder, Kysely, Selectable } from "kysely";
|
||||
import { BunSqliteDialect } from "kysely-bun-sqlite";
|
||||
|
||||
@@ -15,9 +16,11 @@ export interface Database {
|
||||
Attachment: AttachmentTable;
|
||||
File: FileTable;
|
||||
Piece: PieceTable;
|
||||
Option: OptionTable;
|
||||
Repertoire: RepertoireTable;
|
||||
RepertoireEntry: RepertoireEntryTable;
|
||||
Session: SessionTable;
|
||||
sqlite_schema: SqliteSchemaTable;
|
||||
}
|
||||
|
||||
export interface SystemInformation {
|
||||
@@ -52,6 +55,11 @@ export interface FileTable {
|
||||
data: ColumnType<Uint8Array, Uint8Array, never>;
|
||||
}
|
||||
|
||||
export interface OptionTable {
|
||||
key: ColumnType<string, string, never>;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface PieceData {
|
||||
name: string;
|
||||
composer: string | null;
|
||||
@@ -91,13 +99,23 @@ export interface SessionTable extends SessionData {
|
||||
expiresAt: string;
|
||||
}
|
||||
|
||||
export interface SqliteSchemaTable {
|
||||
type: ColumnType<string, never, never>;
|
||||
name: ColumnType<string, never, never>;
|
||||
tbl_name: ColumnType<string, never, never>;
|
||||
rootpage: ColumnType<number, never, never>;
|
||||
sql: ColumnType<string | null, never, never>;
|
||||
}
|
||||
|
||||
export type AccessLog = Selectable<AccessLogTable>;
|
||||
export type Attachment = Selectable<AttachmentTable>;
|
||||
export type File = Selectable<FileTable>;
|
||||
export type Option = Selectable<OptionTable>;
|
||||
export type Piece = Selectable<PieceTable>;
|
||||
export type Repertoire = Selectable<RepertoireTable>;
|
||||
export type RepertoireEntry = Selectable<RepertoireEntryTable>;
|
||||
export type Session = Selectable<SessionTable>;
|
||||
export type SqliteSchema = Selectable<SqliteSchemaTable>;
|
||||
|
||||
function systemInformation<TB extends string, C extends string>(schema: CreateTableBuilder<TB, C>) {
|
||||
return schema
|
||||
@@ -115,6 +133,14 @@ export async function initDatabase(filename: string = "db.sqlite3"): Promise<Kys
|
||||
|
||||
await db.executeQuery(CompiledQuery.raw("PRAGMA foreign_keys = ON"));
|
||||
|
||||
const tables = await db
|
||||
.selectFrom("sqlite_schema")
|
||||
.select("name")
|
||||
.where("type", "=", "table")
|
||||
.execute()
|
||||
.then((tables) => HashSet.make(...tables.map(({ name }) => name)));
|
||||
|
||||
if (!HashSet.has(tables, "Option")) {
|
||||
await db.schema
|
||||
.createTable("AccessLog")
|
||||
.ifNotExists()
|
||||
@@ -222,5 +248,18 @@ export async function initDatabase(filename: string = "db.sqlite3"): Promise<Kys
|
||||
.columns(["pieceId", "filename"])
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createTable("Option")
|
||||
.ifNotExists()
|
||||
.addColumn("key", "text", (c) => c.notNull().primaryKey())
|
||||
.addColumn("value", "text", (c) => c.notNull())
|
||||
.execute();
|
||||
|
||||
await db
|
||||
.insertInto("Option")
|
||||
.values({ key: "database_version", value: "0" })
|
||||
.execute();
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user