Skip to content

Commit

Permalink
Merge pull request #1433 from evidence-dev/fix-motherduck
Browse files Browse the repository at this point in the history
Allow motherduck and in-memory duckdb connections
  • Loading branch information
archiewood authored Dec 29, 2023
2 parents 858f500 + 995e71a commit 153dea7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-dancers-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@evidence-dev/duckdb': patch
---

Allow MotherDuck and in-memory DuckDB connections
30 changes: 23 additions & 7 deletions packages/duckdb/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,24 @@ function duckdbDescribeToEvidenceType(describe) {

/** @type {import("@evidence-dev/db-commons").RunQuery<DuckDBOptions>} */
const runQuery = async (queryString, database, batchSize = 100000) => {
const filename = database ? database.filename : getEnv(envMap, 'filename') ?? ':memory:';
let filename;

if (database && database.filename) {
if (database.filename.startsWith('md:?motherduck_token=') || database.filename === ':memory:') {
// MotherDuck or in-memory database
filename = database.filename;
} else {
// Local database stored in source directory
filename = path.join(database.directory, database.filename);
}
} else {
// Check the filenames from environment variables
filename =
getEnv(envMap, 'filename') ??
// Default to in-memory db
':memory:';
}

const mode = filename !== ':memory:' ? OPEN_READONLY : OPEN_READWRITE;

try {
Expand Down Expand Up @@ -185,17 +202,16 @@ module.exports.getRunner = async (opts, directory) => {
return async (queryContent, queryPath, batchSize) => {
// Filter out non-sql files
if (!queryPath.endsWith('.sql')) return null;
return runQuery(
queryContent,
{ ...opts, filename: path.join(directory, opts.filename) },
batchSize
);
return runQuery(queryContent, { ...opts, directory: directory }, batchSize);
};
};

/** @type {import("@evidence-dev/db-commons").ConnectionTester<DuckDBOptions>} */
module.exports.testConnection = async (opts, directory) => {
const r = await runQuery('SELECT 1;', { ...opts, filename: path.join(directory, opts.filename) })
const r = await runQuery('SELECT 1;', {
...opts,
directory: directory
})
.then(exhaustStream)
.then(() => true)
.catch((e) => {
Expand Down

0 comments on commit 153dea7

Please sign in to comment.