Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,17 @@ export function createCLI(): Command {
.command('create-duckdb')
.description('add a new DuckDB database connection')
.argument('<name>')
.option('-d, --database-path <database>')
.addOption(
new Option(
'--database-path <database>',
'path to DuckDB database file or MotherDuck database (e.g., "md:my_database")'
)
)
.addOption(
new Option('--mother-duck-token <token>', 'MotherDuck API token').env(
'MOTHERDUCK_TOKEN'
)
)
.action(createDuckDbConnectionCommand);

connections
Expand Down
124 changes: 124 additions & 0 deletions test/commands/connections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {Command} from '@commander-js/extra-typings';
import {createCLI} from '../../src/cli';
import path from 'path';
import {errorMessage} from '../../src/util';
import fs from 'fs';
import os from 'os';

let cli: Command;
let args: string[];
Expand Down Expand Up @@ -95,5 +97,127 @@ describe('commands', () => {
);
});
});

describe('create-duckdb', () => {
let tempConfigPath: string;

beforeEach(() => {
// Create a temporary config file for each test
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'malloy-cli-test-')
);
tempConfigPath = path.join(tempDir, 'config.json');
fs.writeFileSync(
tempConfigPath,
JSON.stringify({connections: []}, null, 2)
);
});

afterEach(() => {
// Clean up temporary config file
if (tempConfigPath && fs.existsSync(tempConfigPath)) {
const configDir = path.dirname(tempConfigPath);
fs.unlinkSync(tempConfigPath);
fs.rmdirSync(configDir);
}
});

it('creates a DuckDB connection with motherDuckToken from command line', async () => {
await runWith(
'-c',
tempConfigPath,
'connections',
'create-duckdb',
'test-motherduck',
'--database-path',
'md:my_database',
'--mother-duck-token',
'test-token-123'
);

// Verify the connection was created with the token
const configContent = JSON.parse(
fs.readFileSync(tempConfigPath, 'utf-8')
);
const connection = configContent.connections.find(
(c: {name: string}) => c.name === 'test-motherduck'
);
expect(connection).toBeDefined();
expect(connection.backend).toBe('duckdb');
expect(connection.databasePath).toBe('md:my_database');
expect(connection.motherDuckToken).toBe('test-token-123');
});

it('creates a DuckDB connection with motherDuckToken from environment variable', async () => {
const originalEnv = process.env.MOTHERDUCK_TOKEN;
process.env.MOTHERDUCK_TOKEN = 'env-token-456';

try {
await runWith(
'-c',
tempConfigPath,
'connections',
'create-duckdb',
'test-motherduck-env',
'--database-path',
'md:'
);

// Verify the connection was created with the token from env
const configContent = JSON.parse(
fs.readFileSync(tempConfigPath, 'utf-8')
);
const connection = configContent.connections.find(
(c: {name: string}) => c.name === 'test-motherduck-env'
);
expect(connection).toBeDefined();
expect(connection.backend).toBe('duckdb');
expect(connection.databasePath).toBe('md:');
expect(connection.motherDuckToken).toBe('env-token-456');
} finally {
// Restore original environment variable
if (originalEnv !== undefined) {
process.env.MOTHERDUCK_TOKEN = originalEnv;
} else {
delete process.env.MOTHERDUCK_TOKEN;
}
}
});

it('creates a DuckDB connection without motherDuckToken', async () => {
// Clear any existing MOTHERDUCK_TOKEN from the environment
const originalEnv = process.env.MOTHERDUCK_TOKEN;
delete process.env.MOTHERDUCK_TOKEN;

try {
await runWith(
'-c',
tempConfigPath,
'connections',
'create-duckdb',
'test-duckdb-local',
'--database-path',
'/path/to/local.duckdb'
);

// Verify the connection was created without the token
const configContent = JSON.parse(
fs.readFileSync(tempConfigPath, 'utf-8')
);
const connection = configContent.connections.find(
(c: {name: string}) => c.name === 'test-duckdb-local'
);
expect(connection).toBeDefined();
expect(connection.backend).toBe('duckdb');
expect(connection.databasePath).toBe('/path/to/local.duckdb');
expect(connection.motherDuckToken).toBeUndefined();
} finally {
// Restore original environment variable
if (originalEnv !== undefined) {
process.env.MOTHERDUCK_TOKEN = originalEnv;
}
}
});
});
});
});