Skip to content

Commit 75a7c9c

Browse files
feat: sql tools array as default value (#24389)
1 parent ae8f5a6 commit 75a7c9c

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

server/src/sql-tools/decorators/column.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { asOptions } from 'src/sql-tools/helpers';
22
import { register } from 'src/sql-tools/register';
33
import { ColumnStorage, ColumnType, DatabaseEnum } from 'src/sql-tools/types';
44

5-
export type ColumnValue = null | boolean | string | number | object | Date | (() => string);
5+
export type ColumnValue = null | boolean | string | number | Array<unknown> | object | Date | (() => string);
66

77
export type ColumnBaseOptions = {
88
name?: string;

server/src/sql-tools/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export const fromColumnValue = (columnValue?: ColumnValue) => {
3939
return `'${value.toISOString()}'`;
4040
}
4141

42+
if (Array.isArray(value)) {
43+
return "'{}'";
44+
}
45+
4246
return `'${String(value)}'`;
4347
};
4448

server/src/sql-tools/schema-diff.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,20 @@ describe(schemaDiff.name, () => {
394394

395395
expect(diff.items).toEqual([]);
396396
});
397+
398+
it('should support arrays, ignoring types', () => {
399+
const diff = schemaDiff(
400+
fromColumn({ name: 'column1', type: 'character varying', isArray: true, default: "'{}'" }),
401+
fromColumn({
402+
name: 'column1',
403+
type: 'character varying',
404+
isArray: true,
405+
default: "'{}'::character varying[]",
406+
}),
407+
);
408+
409+
expect(diff.items).toEqual([]);
410+
});
397411
});
398412
});
399413

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
2+
3+
@Table()
4+
export class Table1 {
5+
@Column({ type: 'character varying', array: true, default: [] })
6+
column1!: string[];
7+
}
8+
9+
export const description = 'should register a table with a column with a default value (array)';
10+
export const schema: DatabaseSchema = {
11+
databaseName: 'postgres',
12+
schemaName: 'public',
13+
functions: [],
14+
enums: [],
15+
extensions: [],
16+
parameters: [],
17+
overrides: [],
18+
tables: [
19+
{
20+
name: 'table1',
21+
columns: [
22+
{
23+
name: 'column1',
24+
tableName: 'table1',
25+
type: 'character varying',
26+
nullable: false,
27+
isArray: true,
28+
primary: false,
29+
synchronize: true,
30+
default: "'{}'",
31+
},
32+
],
33+
indexes: [],
34+
triggers: [],
35+
constraints: [],
36+
synchronize: true,
37+
},
38+
],
39+
warnings: [],
40+
};

0 commit comments

Comments
 (0)