Skip to content

Commit 91a7999

Browse files
feat: add multi-line pretty formatting for ALTER TABLE statements
- Format each ALTER TABLE clause on its own line when pretty: true - Enhanced CONSTR_IDENTITY formatting with proper indentation for sequence options - Each sequence option appears on its own line with consistent indentation - Follows existing pretty formatting patterns used throughout the deparser Example output: ALTER TABLE public.table1 ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY ( SEQUENCE NAME public.table1 START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1 ) Co-Authored-By: Dan Lynch <[email protected]>
1 parent 46d33ae commit 91a7999

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/deparser/__tests__/pretty/__snapshots__/alter-table-column.test.ts.snap

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@
22

33
exports[`non-pretty: original/alter/alter-table-column-1.sql 1`] = `"ALTER TABLE public.table1 ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (SEQUENCE NAME public.table1 START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1)"`;
44

5-
exports[`pretty: original/alter/alter-table-column-1.sql 1`] = `"ALTER TABLE public.table1 ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (SEQUENCE NAME public.table1 START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1)"`;
5+
exports[`pretty: original/alter/alter-table-column-1.sql 1`] = `
6+
"ALTER TABLE public.table1
7+
ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
8+
SEQUENCE NAME public.table1
9+
START WITH 1
10+
INCREMENT BY 1
11+
NO MINVALUE
12+
NO MAXVALUE
13+
CACHE 1
14+
)"
15+
`;

packages/deparser/src/deparser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,12 @@ export class Deparser implements DeparserVisitor {
27442744
}
27452745
return this.visit(option, context);
27462746
});
2747-
output.push(`(${optionStrs.join(' ')})`);
2747+
if (context.isPretty()) {
2748+
const indentedOptions = optionStrs.map(option => context.indent(option));
2749+
output.push('(\n' + indentedOptions.join('\n') + '\n)');
2750+
} else {
2751+
output.push(`(${optionStrs.join(' ')})`);
2752+
}
27482753
}
27492754
break;
27502755
case 'CONSTR_PRIMARY':
@@ -4689,10 +4694,7 @@ export class Deparser implements DeparserVisitor {
46894694
const commandsStr = commands
46904695
.map(cmd => {
46914696
const cmdStr = this.visit(cmd, alterContext);
4692-
if (cmdStr.startsWith('ADD CONSTRAINT') || cmdStr.startsWith('ADD ')) {
4693-
return context.newline() + context.indent(cmdStr);
4694-
}
4695-
return cmdStr;
4697+
return context.newline() + context.indent(cmdStr);
46964698
})
46974699
.join(',');
46984700
output.push(commandsStr);

0 commit comments

Comments
 (0)