Prepare for DB migartions
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { Database as BunSqliteDatabase } from "bun:sqlite";
|
import { Database as BunSqliteDatabase } from "bun:sqlite";
|
||||||
import { AttachmentId, PieceId, RepertoireId, RequestId, SessionId, Sha256_Bin, UserId } from "common";
|
import { AttachmentId, PieceId, RepertoireId, RequestId, SessionId, Sha256_Bin, UserId } from "common";
|
||||||
|
import { HashSet } from "effect";
|
||||||
import { ColumnType, CompiledQuery, CreateTableBuilder, Kysely, Selectable } from "kysely";
|
import { ColumnType, CompiledQuery, CreateTableBuilder, Kysely, Selectable } from "kysely";
|
||||||
import { BunSqliteDialect } from "kysely-bun-sqlite";
|
import { BunSqliteDialect } from "kysely-bun-sqlite";
|
||||||
|
|
||||||
@@ -15,9 +16,11 @@ export interface Database {
|
|||||||
Attachment: AttachmentTable;
|
Attachment: AttachmentTable;
|
||||||
File: FileTable;
|
File: FileTable;
|
||||||
Piece: PieceTable;
|
Piece: PieceTable;
|
||||||
|
Option: OptionTable;
|
||||||
Repertoire: RepertoireTable;
|
Repertoire: RepertoireTable;
|
||||||
RepertoireEntry: RepertoireEntryTable;
|
RepertoireEntry: RepertoireEntryTable;
|
||||||
Session: SessionTable;
|
Session: SessionTable;
|
||||||
|
sqlite_schema: SqliteSchemaTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SystemInformation {
|
export interface SystemInformation {
|
||||||
@@ -52,6 +55,11 @@ export interface FileTable {
|
|||||||
data: ColumnType<Uint8Array, Uint8Array, never>;
|
data: ColumnType<Uint8Array, Uint8Array, never>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface OptionTable {
|
||||||
|
key: ColumnType<string, string, never>;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface PieceData {
|
export interface PieceData {
|
||||||
name: string;
|
name: string;
|
||||||
composer: string | null;
|
composer: string | null;
|
||||||
@@ -91,13 +99,23 @@ export interface SessionTable extends SessionData {
|
|||||||
expiresAt: string;
|
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 AccessLog = Selectable<AccessLogTable>;
|
||||||
export type Attachment = Selectable<AttachmentTable>;
|
export type Attachment = Selectable<AttachmentTable>;
|
||||||
export type File = Selectable<FileTable>;
|
export type File = Selectable<FileTable>;
|
||||||
|
export type Option = Selectable<OptionTable>;
|
||||||
export type Piece = Selectable<PieceTable>;
|
export type Piece = Selectable<PieceTable>;
|
||||||
export type Repertoire = Selectable<RepertoireTable>;
|
export type Repertoire = Selectable<RepertoireTable>;
|
||||||
export type RepertoireEntry = Selectable<RepertoireEntryTable>;
|
export type RepertoireEntry = Selectable<RepertoireEntryTable>;
|
||||||
export type Session = Selectable<SessionTable>;
|
export type Session = Selectable<SessionTable>;
|
||||||
|
export type SqliteSchema = Selectable<SqliteSchemaTable>;
|
||||||
|
|
||||||
function systemInformation<TB extends string, C extends string>(schema: CreateTableBuilder<TB, C>) {
|
function systemInformation<TB extends string, C extends string>(schema: CreateTableBuilder<TB, C>) {
|
||||||
return schema
|
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"));
|
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
|
await db.schema
|
||||||
.createTable("AccessLog")
|
.createTable("AccessLog")
|
||||||
.ifNotExists()
|
.ifNotExists()
|
||||||
@@ -222,5 +248,18 @@ export async function initDatabase(filename: string = "db.sqlite3"): Promise<Kys
|
|||||||
.columns(["pieceId", "filename"])
|
.columns(["pieceId", "filename"])
|
||||||
.execute();
|
.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;
|
return db;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user