import { expect, test } from 'bun:test' import { runIsolated } from '@stacksjs/testing' import { mkdtemp, realpath, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { alignmentOpenOptions } from '../src/sqlite-schema-align' // The exported alignment API and actual Buddy command must inspect a real SQLite // file. This catches public dialect admission and preservation, not SQL mocks. for (const mode of ['api', 'cli', 'apply', 'cli-apply', 'migrate', 'provider-postgres', 'provider-turso', 'custom-apply', 'case-apply', 'enum-apply', 'constant-apply', 'table-constraints-apply', 'unsafe', 'unsafe-empty', 'primary', 'partial-unique', 'rollback', 'writer', 'replan', 'fk-apply', 'fk-default', 'fk-violation', 'integrity', 'seed'] as const) { const behavior = mode.startsWith('provider-') ? 'refuses a provider override and preserves the unused file' : mode === 'case-apply' ? 'matches existing identifiers without duplicate additions' : mode.includes('apply') ? 'applies missing columns losslessly' : 'checks its native safety and preservation contract' test(`SQLite model alignment ${mode} ${behavior}`, async () => { const root = await realpath(await mkdtemp(join(tmpdir(), 'stacks-sqlite-align-'))) try { const config = join(root, 'bunfig.toml') await writeFile(config, 'preload = []\n') const result = await runIsolated(`${import.meta.dir}/fixtures/schema-align-sqlite.ts`, { cwd: root, bunfig: config, envFile: false, args: [mode], timeoutMs: 10_000, env: { ...process.env, APP_ENV: 'test', DB_CONNECTION: 'sqlite', DB_DATABASE_PATH: join(root, 'app.sqlite'), STACKS_SQLITE_ALIGN_ROOT: root, DATABASE_URL: undefined, DB_HOST: '127.0.0.1', DB_PORT: '1', DB_DATABASE: 'unused', DB_USERNAME: 'fixture', DB_PASSWORD: '', TURSO_DATABASE_URL: 'libsql://127.0.0.1:1', TURSO_AUTH_TOKEN: '' }, }) expect(result.timedOut, `${result.stdout}\n${result.stderr}`).toBe(false) expect(result.code, `${result.stdout}\n${result.stderr}`).toBe(0) expect(result.stdout).toContain(`SQLite alignment ${mode} preserved data`) } finally { await rm(root, { recursive: true, force: true }) } }, 15_000) } /** * The way alignment opens the file, on the oldest Bun Stacks supports. * * `{ readonly: false, create: false }` names neither mode, and Bun before * 1.4.4 refuses it, so `migrate:align --apply` failed on every SQLite database * there. CI runs 1.4.4, which accepts it, so the suite above cannot see this; * the open is tried under `bun x bun@` instead, as the backup's * oldest-Bun test does. The suite itself does not load on 1.3.0. */ for (const version of ['1.3.0', Bun.version]) { test.skipIf(process.platform === 'win32')(`SQLite alignment opens the database it is asked to on Bun ${version}`, async () => { const root = await realpath(await mkdtemp(join(tmpdir(), 'stacks-sqlite-align-open-'))) try { const probe = join(root, 'probe.ts') await writeFile(probe, `import { Database } from 'bun:sqlite' const [file, apply, plan] = process.argv.slice(2) new Database(file).close() const writer = new Database(file, JSON.parse(apply)) writer.run('CREATE TABLE written (id INTEGER)') writer.close() const reader = new Database(file, JSON.parse(plan)) let refused = false try { reader.run('CREATE TABLE refused (id INTEGER)') } catch { refused = true } reader.close() console.log(JSON.stringify({ wrote: true, refused })) `) const runtime = version === Bun.version ? [process.execPath] : [process.execPath, 'x', `bun@${version}`] const child = Bun.spawnSync([...runtime, probe, join(root, 'app.sqlite'), JSON.stringify(alignmentOpenOptions(true)), JSON.stringify(alignmentOpenOptions(false))], { cwd: root, stdout: 'pipe', stderr: 'pipe' }) expect(child.exitCode, child.stderr.toString()).toBe(0) // Applying writes; planning cannot. expect(JSON.parse(child.stdout.toString().trim().split('\n').at(-1)!)).toEqual({ wrote: true, refused: true }) } finally { await rm(root, { recursive: true, force: true }) } }, 120_000) }