// scripts/ci/check-supabase-migrations.mjs // Supabase 마이그레이션 파일 정합성 검증 (GAP-OPS-01). // // 검증 항목: // 1) server/supabase/migrations 내 모든 파일이 `<14자리 타임스탬프>_.sql` 형식인지 // 2) 타임스탬프 접두사가 중복되지 않는지 // 3) 파일 목록(사전순)에서 타임스탬프가 엄격히 단조 증가하는지 // 4) 각 SQL 파일이 비어 있지 않은지 import { readdirSync, readFileSync, statSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const root = join(dirname(fileURLToPath(import.meta.url)), '..', '..') const migrationsDir = join(root, 'server', 'supabase', 'migrations') const MIGRATION_PATTERN = /^(\d{14})_([a-z0-9]+(?:_[a-z0-9]+)*)\.sql$/ export function validateMigrationFiles(entries) { const errors = [] if (!Array.isArray(entries) || entries.length === 0) { errors.push('no_migration_files_found') return { ok: false, count: 0, errors } } const seenTimestamps = new Map() let previousTimestamp = null let previousFile = null for (const entry of entries) { const { name, size } = entry const match = MIGRATION_PATTERN.exec(name) if (!match) { errors.push(`invalid_migration_filename:${name}`) continue } const timestamp = match[1] if (seenTimestamps.has(timestamp)) { errors.push(`duplicate_migration_timestamp:${timestamp}:${seenTimestamps.get(timestamp)}:${name}`) } else { seenTimestamps.set(timestamp, name) } if (previousTimestamp !== null && BigInt(timestamp) <= BigInt(previousTimestamp)) { errors.push(`non_monotonic_migration_timestamp:${previousFile}->${name}`) } if (typeof size === 'number' && size <= 0) { errors.push(`empty_migration_file:${name}`) } previousTimestamp = timestamp previousFile = name } return { ok: errors.length === 0, count: entries.length, first: entries[0]?.name ?? null, last: entries[entries.length - 1]?.name ?? null, errors, } } function loadActualEntries() { const names = readdirSync(migrationsDir).sort((a, b) => a.localeCompare(b)) return names.map((name) => { const fullPath = join(migrationsDir, name) const stat = statSync(fullPath) const content = stat.isFile() ? readFileSync(fullPath, 'utf8').trim() : '' return { name, size: content.length, } }) } const actual = validateMigrationFiles(loadActualEntries()) if (!actual.ok) { throw new Error(`supabase_migrations_invalid:${actual.errors.join(',')}`) } if (process.argv.includes('--self-test')) { const validSample = [ { name: '20260409000001_initial_schema.sql', size: 120 }, { name: '20260409000002_rls_policies.sql', size: 85 }, ] if (!validateMigrationFiles(validSample).ok) { throw new Error('check_supabase_migrations_self_test_failed:valid_sample_rejected') } const dupSample = [ { name: '20260409000001_initial_schema.sql', size: 120 }, { name: '20260409000001_duplicate_prefix.sql', size: 85 }, ] const dupRes = validateMigrationFiles(dupSample) if (dupRes.ok || !dupRes.errors.some((e) => e.startsWith('duplicate_migration_timestamp:'))) { throw new Error('check_supabase_migrations_self_test_failed:duplicate_not_caught') } const badNameSample = [ { name: '20260409_short_ts.sql', size: 100 }, ] const badNameRes = validateMigrationFiles(badNameSample) if (badNameRes.ok || !badNameRes.errors.some((e) => e.startsWith('invalid_migration_filename:'))) { throw new Error('check_supabase_migrations_self_test_failed:invalid_name_not_caught') } const emptySample = [ { name: '20260409000001_empty.sql', size: 0 }, ] const emptyRes = validateMigrationFiles(emptySample) if (emptyRes.ok || !emptyRes.errors.some((e) => e.startsWith('empty_migration_file:'))) { throw new Error('check_supabase_migrations_self_test_failed:empty_not_caught') } actual.selfTest = 'passed' } process.stdout.write(`${JSON.stringify(actual, null, 2)}\n`)