|
| 1 | +import { Flags } from '@oclif/core' |
| 2 | +import { BaseCommand } from '../../base-command.js' |
| 3 | +import { readLocalMigrations } from '../../checks/migrations.js' |
| 4 | +import { getAppliedVersions, ensureMigrationsTable } from '../../migrate.js' |
| 5 | +import { ok, warn, dim, bold } from '../../ui.js' |
| 6 | +import { pgQuery } from '../../db.js' |
| 7 | + |
| 8 | +/** |
| 9 | + * List local migration files and their applied status. |
| 10 | + * |
| 11 | + * Without --offline, connects to the target database to show which |
| 12 | + * migrations are applied (✓) vs pending (○). |
| 13 | + */ |
| 14 | +export default class MigrateList extends BaseCommand { |
| 15 | + static override description = 'List local migration files and their applied/pending status' |
| 16 | + |
| 17 | + static override examples = [ |
| 18 | + '<%= config.bin %> migrate list', |
| 19 | + '<%= config.bin %> migrate list --env=production', |
| 20 | + '<%= config.bin %> migrate list --offline', |
| 21 | + '<%= config.bin %> migrate list --json', |
| 22 | + ] |
| 23 | + |
| 24 | + static override flags = { |
| 25 | + env: Flags.string({ |
| 26 | + char: 'e', |
| 27 | + description: 'Environment to check applied status against', |
| 28 | + }), |
| 29 | + offline: Flags.boolean({ |
| 30 | + description: 'List local files only without connecting to the database', |
| 31 | + default: false, |
| 32 | + }), |
| 33 | + json: Flags.boolean({ description: 'Output results as JSON' }), |
| 34 | + } |
| 35 | + |
| 36 | + async run(): Promise<void> { |
| 37 | + const { flags } = await this.parse(MigrateList) |
| 38 | + |
| 39 | + const config = await this.loadConfigOrFail() |
| 40 | + const dir = this.resolveMigrationsDir(config) |
| 41 | + |
| 42 | + const migrations = await readLocalMigrations(dir).catch(() => []) |
| 43 | + |
| 44 | + if (flags.offline || !config.source) { |
| 45 | + // Offline: list files only |
| 46 | + if (flags.json) { |
| 47 | + this.log(JSON.stringify(migrations, null, 2)) |
| 48 | + return |
| 49 | + } |
| 50 | + if (migrations.length === 0) { |
| 51 | + this.log(`\n ${dim('No local migration files found in')} ${dim(dir)}\n`) |
| 52 | + return |
| 53 | + } |
| 54 | + this.log(`\n ${bold(`${migrations.length} local migration(s)`)} ${dim(`in ${dir}`)}\n`) |
| 55 | + for (const m of migrations) { |
| 56 | + this.log(` ${dim('○')} ${m.filename}`) |
| 57 | + } |
| 58 | + this.log('') |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Online: connect to DB and check applied status |
| 63 | + const { envName, env } = this.resolveEnv(config, flags.env) |
| 64 | + |
| 65 | + const pre = this.createPreflight('Migrate list preflight checks') |
| 66 | + .addDatabase('Target', envName, env.dbUrl) |
| 67 | + await this.runPreflight(pre, 'Migrate list') |
| 68 | + |
| 69 | + let applied: Set<string> |
| 70 | + try { |
| 71 | + await ensureMigrationsTable(env.dbUrl, pgQuery) |
| 72 | + applied = await getAppliedVersions(env.dbUrl, pgQuery) |
| 73 | + } catch { |
| 74 | + applied = new Set() |
| 75 | + } |
| 76 | + |
| 77 | + if (flags.json) { |
| 78 | + const result = migrations.map(m => ({ |
| 79 | + ...m, |
| 80 | + applied: applied.has(m.version), |
| 81 | + })) |
| 82 | + this.log(JSON.stringify(result, null, 2)) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if (migrations.length === 0) { |
| 87 | + this.log(`\n ${dim('No local migration files found in')} ${dim(dir)}\n`) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + const pendingCount = migrations.filter(m => !applied.has(m.version)).length |
| 92 | + const appliedCount = migrations.length - pendingCount |
| 93 | + |
| 94 | + this.log(`\n ${bold(`${migrations.length} migration(s)`)} ${dim(`→ ${envName}`)} · ${ok(`${appliedCount} applied`)} · ${pendingCount > 0 ? warn(`${pendingCount} pending`) : dim('0 pending')}\n`) |
| 95 | + |
| 96 | + for (const m of migrations) { |
| 97 | + const isApplied = applied.has(m.version) |
| 98 | + const icon = isApplied ? ok('✓') : dim('○') |
| 99 | + const label = isApplied ? dim(m.filename) : m.filename |
| 100 | + this.log(` ${icon} ${label}`) |
| 101 | + } |
| 102 | + this.log('') |
| 103 | + } |
| 104 | +} |
0 commit comments