|
| 1 | +import { describe, expect, test, beforeAll, afterAll } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import os from 'node:os'; |
| 5 | +import { transformSql, validateSql } from './dsql-compat'; |
| 6 | + |
| 7 | +let tmpDir: string; |
| 8 | + |
| 9 | +beforeAll(() => { |
| 10 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'compat-test-')); |
| 11 | +}); |
| 12 | + |
| 13 | +afterAll(() => { |
| 14 | + fs.rmSync(tmpDir, { recursive: true }); |
| 15 | +}); |
| 16 | + |
| 17 | +function runCheckCompat(migrationsDir: string): { exitCode: number; errors: string[] } { |
| 18 | + const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')); |
| 19 | + let transformed = 0; |
| 20 | + const errors: string[] = []; |
| 21 | + |
| 22 | + for (const file of files) { |
| 23 | + const filePath = path.join(migrationsDir, file); |
| 24 | + const original = fs.readFileSync(filePath, 'utf8'); |
| 25 | + const sql = transformSql(original); |
| 26 | + if (sql !== original) { |
| 27 | + fs.writeFileSync(filePath, sql); |
| 28 | + transformed++; |
| 29 | + } |
| 30 | + for (const e of validateSql(sql)) { |
| 31 | + errors.push(`${file}: ${e.pattern}`); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return { exitCode: errors.length > 0 ? 1 : 0, errors }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('check-dsql-compat integration', () => { |
| 39 | + test('C1: transforms drizzle-kit output', () => { |
| 40 | + const sqlFile = path.join(tmpDir, '0001.sql'); |
| 41 | + fs.writeFileSync( |
| 42 | + sqlFile, |
| 43 | + 'CREATE TABLE "T" ("id" text PRIMARY KEY, "userId" text NOT NULL REFERENCES "User"("id"));--> statement-breakpoint\nCREATE INDEX "T_userId_idx" ON "T" ("userId");', |
| 44 | + ); |
| 45 | + |
| 46 | + const { exitCode } = runCheckCompat(tmpDir); |
| 47 | + expect(exitCode).toBe(0); |
| 48 | + |
| 49 | + const result = fs.readFileSync(sqlFile, 'utf8'); |
| 50 | + expect(result).not.toContain('statement-breakpoint'); |
| 51 | + expect(result).not.toContain('REFERENCES'); |
| 52 | + expect(result).toContain('CREATE INDEX ASYNC'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('C2: no-transform file unchanged', () => { |
| 56 | + // Clean up previous files |
| 57 | + for (const f of fs.readdirSync(tmpDir)) fs.unlinkSync(path.join(tmpDir, f)); |
| 58 | + |
| 59 | + const sqlFile = path.join(tmpDir, '0002.sql'); |
| 60 | + const content = 'ALTER TABLE "T" ADD COLUMN "name" text;\n'; |
| 61 | + fs.writeFileSync(sqlFile, content); |
| 62 | + |
| 63 | + runCheckCompat(tmpDir); |
| 64 | + |
| 65 | + expect(fs.readFileSync(sqlFile, 'utf8')).toBe(content); |
| 66 | + }); |
| 67 | + |
| 68 | + test('C3: unfixable pattern returns exit 1', () => { |
| 69 | + // Clean up previous files |
| 70 | + for (const f of fs.readdirSync(tmpDir)) fs.unlinkSync(path.join(tmpDir, f)); |
| 71 | + |
| 72 | + const sqlFile = path.join(tmpDir, '0003.sql'); |
| 73 | + fs.writeFileSync(sqlFile, 'ALTER TABLE "T" ALTER COLUMN "c" TYPE varchar(100);\n'); |
| 74 | + |
| 75 | + const { exitCode, errors } = runCheckCompat(tmpDir); |
| 76 | + expect(exitCode).toBe(1); |
| 77 | + expect(errors.some((e) => e.includes('ALTER COLUMN TYPE'))).toBe(true); |
| 78 | + }); |
| 79 | +}); |
0 commit comments