-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix problems with local libSQL DB (#12089)
Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
fef0b8c
commit 6e06e6e
Showing
14 changed files
with
214 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@astrojs/db': patch | ||
--- | ||
|
||
Fixes initial schema push for local file and in-memory libSQL DB |
This file contains 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,5 @@ | ||
--- | ||
'@astrojs/db': patch | ||
--- | ||
|
||
Fixes relative local libSQL db URL |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,10 @@ | ||
import db from '@astrojs/db'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [db()], | ||
devToolbar: { | ||
enabled: false, | ||
}, | ||
}); |
This file contains 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,13 @@ | ||
import { column, defineDb, defineTable } from 'astro:db'; | ||
|
||
const User = defineTable({ | ||
columns: { | ||
id: column.text({ primaryKey: true, optional: false }), | ||
username: column.text({ optional: false, unique: true }), | ||
password: column.text({ optional: false }), | ||
}, | ||
}); | ||
|
||
export default defineDb({ | ||
tables: { User }, | ||
}); |
This file contains 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 { User, db } from 'astro:db'; | ||
|
||
export default async function () { | ||
await db.batch([ | ||
db.insert(User).values([{ id: 'mario', username: 'Mario', password: 'itsame' }]), | ||
]); | ||
} |
This file contains 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,14 @@ | ||
{ | ||
"name": "@test/db-libsql-remote", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "astro dev", | ||
"build": "astro build", | ||
"preview": "astro preview" | ||
}, | ||
"dependencies": { | ||
"@astrojs/db": "workspace:*", | ||
"astro": "workspace:*" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/db/test/fixtures/libsql-remote/src/pages/index.astro
This file contains 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 @@ | ||
--- | ||
/// <reference path="../../.astro/db-types.d.ts" /> | ||
import { User, db } from 'astro:db'; | ||
const users = await db.select().from(User); | ||
--- | ||
|
||
<h2>Users</h2> | ||
<ul class="users-list"> | ||
{users.map((user) => <li>{user.name}</li>)} | ||
</ul> |
This file contains 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,77 @@ | ||
import assert from 'node:assert/strict'; | ||
import { relative } from 'node:path'; | ||
import { rm } from 'node:fs/promises'; | ||
import { after, before, describe, it } from 'node:test'; | ||
import { fileURLToPath } from 'node:url'; | ||
import testAdapter from '../../astro/test/test-adapter.js'; | ||
import { loadFixture } from '../../astro/test/test-utils.js'; | ||
import { clearEnvironment, initializeRemoteDb } from './test-utils.js'; | ||
|
||
describe('astro:db local database', () => { | ||
let fixture; | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/libsql-remote/', import.meta.url), | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
}); | ||
|
||
describe('build --remote with local libSQL file (absolute path)', () => { | ||
before(async () => { | ||
clearEnvironment(); | ||
|
||
const absoluteFileUrl = new URL('./fixtures/libsql-remote/dist/absolute.db', import.meta.url); | ||
// Remove the file if it exists to avoid conflict between test runs | ||
await rm(absoluteFileUrl, { force: true }); | ||
|
||
process.env.ASTRO_INTERNAL_TEST_REMOTE = true; | ||
process.env.ASTRO_DB_REMOTE_URL = absoluteFileUrl.toString(); | ||
await fixture.build(); | ||
await initializeRemoteDb(fixture.config); | ||
}); | ||
|
||
after(async () => { | ||
delete process.env.ASTRO_INTERNAL_TEST_REMOTE; | ||
delete process.env.ASTRO_DB_REMOTE_URL; | ||
}); | ||
|
||
it('Can render page', async () => { | ||
const app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 200); | ||
}); | ||
}); | ||
|
||
describe('build --remote with local libSQL file (relative path)', () => { | ||
before(async () => { | ||
clearEnvironment(); | ||
|
||
const absoluteFileUrl = new URL('./fixtures/libsql-remote/dist/relative.db', import.meta.url); | ||
const prodDbPath = relative( | ||
fileURLToPath(fixture.config.root), | ||
fileURLToPath(absoluteFileUrl), | ||
); | ||
// Remove the file if it exists to avoid conflict between test runs | ||
await rm(prodDbPath, { force: true }); | ||
|
||
process.env.ASTRO_INTERNAL_TEST_REMOTE = true; | ||
process.env.ASTRO_DB_REMOTE_URL = `file:${prodDbPath}`; | ||
await fixture.build(); | ||
await initializeRemoteDb(fixture.config); | ||
}); | ||
|
||
after(async () => { | ||
delete process.env.ASTRO_INTERNAL_TEST_REMOTE; | ||
delete process.env.ASTRO_DB_REMOTE_URL; | ||
}); | ||
|
||
it('Can render page', async () => { | ||
const app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 200); | ||
}); | ||
}); | ||
}); |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.