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
14 changes: 12 additions & 2 deletions packages/void/src/cli/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { DatabaseProviderAdapter } from '../database-provider.ts';
import { resolveLocalD1Target } from '../local-d1.ts';
import type { LocalD1Target } from '../local-d1.ts';
import {
assertFlatMigrationLayout,
collectMigrations,
readJournal,
stripSqlExt,
Expand All @@ -34,7 +35,7 @@ import type { MySqlClient } from '../migrations/runner-mysql.ts';
import type { SqliteDatabase, SqliteStatement } from '../migrations/runner.ts';
import { assertJournalCoherence, validateMigrations } from '../migrations/validate.ts';
import { detectFramework } from '../plugin-inference.ts';
import { readProjectPaths } from '../project-paths.ts';
import { readProjectPaths, resolveProjectPaths } from '../project-paths.ts';
import type { SeedFn } from '../runtime/db/seed.ts';
import { cfAccessHeaders } from '../shared/cf-access.ts';
import { loadLocalEnv } from '../shared/env.ts';
Expand Down Expand Up @@ -220,7 +221,16 @@ export async function runDbCommand(
console.log();
}

const dialect = getDatabaseDialect(readConfig(root));
const config = readConfig(root);
const dialect = getDatabaseDialect(config);
if (
args.action === 'migrate' ||
args.action === 'status' ||
args.action === 'reset' ||
args.action === 'seed'
) {
assertFlatMigrationLayout(resolveProjectPaths(root, config).migrationsDir);
}

if (args.action === 'push') {
await runPush(root, dialect);
Expand Down
18 changes: 18 additions & 0 deletions packages/void/src/migrations/collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { mkdirSync, readFileSync, readdirSync, writeFileSync, existsSync } from 'node:fs';
import { dirname, join } from 'pathslash';

export function assertFlatMigrationLayout(migrationsDir: string): void {
if (!existsSync(migrationsDir)) {
return;
}

const entries = readdirSync(migrationsDir, { withFileTypes: true });
const nested = entries.find(
(entry) =>
(entry.isDirectory() || entry.isSymbolicLink()) &&
existsSync(join(migrationsDir, entry.name, 'migration.sql')),
);
if (nested) {
throw new Error(
`db: Unsupported nested migration folder '${join(migrationsDir, nested.name)}'. Void migrations use flat .sql files from drizzle-kit 0.x.`,
);
}
}

/**
* Collect all .sql migration files from the migrations directory,
* sorted by filename (lexicographic order).
Expand Down
14 changes: 14 additions & 0 deletions packages/void/test/unit/application-analysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ describe('shared application analysis', () => {
]);
});

it('ignores nested migrations managed by external tools', () => {
const applicationRoot = root();
source(
applicationRoot,
'db/migrations/20261127010000_init/migration.sql',
'CREATE TABLE events (id TEXT);',
);

const analysis = analyzeApplicationSync(applicationRoot, { config: {} });

expect(analysis.needsDatabase).toBe(false);
expect(analysis.migrations).toEqual([]);
});

it('includes migration-only database requirements without inventing source inference', () => {
const applicationRoot = root();
source(applicationRoot, 'db/migrations/001.sql', 'CREATE TABLE events (id TEXT);');
Expand Down
23 changes: 21 additions & 2 deletions packages/void/test/unit/collect-migrations.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { toSlash } from 'pathslash';
import { describe, it, expect } from 'vite-plus/test';
import { packageFixturePath } from '../../../../test/fixture-dir.ts';
import { collectMigrations } from '../../src/migrations/collect.ts';
import { assertFlatMigrationLayout, collectMigrations } from '../../src/migrations/collect.ts';

const TMP_DIR = packageFixturePath(import.meta.dirname, '.tmp-collect-test');

Expand Down Expand Up @@ -41,12 +42,14 @@ describe('collectMigrations', () => {
cleanup();
});

it('ignores non-.sql files', () => {
it('ignores non-.sql files and unrelated subdirectories', () => {
setup({
'001_create.sql': 'CREATE TABLE a;',
'README.md': '# Migrations',
'notes.txt': 'some notes',
});
mkdirSync(join(TMP_DIR, 'meta'));
writeFileSync(join(TMP_DIR, 'meta', '_journal.json'), '{}');

const result = collectMigrations(TMP_DIR);
expect(result).toHaveLength(1);
Expand All @@ -55,6 +58,22 @@ describe('collectMigrations', () => {
cleanup();
});

it('checks for nested folders separately from collecting flat migrations', () => {
setup({ '001_create.sql': 'CREATE TABLE a;' });
try {
const folder = join(TMP_DIR, '20261127010000_nested');
mkdirSync(folder);
writeFileSync(join(folder, 'migration.sql'), 'CREATE TABLE b;');

expect(collectMigrations(TMP_DIR).map(({ name }) => name)).toEqual(['001_create.sql']);
expect(() => assertFlatMigrationLayout(TMP_DIR)).toThrow(
"Unsupported nested migration folder '" + toSlash(folder) + "'",
);
} finally {
cleanup();
}
});

it('handles empty directory', () => {
setup({});
const result = collectMigrations(TMP_DIR);
Expand Down
16 changes: 16 additions & 0 deletions packages/void/test/unit/db-migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ describe('void db migrate', () => {
expect(mockLog.error).not.toHaveBeenCalled();
});

it('rejects Drizzle Kit 1.0 folders for D1', async () => {
const root = createProjectRoot({});
const folder = join(root, 'db', 'migrations', '20261127010000_create_users');
mkdirSync(folder);
writeFileSync(join(folder, 'migration.sql'), 'CREATE TABLE users (id integer PRIMARY KEY);');
writeFileSync(join(folder, 'snapshot.json'), '{}');

await expect(runDbCommand(root, { action: 'migrate' })).rejects.toThrow(
"db: Unsupported nested migration folder '" +
toSlash(folder) +
"'. Void migrations use flat .sql files from drizzle-kit 0.x.",
);
expect(mockGetAppliedMigrations).not.toHaveBeenCalled();
expect(mockRunMigrations).not.toHaveBeenCalled();
});

it('rejects remote migrate for PostgreSQL before falling through to the D1 endpoint', async () => {
const root = createPgProjectRoot({
'001_create_users.sql': 'CREATE TABLE users (id text primary key);',
Expand Down
16 changes: 16 additions & 0 deletions packages/void/test/unit/db-reset-pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { PGlite } from '@electric-sql/pglite';
import { citext } from '@electric-sql/pglite/contrib/citext';
import { toSlash } from 'pathslash';
import {
afterAll,
afterEach,
Expand Down Expand Up @@ -74,6 +75,21 @@ function project(statements: Array<string>): string {
}

describe('void db reset on PostgreSQL', () => {
it('refuses a nested migration folder before migrating or resetting PostgreSQL', async () => {
const root = project(['CREATE TABLE new_flat (id integer PRIMARY KEY);']);
const folder = join(root, 'db', 'migrations', '20261127010000_create_users');
mkdirSync(folder);
writeFileSync(join(folder, 'migration.sql'), 'CREATE TABLE users (id integer PRIMARY KEY);');
await state.pg!.exec('CREATE TABLE keep_on_refusal (id integer PRIMARY KEY)');

const message = "Unsupported nested migration folder '" + toSlash(folder) + "'";
await expect(runDbCommand(root, { action: 'migrate' })).rejects.toThrow(message);
await expect(runDbCommand(root, { action: 'reset', yes: true })).rejects.toThrow(message);
expect(
(await state.pg!.query("SELECT to_regclass('public.keep_on_refusal') AS name")).rows,
).toEqual([{ name: 'keep_on_refusal' }]);
});

it('drops types, sequences and views so the migrations apply again, and keeps extensions', async () => {
const pg = state.pg!;
// Installed by hand, outside the migrations.
Expand Down