|
28 | 28 | * neither reported nor changed; a changed literal stores the new value; a row |
29 | 29 | * stored before is not rewritten; and with no auto-reconcile the two entries |
30 | 30 | * are reported `safe` and applied by the explicit apply door. |
| 31 | + * |
| 32 | + * [#36596] §3, one cell per dialect: the same removal on a NOT NULL column. |
| 33 | + * #36245 first shipped a MySQL `DROP DEFAULT`, which on MySQL 8 leaves even a |
| 34 | + * NULLABLE column with no default at all, so §2's unset insert was refused |
| 35 | + * ("Field 'digest' doesn't have a default value", ER 1364) — MariaDB 10.11, |
| 36 | + * where it was measured, stores NULL. §2 now holds on MySQL 8 as it stands. On |
| 37 | + * a NOT NULL column the drop never travels alone, since an omitting insert |
| 38 | + * would then be refused: where the declaration does not hold the column NOT |
| 39 | + * NULL it is paired with the `relax_not_null` (`needs_confirm`, both applied by |
| 40 | + * the explicit apply door, and by the publish when the field says `storage: |
| 41 | + * { notNull: false }` in words), after which an unset insert stores NULL; |
| 42 | + * where it does, the column is left alone and the entry reported `destructive`. |
31 | 43 | */ |
32 | 44 |
|
33 | 45 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
@@ -198,6 +210,128 @@ function measure(cell: DialectCell): void { |
198 | 210 | expect(await second.detectManagedDrift([SECOND])).toEqual([]); |
199 | 211 | }); |
200 | 212 | }); |
| 213 | + |
| 214 | + describe(`#36596 §3 — a stale literal DEFAULT on a NOT NULL column (${cell.label})`, () => { |
| 215 | + const NN = 'os36596_pref'; |
| 216 | + const open: SqlDriver[] = []; |
| 217 | + let dir: string | undefined; |
| 218 | + |
| 219 | + const config = (): Record<string, any> => { |
| 220 | + if (cell.id !== 'sqlite') return cell.config(); |
| 221 | + dir ??= mkdtempSync(join(tmpdir(), 'os36596-')); |
| 222 | + return { ...cell.config(), connection: { filename: join(dir, 'db.sqlite') } }; |
| 223 | + }; |
| 224 | + |
| 225 | + const boot = async (obj: any, opts: Record<string, any> = {}): Promise<SqlDriver> => { |
| 226 | + const driver = new SqlDriver({ ...config(), ...opts } as any); |
| 227 | + (driver as any).logger = quietLogger(); |
| 228 | + open.push(driver); |
| 229 | + await driver.initObjects([obj]); |
| 230 | + return driver; |
| 231 | + }; |
| 232 | + |
| 233 | + const insertUnset = async (driver: SqlDriver, id: string) => { |
| 234 | + await driver.create(NN, { id, title: id }, { bypassTenantAudit: true } as any); |
| 235 | + return (driver as any).knex(NN).where('id', id).first(); |
| 236 | + }; |
| 237 | + |
| 238 | + /** The first release: `code` is NOT NULL, with a literal default. */ |
| 239 | + const FIRST_NN = { |
| 240 | + name: NN, |
| 241 | + fields: { |
| 242 | + title: { type: 'string' }, |
| 243 | + code: { type: 'string', defaultValue: 'none', storage: { notNull: true } }, |
| 244 | + }, |
| 245 | + } as any; |
| 246 | + const second = (code: Record<string, any>) => ({ name: NN, fields: { title: { type: 'string' }, code: { type: 'string', ...code } } }) as any; |
| 247 | + |
| 248 | + /** Create the first release's table, then boot the second declaration over it. */ |
| 249 | + const upgrade = async (next: any, opts: Record<string, any> = {}): Promise<SqlDriver> => { |
| 250 | + const first = await boot(FIRST_NN); |
| 251 | + await (first as any).knex.schema.dropTableIfExists(NN); |
| 252 | + await first.initObjects([FIRST_NN]); |
| 253 | + await first.disconnect(); |
| 254 | + open.splice(open.indexOf(first), 1); |
| 255 | + return boot(next, opts); |
| 256 | + }; |
| 257 | + |
| 258 | + const nullable = async (driver: SqlDriver): Promise<boolean> => |
| 259 | + (await (driver as any).knex(NN).columnInfo())?.code?.nullable === true; |
| 260 | + |
| 261 | + afterEach(async () => { |
| 262 | + const last = open[open.length - 1]; |
| 263 | + if (last) await (last as any).knex.schema.dropTableIfExists(NN); |
| 264 | + for (const d of open.splice(0)) await d.disconnect(); |
| 265 | + if (dir) rmSync(dir, { recursive: true, force: true }); |
| 266 | + dir = undefined; |
| 267 | + }); |
| 268 | + |
| 269 | + it('no default, no storage declaration, not required: the drop is paired with the relax and an unset insert then stores NULL', async () => { |
| 270 | + const next = second({}); |
| 271 | + const driver = await upgrade(next, { autoMigrate: 'safe', schemaMode: 'managed' }); |
| 272 | + |
| 273 | + const drift = await driver.detectManagedDrift([next]); |
| 274 | + expect(drift.map((d) => [d.column, d.op.type, d.category]).sort()).toEqual([ |
| 275 | + ['code', 'drop_column_default', 'needs_confirm'], |
| 276 | + ['code', 'relax_not_null', 'needs_confirm'], |
| 277 | + ]); |
| 278 | + // Dev auto-reconcile applies neither (ADR-0113 D5): the column is left as |
| 279 | + // it was, so an unset insert still stores the old default, never refused. |
| 280 | + expect(await nullable(driver)).toBe(false); |
| 281 | + expect((await insertUnset(driver, 'unrepaired')).code).toBe('none'); |
| 282 | + |
| 283 | + const { applied, skipped } = await driver.applyMigrationEntries(drift, { allowDestructive: false }); |
| 284 | + expect(skipped).toEqual([]); |
| 285 | + expect(applied).toHaveLength(2); |
| 286 | + expect(await nullable(driver)).toBe(true); |
| 287 | + expect((await insertUnset(driver, 'repaired')).code).toBeNull(); |
| 288 | + expect(await driver.detectManagedDrift([next])).toEqual([]); |
| 289 | + // The row stored before keeps what it holds. |
| 290 | + expect((await (driver as any).knex(NN).where('id', 'unrepaired').first()).code).toBe('none'); |
| 291 | + }); |
| 292 | + |
| 293 | + it('`storage: { notNull: false }` declared: the publish applies the relax and the drop together', async () => { |
| 294 | + const next = second({ storage: { notNull: false } }); |
| 295 | + const driver = await upgrade(next); |
| 296 | + expect((await insertUnset(driver, 'unrepaired')).code).toBe('none'); |
| 297 | + |
| 298 | + const { applied, skipped } = await driver.applyPublishedColumnChanges(next); |
| 299 | + expect(skipped).toEqual([]); |
| 300 | + expect(applied.map((d) => d.op.type).sort()).toEqual(['drop_column_default', 'relax_not_null']); |
| 301 | + expect((await insertUnset(driver, 'repaired')).code).toBeNull(); |
| 302 | + expect(await driver.detectManagedDrift([next])).toEqual([]); |
| 303 | + }); |
| 304 | + |
| 305 | + it('a declaration that still holds the column NOT NULL: the column is left alone and the entry reported `destructive`', async () => { |
| 306 | + const next = second({ storage: { notNull: true } }); |
| 307 | + const driver = await upgrade(next, { autoMigrate: 'safe', schemaMode: 'managed' }); |
| 308 | + |
| 309 | + const drift = await driver.detectManagedDrift([next]); |
| 310 | + expect(drift.map((d) => [d.column, d.op.type, d.category])).toEqual([['code', 'drop_column_default', 'destructive']]); |
| 311 | + expect(drift[0].message).toMatch(/Left as it is/); |
| 312 | + |
| 313 | + // Neither the boot, the publish nor the non-destructive apply door touches it. |
| 314 | + expect((await driver.applyPublishedColumnChanges(next)).applied).toEqual([]); |
| 315 | + const { applied } = await driver.applyMigrationEntries(drift, { allowDestructive: false }); |
| 316 | + expect(applied).toEqual([]); |
| 317 | + expect(await nullable(driver)).toBe(false); |
| 318 | + expect((await insertUnset(driver, 'kept')).code).toBe('none'); |
| 319 | + }); |
| 320 | + |
| 321 | + it('a `required` field over the NOT NULL column (no storage declaration): left alone and reported `destructive`', async () => { |
| 322 | + const next = second({ required: true }); |
| 323 | + const driver = await upgrade(next); |
| 324 | + |
| 325 | + const drift = await driver.detectManagedDrift([next]); |
| 326 | + // The relax stays silent for a legacy `required` field (ADR-0113); the |
| 327 | + // stale default is reported, never applied by a non-destructive door. |
| 328 | + expect(drift.map((d) => [d.column, d.op.type, d.category])).toEqual([['code', 'drop_column_default', 'destructive']]); |
| 329 | + expect((await driver.applyMigrationEntries(drift, { allowDestructive: false })).applied).toEqual([]); |
| 330 | + expect(await nullable(driver)).toBe(false); |
| 331 | + await driver.create(NN, { id: 'r', title: 'r', code: 'given' }, { bypassTenantAudit: true } as any); |
| 332 | + expect((await (driver as any).knex(NN).where('id', 'r').first()).code).toBe('given'); |
| 333 | + }); |
| 334 | + }); |
201 | 335 | } |
202 | 336 |
|
203 | 337 | for (const cell of DIALECT_CELLS) { |
|
0 commit comments