Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
443 changes: 443 additions & 0 deletions docs/architecture-database-migration.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "drizzle-kit"

export default defineConfig({
schema: "./electron/db/schema.ts",
out: "./electron/db/migrations",
dialect: "sqlite",
})
35 changes: 35 additions & 0 deletions electron/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Database from "better-sqlite3"
import { drizzle } from "drizzle-orm/better-sqlite3"
import { app } from "electron"
import { join } from "path"
import * as schema from "./schema"

type AppDb = ReturnType<typeof drizzle<typeof schema>>

let db: AppDb | null = null
let sqlite: Database.Database | null = null

export function getDb(): AppDb {
if (!db) {
throw new Error("Database not initialized. Call initializeDb() first.")
}
return db
}

export function initializeDb(): AppDb {
const dbPath = join(app.getPath("userData"), "r2modman.db")
sqlite = new Database(dbPath)
sqlite.pragma("journal_mode = WAL")
sqlite.pragma("foreign_keys = ON")

db = drizzle({ client: sqlite, schema })
return db
}

export function closeDb(): void {
if (sqlite) {
sqlite.close()
sqlite = null
db = null
}
}
11 changes: 11 additions & 0 deletions electron/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { migrate } from "drizzle-orm/better-sqlite3/migrator"
import { getDb } from "./index"
import { join } from "path"

export function runMigrations(): void {
const db = getDb()
// In development, migrations are relative to project root.
// In production, they need to be bundled — adjust path as needed.
const migrationsFolder = join(__dirname, "../../electron/db/migrations")
migrate(db, { migrationsFolder })
Comment on lines +7 to +10
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runMigrations() uses a hard-coded migrations folder relative to __dirname ("../../electron/db/migrations"). In packaged builds __dirname points into the bundled output, so this path is likely invalid unless migrations are explicitly copied there. Consider resolving migrationsFolder via app.getAppPath()/process.resourcesPath and ensure electron-builder includes the migrations directory as an extraResource.

Copilot uses AI. Check for mistakes.
}
64 changes: 64 additions & 0 deletions electron/db/migrations/0000_perpetual_mentallo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
CREATE TABLE `game` (
`id` text PRIMARY KEY NOT NULL,
`is_default` integer DEFAULT false NOT NULL,
`last_accessed_at` text,
`created_at` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `game_settings` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`game_id` text NOT NULL,
`mod_download_folder` text,
`cache_folder` text,
`game_install_folder` text DEFAULT '' NOT NULL,
`mod_cache_folder` text DEFAULT '' NOT NULL,
`launch_parameters` text DEFAULT '' NOT NULL,
`online_mod_list_cache_date` text,
FOREIGN KEY (`game_id`) REFERENCES `game`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `game_settings_game_id_unique` ON `game_settings` (`game_id`);--> statement-breakpoint
CREATE TABLE `global_settings` (
`id` integer PRIMARY KEY NOT NULL,
`data_folder` text DEFAULT '' NOT NULL,
`steam_folder` text DEFAULT '' NOT NULL,
`mod_download_folder` text DEFAULT '' NOT NULL,
`cache_folder` text DEFAULT '' NOT NULL,
`speed_limit_enabled` integer DEFAULT false NOT NULL,
`speed_limit_bps` integer DEFAULT 0 NOT NULL,
`speed_unit` text DEFAULT 'Bps' NOT NULL,
`max_concurrent_downloads` integer DEFAULT 3 NOT NULL,
`download_cache_enabled` integer DEFAULT true NOT NULL,
`preferred_thunderstore_cdn` text DEFAULT 'main' NOT NULL,
`auto_install_mods` integer DEFAULT true NOT NULL,
`enforce_dependency_versions` integer DEFAULT true NOT NULL,
`card_display_type` text DEFAULT 'collapsed' NOT NULL,
`theme` text DEFAULT 'dark' NOT NULL,
`language` text DEFAULT 'en' NOT NULL,
`funky_mode` integer DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE `profile` (
`id` text PRIMARY KEY NOT NULL,
`game_id` text NOT NULL,
`name` text NOT NULL,
`is_default` integer DEFAULT false NOT NULL,
`is_active` integer DEFAULT false NOT NULL,
`created_at` text NOT NULL,
FOREIGN KEY (`game_id`) REFERENCES `game`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_profile_game_id` ON `profile` (`game_id`);--> statement-breakpoint
CREATE TABLE `profile_mod` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` text NOT NULL,
`mod_id` text NOT NULL,
`installed_version` text NOT NULL,
`enabled` integer DEFAULT true NOT NULL,
`dependency_warnings` text,
FOREIGN KEY (`profile_id`) REFERENCES `profile`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `uq_profile_mod_profile_mod` ON `profile_mod` (`profile_id`,`mod_id`);--> statement-breakpoint
CREATE INDEX `idx_profile_mod_profile_id` ON `profile_mod` (`profile_id`);--> statement-breakpoint
CREATE INDEX `idx_profile_mod_mod_id` ON `profile_mod` (`mod_id`);
Loading
Loading