|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #3954 — how `os migrate plan` renders the pending work. |
| 5 | + * |
| 6 | + * The additive section carries an implicit promise: what it lists is created, |
| 7 | + * never data-losing, and needs no `--allow-destructive` thought. The datetime |
| 8 | + * convergence (#3912/#3942) rewrites rows and rebuilds columns, so folding it |
| 9 | + * into that section would quietly extend the promise to cover a table rewrite. |
| 10 | + * These tests pin the split, and pin that the summary line — the one an operator |
| 11 | + * reads before typing "yes" — never omits in-place work. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 15 | +import { |
| 16 | + renderPendingSchemaWork, |
| 17 | + summarizePendingSchemaWork, |
| 18 | + type PendingSchemaWork, |
| 19 | +} from './schema-migrate.js'; |
| 20 | + |
| 21 | +let lines: string[]; |
| 22 | + |
| 23 | +beforeEach(() => { |
| 24 | + lines = []; |
| 25 | + vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => { |
| 26 | + lines.push(args.map(String).join(' ')); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vi.restoreAllMocks(); |
| 32 | +}); |
| 33 | + |
| 34 | +const out = () => lines.join('\n'); |
| 35 | + |
| 36 | +const ADDITIVE: PendingSchemaWork[] = [ |
| 37 | + { table: 'widgets', kind: 'create_table', columns: ['sku', 'qty'] }, |
| 38 | + { table: 'orders', kind: 'add_columns', columns: ['note'] }, |
| 39 | +]; |
| 40 | + |
| 41 | +const IN_PLACE: PendingSchemaWork[] = [ |
| 42 | + { table: 'evt', kind: 'normalize_datetime_storage', columns: ['at'], rows: 1234567 }, |
| 43 | + { table: 'legacy', kind: 'widen_datetime_columns', columns: ['at', 'created_at'], rows: 42 }, |
| 44 | +]; |
| 45 | + |
| 46 | +describe('renderPendingSchemaWork (#3954)', () => { |
| 47 | + it('renders nothing at all when there is nothing pending', () => { |
| 48 | + renderPendingSchemaWork([]); |
| 49 | + expect(out()).toBe(''); |
| 50 | + }); |
| 51 | + |
| 52 | + it('keeps the additive section exactly as it was when only additive work is pending', () => { |
| 53 | + renderPendingSchemaWork(ADDITIVE); |
| 54 | + expect(out()).toContain('New (additive — created when you apply)'); |
| 55 | + expect(out()).toContain('widgets'); |
| 56 | + expect(out()).toContain('[create_table, 2 column(s)]'); |
| 57 | + expect(out()).toContain('[add_columns: note]'); |
| 58 | + // No second heading appears when there is no in-place work. |
| 59 | + expect(out()).not.toContain('In place'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('puts the datetime convergence under its OWN heading, not the additive one', () => { |
| 63 | + renderPendingSchemaWork(IN_PLACE); |
| 64 | + expect(out()).toContain('In place (existing rows converged when you apply)'); |
| 65 | + // The additive heading claims the work is never data-losing; a row rewrite |
| 66 | + // must never be listed beneath it. |
| 67 | + expect(out()).not.toContain('New (additive'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('names the columns and the size of each in-place step', () => { |
| 71 | + renderPendingSchemaWork(IN_PLACE); |
| 72 | + expect(out()).toContain('normalize_datetime_storage: at'); |
| 73 | + expect(out()).toContain('1,234,567 row update(s)'); |
| 74 | + expect(out()).toContain('widen_datetime_columns: at, created_at'); |
| 75 | + // A MySQL widen is ALTER … MODIFY — a rebuild, said outright. |
| 76 | + expect(out()).toContain('42 row table rebuild'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('shows both sections when both kinds are pending', () => { |
| 80 | + renderPendingSchemaWork([...ADDITIVE, ...IN_PLACE]); |
| 81 | + expect(out()).toContain('New (additive — created when you apply)'); |
| 82 | + expect(out()).toContain('In place (existing rows converged when you apply)'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('reads an unmeasured count as unknown rather than zero', () => { |
| 86 | + renderPendingSchemaWork([{ table: 'evt', kind: 'normalize_datetime_storage', columns: ['at'] }]); |
| 87 | + expect(out()).toContain('? row update(s)'); |
| 88 | + expect(out()).not.toContain('0 row update(s)'); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('summarizePendingSchemaWork (#3954)', () => { |
| 93 | + it('is unchanged for purely additive work', () => { |
| 94 | + expect(summarizePendingSchemaWork(ADDITIVE)).toBe('1 table(s) to create, 1 column(s) to add'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('is unchanged when nothing is pending', () => { |
| 98 | + expect(summarizePendingSchemaWork([])).toBe('0 table(s) to create, 0 column(s) to add'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('never omits in-place work — this is the line read before confirming', () => { |
| 102 | + const summary = summarizePendingSchemaWork([...ADDITIVE, ...IN_PLACE]); |
| 103 | + expect(summary).toContain('1 table(s) to create'); |
| 104 | + expect(summary).toContain('1 column(s) to add'); |
| 105 | + expect(summary).toContain('3 datetime column(s) to converge in place'); |
| 106 | + expect(summary).toContain('~1,234,609 rows'); |
| 107 | + }); |
| 108 | +}); |
0 commit comments