Skip to content

Commit 85a2b27

Browse files
fix: correct ALTER TABLE ADD IDENTITY column generation
- Remove duplicate 'GENERATED' and 'AS IDENTITY' text in AT_AddIdentity case - Fix sequence name formatting to use 'SEQUENCE NAME schema.table' format - Handle NO MINVALUE/NO MAXVALUE cases when arg is null in CONSTR_IDENTITY - Fixes __tests__/kitchen-sink/original-alter-alter-table-column.test.ts Co-Authored-By: Dan Lynch <[email protected]>
1 parent 7560a5e commit 85a2b27

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

packages/deparser/src/deparser.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,20 +2704,42 @@ export class Deparser implements DeparserVisitor {
27042704
.map(option => {
27052705
if (option.DefElem) {
27062706
const defElem = option.DefElem;
2707-
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
2708-
if (defElem.defname === 'start') {
2707+
if (defElem.defname === 'sequence_name') {
2708+
if (defElem.arg && defElem.arg.List) {
2709+
const nameList = ListUtils.unwrapList(defElem.arg)
2710+
.map(item => this.visit(item, context))
2711+
.join('.');
2712+
return `SEQUENCE NAME ${nameList}`;
2713+
}
2714+
return 'SEQUENCE NAME';
2715+
} else if (defElem.defname === 'start') {
2716+
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
27092717
return `START WITH ${argValue}`;
27102718
} else if (defElem.defname === 'increment') {
2719+
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
27112720
return `INCREMENT BY ${argValue}`;
27122721
} else if (defElem.defname === 'minvalue') {
2713-
return `MINVALUE ${argValue}`;
2722+
if (defElem.arg) {
2723+
const argValue = this.visit(defElem.arg, context);
2724+
return `MINVALUE ${argValue}`;
2725+
} else {
2726+
return 'NO MINVALUE';
2727+
}
27142728
} else if (defElem.defname === 'maxvalue') {
2715-
return `MAXVALUE ${argValue}`;
2729+
if (defElem.arg) {
2730+
const argValue = this.visit(defElem.arg, context);
2731+
return `MAXVALUE ${argValue}`;
2732+
} else {
2733+
return 'NO MAXVALUE';
2734+
}
27162735
} else if (defElem.defname === 'cache') {
2736+
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
27172737
return `CACHE ${argValue}`;
27182738
} else if (defElem.defname === 'cycle') {
2739+
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
27192740
return argValue === 'true' ? 'CYCLE' : 'NO CYCLE';
27202741
}
2742+
const argValue = defElem.arg ? this.visit(defElem.arg, context) : '';
27212743
return `${defElem.defname.toUpperCase()} ${argValue}`;
27222744
}
27232745
return this.visit(option, context);
@@ -5234,11 +5256,10 @@ export class Deparser implements DeparserVisitor {
52345256
if (node.name) {
52355257
output.push(QuoteUtils.quote(node.name));
52365258
}
5237-
output.push('ADD GENERATED');
5259+
output.push('ADD');
52385260
if (node.def) {
52395261
output.push(this.visit(node.def, context));
52405262
}
5241-
output.push('AS IDENTITY');
52425263
break;
52435264
case 'AT_SetIdentity':
52445265
output.push('ALTER COLUMN');

0 commit comments

Comments
 (0)