-
Notifications
You must be signed in to change notification settings - Fork 0
build: migrate to db #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c199e8
init: add interface
AkaraChen 312d083
init: add interface implement, need more test
AkaraChen 980a3f6
init: db
AkaraChen 3e30129
feat: trpc design interface
AkaraChen 98edc7a
feat: migrate data handling to database for games, mods, profiles, an…
AkaraChen 416ae99
feat: implement tRPC services and data handling for database mode
AkaraChen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.