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
27 changes: 24 additions & 3 deletions src/postgres/sql-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

function getDirname(): string {
if (typeof __dirname !== 'undefined' && __dirname) {
return __dirname;
}
const stack = new Error().stack || '';
for (const line of stack.split('\n')) {
const match = line.match(/(file:\/\/\/[^\s)]+)/);
if (match) {
try {
return dirname(fileURLToPath(match[1]));
} catch {
// continue searching
}
}
}
throw new Error('Could not determine sql-loader directory path');
}

const currentDir = getDirname();

/**
* Loads a migration's SQL from its `.sql` file — the portable source of truth
Expand All @@ -10,7 +31,7 @@ import { join } from 'path';
* analogous to how the Lua scripts are bundled), so the same relative lookup
* works at runtime.
*/
const MIGRATIONS_DIR = join(__dirname, 'migrations');
const MIGRATIONS_DIR = join(currentDir, 'migrations');

/**
* Runtime queries live under `commands/`. Each `.sql` file is one parameterized
Expand All @@ -20,7 +41,7 @@ const MIGRATIONS_DIR = join(__dirname, 'migrations');
* Python/Elixir/PHP/Rust ports (mirroring how the Redis backend's `.lua`
* scripts never hardcode the key prefix).
*/
const COMMANDS_DIR = join(__dirname, 'commands');
const COMMANDS_DIR = join(currentDir, 'commands');

const migrationCache = new Map<string, string>();
const commandCache = new Map<string, string>();
Expand Down
38 changes: 38 additions & 0 deletions tests/postgres/sql_loader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it, vi } from 'vitest';
import {
loadCommandSql,
loadMigrationSql,
} from '../../src/postgres/sql-loader';

describe('PostgreSQL SQL Loader', () => {
it('loads migration SQL files without throwing', () => {
const migration = loadMigrationSql('0001_schema.sql');
expect(typeof migration).toBe('string');
expect(migration.length).toBeGreaterThan(0);
expect(migration).toContain('CREATE TABLE');
});

it('loads command SQL files without throwing', () => {
const command = loadCommandSql('add_job');
expect(typeof command).toBe('string');
expect(command.length).toBeGreaterThan(0);
});

it('caches loaded SQL content for subsequent calls', async () => {
vi.resetModules();
const readFileSync = vi.fn(() => 'SELECT 1;');

vi.doMock('fs', () => ({
readFileSync,
}));

const { loadCommandSql: loadCommandSqlFresh } =
await import('../../src/postgres/sql-loader');

expect(loadCommandSqlFresh('add_job')).toBe('SELECT 1;');
expect(loadCommandSqlFresh('add_job')).toBe('SELECT 1;');

expect(readFileSync).toHaveBeenCalledTimes(1);
vi.doUnmock('fs');
});
});
Loading