From 44f7bbcd000b47359003b84a771eaadb0d835fe7 Mon Sep 17 00:00:00 2001 From: erensanlier Date: Wed, 24 Jul 2024 23:28:58 +0300 Subject: [PATCH] fix: SQLite Directory Selection to Prefer Config Directory (#995) * fix: Prefer config.database.directory for SQLite directory if available - Updated SQLite directory selection to use config.database.directory if provided. * add test and use ternary * changeset --------- Co-authored-by: typedarray <90073088+0xOlias@users.noreply.github.com> --- .changeset/metal-apes-care.md | 5 +++++ .../build/configAndIndexingFunctions.test.ts | 21 +++++++++++++++++++ .../src/build/configAndIndexingFunctions.ts | 7 ++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/metal-apes-care.md diff --git a/.changeset/metal-apes-care.md b/.changeset/metal-apes-care.md new file mode 100644 index 000000000..b6ecf0224 --- /dev/null +++ b/.changeset/metal-apes-care.md @@ -0,0 +1,5 @@ +--- +"@ponder/core": patch +--- + +Fixed a bug where the SQLite `directory` option in `ponder.config.ts` was not respected. diff --git a/packages/core/src/build/configAndIndexingFunctions.test.ts b/packages/core/src/build/configAndIndexingFunctions.test.ts index 03ac980bd..9795706e6 100644 --- a/packages/core/src/build/configAndIndexingFunctions.test.ts +++ b/packages/core/src/build/configAndIndexingFunctions.test.ts @@ -640,6 +640,27 @@ test("buildConfigAndIndexingFunctions() database uses sqlite by default", async process.env.DATABASE_URL = prev; }); +test("buildConfigAndIndexingFunctions() database respects custom sqlite path", async () => { + const config = createConfig({ + database: { kind: "sqlite", directory: "custom-sqlite/directory" }, + networks: { mainnet: { chainId: 1, transport: http() } }, + contracts: { a: { network: "mainnet", abi: [event0] } }, + }); + + const { databaseConfig } = await buildConfigAndIndexingFunctions({ + config, + rawIndexingFunctions: [{ name: "a:Event0", fn: () => {} }], + options, + }); + + console.log(databaseConfig); + + expect(databaseConfig).toMatchObject({ + kind: "sqlite", + directory: expect.stringContaining(path.join("custom-sqlite", "directory")), + }); +}); + test("buildConfigAndIndexingFunctions() database uses sqlite if specified even if DATABASE_URL env var present", async () => { const config = createConfig({ database: { kind: "sqlite" }, diff --git a/packages/core/src/build/configAndIndexingFunctions.ts b/packages/core/src/build/configAndIndexingFunctions.ts index 2a7c10d4c..a38a3e6fc 100644 --- a/packages/core/src/build/configAndIndexingFunctions.ts +++ b/packages/core/src/build/configAndIndexingFunctions.ts @@ -54,7 +54,12 @@ export async function buildConfigAndIndexingFunctions({ // Build database. let databaseConfig: DatabaseConfig; - const sqliteDir = path.join(ponderDir, "sqlite"); + // Determine SQLite directory, preferring config.database.directory if available + const sqliteDir = + config.database?.kind === "sqlite" && config.database.directory + ? path.resolve(config.database.directory) + : path.join(ponderDir, "sqlite"); + const sqlitePrintPath = path.relative(rootDir, sqliteDir); if (config.database?.kind) {