fix(sql-tools): null default (#20796)

This commit is contained in:
Jason Rasmussen 2025-08-08 15:44:39 -04:00 committed by GitHub
parent 538d5c81ea
commit 2ce4f8dd3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 3 deletions

View File

@ -62,6 +62,23 @@ describe('compareColumns', () => {
]);
});
it('should detect a change in default', () => {
const source: DatabaseColumn = { ...testColumn, nullable: true };
const target: DatabaseColumn = { ...testColumn, nullable: true, default: "''" };
const reason = `default is different (null vs '')`;
expect(compareColumns.onCompare(source, target)).toEqual([
{
columnName: 'test',
tableName: 'table1',
type: 'ColumnAlter',
changes: {
default: 'NULL',
},
reason,
},
]);
});
it('should detect a comment change', () => {
const source: DatabaseColumn = { ...testColumn, comment: 'new comment' };
const target: DatabaseColumn = { ...testColumn, comment: 'old comment' };

View File

@ -72,9 +72,9 @@ export const compareColumns = {
tableName: source.tableName,
columnName: source.name,
changes: {
default: String(source.default),
default: String(source.default ?? 'NULL'),
},
reason: `default is different (${source.default} vs ${target.default})`,
reason: `default is different (${source.default ?? 'null'} vs ${target.default})`,
});
}

View File

@ -175,7 +175,7 @@ export const isDefaultEqual = (source: DatabaseColumn, target: DatabaseColumn) =
if (
withTypeCast(source.default, getColumnType(source)) === target.default ||
source.default === withTypeCast(target.default, getColumnType(target))
withTypeCast(target.default, getColumnType(target)) === source.default
) {
return true;
}

View File

@ -116,6 +116,20 @@ describe(transformColumns.name, () => {
}),
).toEqual([`ALTER TABLE "table1" ALTER COLUMN "column1" SET DEFAULT uuid_generate_v4();`]);
});
it('should update the default value to NULL', () => {
expect(
transformColumns(ctx, {
type: 'ColumnAlter',
tableName: 'table1',
columnName: 'column1',
changes: {
default: 'NULL',
},
reason: 'unknown',
}),
).toEqual([`ALTER TABLE "table1" ALTER COLUMN "column1" SET DEFAULT NULL;`]);
});
});
describe('ColumnDrop', () => {