|
| 1 | +import { |
| 2 | + existsSync, |
| 3 | + mkdirSync, |
| 4 | + mkdtempSync, |
| 5 | + readFileSync, |
| 6 | + realpathSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs' |
| 10 | +import { tmpdir } from 'node:os' |
| 11 | +import { join } from 'node:path' |
| 12 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +const spawnSyncMock = vi.hoisted(() => vi.fn()) |
| 15 | +vi.mock('node:child_process', () => ({ spawnSync: spawnSyncMock })) |
| 16 | +vi.mock('@/commands/init/utils.js', () => ({ |
| 17 | + detectPackageManager: () => 'npm', |
| 18 | + runnerArgv: () => ({ command: 'npx', prefixArgs: [] }), |
| 19 | +})) |
| 20 | + |
| 21 | +import { scaffoldDrizzleMigration } from '../drizzle-helper.js' |
| 22 | + |
| 23 | +/** |
| 24 | + * drizzle.config.ts's `out` owns where drizzle-kit writes. The scaffold must |
| 25 | + * follow the path drizzle-kit reports, never the --migrations-dir it scans as |
| 26 | + * a fallback (CIP-5162). |
| 27 | + */ |
| 28 | +// What drizzle-kit's `generate --custom` actually writes. |
| 29 | +const PLACEHOLDER = '-- Custom SQL migration file, put your code below! --\n' |
| 30 | + |
| 31 | +describe('scaffoldDrizzleMigration', () => { |
| 32 | + let root: string |
| 33 | + let cwd: string |
| 34 | + beforeEach(() => { |
| 35 | + // realpath: process.cwd() reports the physical path after chdir, so a |
| 36 | + // symlinked TMPDIR would otherwise break the expected-path assertions. |
| 37 | + root = realpathSync(mkdtempSync(join(tmpdir(), 'stash-scaffold-'))) |
| 38 | + cwd = process.cwd() |
| 39 | + process.chdir(root) |
| 40 | + spawnSyncMock.mockReset() |
| 41 | + }) |
| 42 | + afterEach(() => { |
| 43 | + process.chdir(cwd) |
| 44 | + rmSync(root, { recursive: true, force: true }) |
| 45 | + }) |
| 46 | + |
| 47 | + function drizzleKitWrites(relPath: string) { |
| 48 | + spawnSyncMock.mockImplementation(() => { |
| 49 | + mkdirSync(join(relPath, '..'), { recursive: true }) |
| 50 | + writeFileSync(relPath, PLACEHOLDER) |
| 51 | + return { |
| 52 | + status: 0, |
| 53 | + stdout: `[✓] Your SQL migration file ➜ ${relPath} 🚀\n`, |
| 54 | + stderr: '', |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + it('writes to the directory drizzle-kit reports, not the scanned one', async () => { |
| 60 | + mkdirSync('drizzle') |
| 61 | + writeFileSync( |
| 62 | + 'drizzle/0000_drop_users_email.sql', |
| 63 | + 'ALTER TABLE t DROP COLUMN c;', |
| 64 | + ) |
| 65 | + drizzleKitWrites('src/db/migrations/0003_drop_users_email.sql') |
| 66 | + |
| 67 | + const result = await scaffoldDrizzleMigration({ |
| 68 | + name: 'drop_users_email', |
| 69 | + outDir: 'drizzle', |
| 70 | + sql: 'ALTER TABLE users DROP COLUMN email;', |
| 71 | + }) |
| 72 | + |
| 73 | + expect(result.path).toBe( |
| 74 | + join(root, 'src/db/migrations/0003_drop_users_email.sql'), |
| 75 | + ) |
| 76 | + expect(readFileSync(result.path, 'utf-8')).toContain('DROP COLUMN email') |
| 77 | + expect(readFileSync('drizzle/0000_drop_users_email.sql', 'utf-8')).toBe( |
| 78 | + 'ALTER TABLE t DROP COLUMN c;', |
| 79 | + ) |
| 80 | + expect(result.dir).not.toBe(result.requestedDir) |
| 81 | + }) |
| 82 | + |
| 83 | + it('does not require the fallback directory to exist when the path is reported', async () => { |
| 84 | + drizzleKitWrites('db/0000_drop_a.sql') |
| 85 | + const result = await scaffoldDrizzleMigration({ |
| 86 | + name: 'drop_a', |
| 87 | + outDir: 'drizzle', |
| 88 | + sql: 'SELECT 1;', |
| 89 | + }) |
| 90 | + expect(existsSync(result.path)).toBe(true) |
| 91 | + }) |
| 92 | + |
| 93 | + it('falls back to the scan when the banner is unrecognised, taking only a new exact-suffix file', async () => { |
| 94 | + mkdirSync('drizzle') |
| 95 | + writeFileSync('drizzle/0000_drop_a.sql', 'ALTER TABLE t DROP COLUMN c;') |
| 96 | + spawnSyncMock.mockImplementation(() => { |
| 97 | + writeFileSync('drizzle/0001_drop_a.sql', PLACEHOLDER) |
| 98 | + writeFileSync('drizzle/0002_drop_a_backup.sql', '') |
| 99 | + return { status: 0, stdout: 'done', stderr: '' } |
| 100 | + }) |
| 101 | + const result = await scaffoldDrizzleMigration({ |
| 102 | + name: 'drop_a', |
| 103 | + outDir: 'drizzle', |
| 104 | + sql: 'SELECT 1;', |
| 105 | + }) |
| 106 | + expect(result.path).toBe(join(root, 'drizzle/0001_drop_a.sql')) |
| 107 | + expect(readFileSync('drizzle/0000_drop_a.sql', 'utf-8')).toBe( |
| 108 | + 'ALTER TABLE t DROP COLUMN c;', |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + it('refuses to overwrite a pre-existing same-stem file when nothing new appeared', async () => { |
| 113 | + mkdirSync('drizzle') |
| 114 | + writeFileSync('drizzle/0000_drop_a.sql', 'ALTER TABLE t DROP COLUMN c;') |
| 115 | + spawnSyncMock.mockReturnValue({ status: 0, stdout: 'done', stderr: '' }) |
| 116 | + await expect( |
| 117 | + scaffoldDrizzleMigration({ |
| 118 | + name: 'drop_a', |
| 119 | + outDir: 'drizzle', |
| 120 | + sql: 'SELECT 1;', |
| 121 | + }), |
| 122 | + ).rejects.toThrow(/Refusing to overwrite/) |
| 123 | + expect(readFileSync('drizzle/0000_drop_a.sql', 'utf-8')).toBe( |
| 124 | + 'ALTER TABLE t DROP COLUMN c;', |
| 125 | + ) |
| 126 | + }) |
| 127 | + |
| 128 | + it('follows a reported path even when the banner is colourised', async () => { |
| 129 | + mkdirSync('db') |
| 130 | + spawnSyncMock.mockImplementation(() => { |
| 131 | + writeFileSync('db/0000_drop_a.sql', PLACEHOLDER) |
| 132 | + return { |
| 133 | + status: 0, |
| 134 | + stdout: |
| 135 | + '[✓] Your SQL migration file \u001b[2m➜\u001b[0m \u001b[1mdb/0000_drop_a.sql\u001b[0m 🚀', |
| 136 | + stderr: '', |
| 137 | + } |
| 138 | + }) |
| 139 | + const result = await scaffoldDrizzleMigration({ |
| 140 | + name: 'drop_a', |
| 141 | + outDir: 'drizzle', |
| 142 | + sql: 'SELECT 1;', |
| 143 | + }) |
| 144 | + expect(result.path).toBe(join(root, 'db/0000_drop_a.sql')) |
| 145 | + }) |
| 146 | + |
| 147 | + it('refuses to overwrite a reported file that already has content', async () => { |
| 148 | + mkdirSync('db') |
| 149 | + writeFileSync('db/0000_old.sql', 'ALTER TABLE t DROP COLUMN c;') |
| 150 | + spawnSyncMock.mockReturnValue({ |
| 151 | + status: 0, |
| 152 | + stdout: '➜ db/0000_old.sql', |
| 153 | + stderr: '', |
| 154 | + }) |
| 155 | + await expect( |
| 156 | + scaffoldDrizzleMigration({ |
| 157 | + name: 'x', |
| 158 | + outDir: 'drizzle', |
| 159 | + sql: 'SELECT 1;', |
| 160 | + }), |
| 161 | + ).rejects.toThrow(/not a drizzle-kit scaffold/) |
| 162 | + expect(readFileSync('db/0000_old.sql', 'utf-8')).toBe( |
| 163 | + 'ALTER TABLE t DROP COLUMN c;', |
| 164 | + ) |
| 165 | + }) |
| 166 | + |
| 167 | + it('surfaces drizzle-kit stdout and stderr on failure', async () => { |
| 168 | + spawnSyncMock.mockReturnValue({ |
| 169 | + status: 1, |
| 170 | + stdout: 'config error', |
| 171 | + stderr: 'boom', |
| 172 | + }) |
| 173 | + await expect( |
| 174 | + scaffoldDrizzleMigration({ name: 'x', outDir: 'drizzle', sql: '' }), |
| 175 | + ).rejects.toThrow(/config error\nboom/) |
| 176 | + }) |
| 177 | +}) |
0 commit comments