Skip to content

High performance self-hosted photo and video management solution.

feat: sql tools array as default value (#24389)

Daniel Dietzler75a7c9c06c81
diff --git a/server/src/sql-tools/decorators/column.decorator.ts b/server/src/sql-tools/decorators/column.decorator.ts
index adb3d0ed59..e5a0eb52f8 100644
--- a/server/src/sql-tools/decorators/column.decorator.ts
+++ b/server/src/sql-tools/decorators/column.decorator.ts
@@ -2,7 +2,7 @@ import { asOptions } from 'src/sql-tools/helpers';
import { register } from 'src/sql-tools/register';
import { ColumnStorage, ColumnType, DatabaseEnum } from 'src/sql-tools/types';
-export type ColumnValue = null | boolean | string | number | object | Date | (() => string);
+export type ColumnValue = null | boolean | string | number | Array<unknown> | object | Date | (() => string);
export type ColumnBaseOptions = {
name?: string;
diff --git a/server/src/sql-tools/helpers.ts b/server/src/sql-tools/helpers.ts
index 2ef35ce9ba..e0daf8262f 100644
--- a/server/src/sql-tools/helpers.ts
+++ b/server/src/sql-tools/helpers.ts
@@ -39,6 +39,10 @@ export const fromColumnValue = (columnValue?: ColumnValue) => {
return `'${value.toISOString()}'`;
}
+ if (Array.isArray(value)) {
+ return "'{}'";
+ }
+
return `'${String(value)}'`;
};
diff --git a/server/src/sql-tools/schema-diff.spec.ts b/server/src/sql-tools/schema-diff.spec.ts
index fe249b4e29..f45fb98bd3 100644
--- a/server/src/sql-tools/schema-diff.spec.ts
+++ b/server/src/sql-tools/schema-diff.spec.ts
@@ -394,6 +394,20 @@ describe(schemaDiff.name, () => {
expect(diff.items).toEqual([]);
});
+
+ it('should support arrays, ignoring types', () => {
+ const diff = schemaDiff(
+ fromColumn({ name: 'column1', type: 'character varying', isArray: true, default: "'{}'" }),
+ fromColumn({
+ name: 'column1',
+ type: 'character varying',
+ isArray: true,
+ default: "'{}'::character varying[]",
+ }),
+ );
+
+ expect(diff.items).toEqual([]);
+ });
});
});
diff --git a/server/test/sql-tools/column-default-array.stub.ts b/server/test/sql-tools/column-default-array.stub.ts
new file mode 100644
index 0000000000..b5e9b7d04a
--- /dev/null
+++ b/server/test/sql-tools/column-default-array.stub.ts
@@ -0,0 +1,40 @@
+import { Column, DatabaseSchema, Table } from 'src/sql-tools';
+
+@Table()
+export class Table1 {
+ @Column({ type: 'character varying', array: true, default: [] })
+ column1!: string[];
+}
+
+export const description = 'should register a table with a column with a default value (array)';
+export const schema: DatabaseSchema = {
+ databaseName: 'postgres',
+ schemaName: 'public',
+ functions: [],
+ enums: [],
+ extensions: [],
+ parameters: [],
+ overrides: [],
+ tables: [
+ {
+ name: 'table1',
+ columns: [
+ {
+ name: 'column1',
+ tableName: 'table1',
+ type: 'character varying',
+ nullable: false,
+ isArray: true,
+ primary: false,
+ synchronize: true,
+ default: "'{}'",
+ },
+ ],
+ indexes: [],
+ triggers: [],
+ constraints: [],
+ synchronize: true,
+ },
+ ],
+ warnings: [],
+};