Skip to content
Open
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
32 changes: 30 additions & 2 deletions packages/database/dev/src/lib/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test('Throws MigrationFileNotFoundError for directory missing migration.sql', as
db = await PGlite.create()

await expect(applyMigrations(db, tmpDir.path, '0001_missing_file')).rejects.toThrow(
/Migration SQL file not found: .*0001_missing_file[/\\]migration.sql/,
/Migration "0001_missing_file" is missing its SQL file at .*0001_missing_file[/\\]migration\.sql/,
)
})

Expand Down Expand Up @@ -280,7 +280,20 @@ test('Throws when a directory and flat file share the same name', async () => {

db = await PGlite.create()

await expect(applyMigrations(db, tmpDir.path)).rejects.toThrow(/Duplicate migration name "0001_create_users"/)
await expect(applyMigrations(db, tmpDir.path)).rejects.toThrow(
/Two or more migrations share the name "0001_create_users"/,
)
})

test('Throws when distinct migrations share a version', async () => {
tmpDir = await tmp.dir()

await createMigrationDir(tmpDir.path, '0001_create_users', 'CREATE TABLE users (id SERIAL PRIMARY KEY)')
await createFlatMigration(tmpDir.path, '0001_create_posts', 'CREATE TABLE posts (id SERIAL PRIMARY KEY)')

db = await PGlite.create()

await expect(applyMigrations(db, tmpDir.path)).rejects.toThrow(/Version 1 is used by multiple migrations/)
})

test('Ignores flat .sql files whose name does not match the migration pattern', async () => {
Expand All @@ -297,3 +310,18 @@ test('Ignores flat .sql files whose name does not match the migration pattern',

expect(applied).toEqual(['0001_create_users'])
})

test('Skips entries with disallowed characters (uppercase, spaces, special) in the slug', async () => {
tmpDir = await tmp.dir()

await createFlatMigration(tmpDir.path, '0001_create_users', 'CREATE TABLE users (id SERIAL PRIMARY KEY)')
await createMigrationDir(tmpDir.path, '0002_CAPS_NOT_OK', 'CREATE TABLE caps (id SERIAL PRIMARY KEY)')
await createMigrationDir(tmpDir.path, '0003_special!chars', 'CREATE TABLE bad (id SERIAL PRIMARY KEY)')
await fs.writeFile(join(tmpDir.path, '0004_with space.sql'), 'CREATE TABLE bad (id SERIAL PRIMARY KEY)')

db = await PGlite.create()

const applied = await applyMigrations(db, tmpDir.path)

expect(applied).toEqual(['0001_create_users'])
})
Loading
Loading