Skip to content

Commit

Permalink
Make ASTRO_DATABASE_FILE work with file paths (#10631)
Browse files Browse the repository at this point in the history
* Make ASTRO_DATABASE_FILE work with file paths

* Use pathToFileURL
  • Loading branch information
matthewp authored Apr 1, 2024
1 parent da2fb87 commit 157392e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-years-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Make ASTRO_DATABASE_FILE work with file paths
5 changes: 2 additions & 3 deletions packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,11 @@ export function getLocalVirtualModContents({

const dbUrl = new URL(DB_PATH, root);
return `
import { asDrizzleTable, createLocalDatabaseClient } from ${RUNTIME_IMPORT};
import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ''}
${shouldSeed ? integrationSeedImportStatements.join('\n') : ''}
const dbUrl = import.meta.env.ASTRO_DATABASE_FILE ?? ${JSON.stringify(dbUrl)};
const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
export const db = createLocalDatabaseClient({ dbUrl });
${
Expand Down
15 changes: 15 additions & 0 deletions packages/db/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'drizzle-orm/sqlite-core';
import { type DBColumn, type DBTable } from '../core/types.js';
import { type SerializedSQL, isSerializedSQL } from './types.js';
import { pathToFileURL } from 'url';

export type { Table } from './types.js';
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
Expand Down Expand Up @@ -130,3 +131,17 @@ function handleSerializedSQL<T>(def: T | SerializedSQL) {
}
return def;
}

export function normalizeDatabaseUrl(envDbUrl: string | undefined, defaultDbUrl: string): string {
if(envDbUrl) {
// This could be a file URL, or more likely a root-relative file path.
// Convert it to a file URL.
if(envDbUrl.startsWith('file://')) {
return envDbUrl;
}
return new URL(envDbUrl, pathToFileURL(process.cwd())).toString();
} else {
// This is going to be a file URL always,
return defaultDbUrl;
}
}
22 changes: 21 additions & 1 deletion packages/db/test/local-prod.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import testAdapter from '../../astro/test/test-adapter.js';
import { loadFixture } from '../../astro/test/test-utils.js';
import { fileURLToPath } from 'url';

describe('astro:db local database', () => {
let fixture;
Expand All @@ -12,7 +13,7 @@ describe('astro:db local database', () => {
});
});

describe('build (not remote) with DATABASE_FILE env', () => {
describe('build (not remote) with DATABASE_FILE env (file URL)', () => {
const prodDbPath = new URL('./fixtures/basics/dist/astro.db', import.meta.url).toString();
before(async () => {
process.env.ASTRO_DATABASE_FILE = prodDbPath;
Expand All @@ -31,6 +32,25 @@ describe('astro:db local database', () => {
});
});

describe('build (not remote) with DATABASE_FILE env (file path)', () => {
const prodDbPath = fileURLToPath(new URL('./fixtures/basics/dist/astro.db', import.meta.url));
before(async () => {
process.env.ASTRO_DATABASE_FILE = prodDbPath;
await fixture.build();
});

after(async () => {
delete process.env.ASTRO_DATABASE_FILE;
});

it('Can render page', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
expect(response.status).to.equal(200);
});
});

describe('build (not remote)', () => {
it('should throw during the build for server output', async () => {
delete process.env.ASTRO_DATABASE_FILE;
Expand Down

0 comments on commit 157392e

Please sign in to comment.