-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcreate-database-column-field-visitor.postgres.ts
More file actions
456 lines (403 loc) · 15.3 KB
/
create-database-column-field-visitor.postgres.ts
File metadata and controls
456 lines (403 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import type {
AttachmentFieldCore,
AutoNumberFieldCore,
ButtonFieldCore,
CheckboxFieldCore,
ColorFieldCore,
ConditionalRollupFieldCore,
CreatedByFieldCore,
CreatedTimeFieldCore,
DateFieldCore,
FieldCore,
FormulaFieldCore,
IFieldVisitor,
ILinkFieldOptions,
LastModifiedByFieldCore,
LastModifiedTimeFieldCore,
LinkFieldCore,
LongTextFieldCore,
MultipleSelectFieldCore,
NumberFieldCore,
RatingFieldCore,
RollupFieldCore,
SingleLineTextFieldCore,
SingleSelectFieldCore,
UserFieldCore,
} from '@teable/core';
import { DbFieldType, Relationship } from '@teable/core';
import type { Knex } from 'knex';
import type { AutoNumberFieldDto } from '../../features/field/model/field-dto/auto-number-field.dto';
import type { CreatedByFieldDto } from '../../features/field/model/field-dto/created-by-field.dto';
import type { CreatedTimeFieldDto } from '../../features/field/model/field-dto/created-time-field.dto';
import type { FormulaFieldDto } from '../../features/field/model/field-dto/formula-field.dto';
import type { LastModifiedByFieldDto } from '../../features/field/model/field-dto/last-modified-by-field.dto';
import type { LastModifiedTimeFieldDto } from '../../features/field/model/field-dto/last-modified-time-field.dto';
import type { LinkFieldDto } from '../../features/field/model/field-dto/link-field.dto';
import { SchemaType } from '../../features/field/util';
import type { IFormulaConversionContext } from '../../features/record/query-builder/sql-conversion.visitor';
import { GeneratedColumnQuerySupportValidatorPostgres } from '../generated-column-query/postgres/generated-column-query-support-validator.postgres';
import type { ICreateDatabaseColumnContext } from './create-database-column-field-visitor.interface';
import { validateGeneratedColumnSupport } from './create-database-column-field.util';
/**
* PostgreSQL implementation of database column visitor.
*/
export class CreatePostgresDatabaseColumnFieldVisitor implements IFieldVisitor<void> {
private sql: string[] = [];
constructor(private readonly context: ICreateDatabaseColumnContext) {}
getSql(): string[] {
return this.sql;
}
private getSchemaType(dbFieldType: DbFieldType): SchemaType {
switch (dbFieldType) {
case DbFieldType.Blob:
return SchemaType.Binary;
case DbFieldType.Integer:
return SchemaType.Integer;
case DbFieldType.Json:
// PostgreSQL supports native JSONB
return SchemaType.Jsonb;
case DbFieldType.Real:
return SchemaType.Double;
case DbFieldType.Text:
return SchemaType.Text;
case DbFieldType.DateTime:
return SchemaType.Datetime;
case DbFieldType.Boolean:
return SchemaType.Boolean;
default:
throw new Error(`Unsupported DbFieldType: ${dbFieldType}`);
}
}
private createStandardColumn(field: FieldCore): void {
const schemaType = this.getSchemaType(field.dbFieldType);
const column = this.context.table[schemaType](this.context.dbFieldName);
if (this.context.notNull) {
column.notNullable();
}
if (this.context.unique) {
column.unique();
}
}
private createFormulaColumns(field: FormulaFieldCore): void {
const formulaFieldDto = this.context.field as FormulaFieldDto;
const clearPersistedGeneratedMeta = () => {
formulaFieldDto.meta = undefined;
};
// Never persist lookup formulas as generated columns; they may be multi-valued (JSON)
// and/or depend on link/lookup resolution logic not suitable for generated columns.
if (field.isLookup || field.isMultipleCellValue) {
clearPersistedGeneratedMeta();
this.createStandardColumn(field);
return;
}
if (this.context.dbProvider) {
const generatedColumnName = field.getGeneratedColumnName();
const columnType = this.getPostgresColumnType(field.dbFieldType);
const expression = field.getExpression();
// Skip if no expression
if (!expression) {
// Fallback to a standard column if no expression
clearPersistedGeneratedMeta();
this.createStandardColumn(field);
return;
}
// Check if the formula is supported for generated columns
const supportValidator = new GeneratedColumnQuerySupportValidatorPostgres();
const isSupported = validateGeneratedColumnSupport(
field,
supportValidator,
this.context.tableDomain
);
if (isSupported) {
const conversionContext: IFormulaConversionContext = {
table: this.context.tableDomain,
isGeneratedColumn: true, // Mark this as a generated column context
};
const conversionResult = this.context.dbProvider.convertFormulaToGeneratedColumn(
expression,
conversionContext
);
// Create generated column using specificType
// PostgreSQL syntax: GENERATED ALWAYS AS (expression) STORED
const generatedColumnDefinition = `${columnType} GENERATED ALWAYS AS (${conversionResult.sql}) STORED`;
this.context.table.specificType(generatedColumnName, generatedColumnDefinition);
(this.context.field as FormulaFieldDto).setMetadata({ persistedAsGeneratedColumn: true });
return;
}
}
// Fallback: create a standard column when not supported as generated
clearPersistedGeneratedMeta();
this.createStandardColumn(field);
}
private getPostgresColumnType(dbFieldType: DbFieldType): string {
switch (dbFieldType) {
case DbFieldType.Text:
return 'TEXT';
case DbFieldType.Integer:
return 'INTEGER';
case DbFieldType.Real:
return 'DOUBLE PRECISION';
case DbFieldType.Boolean:
return 'BOOLEAN';
case DbFieldType.DateTime:
return 'TIMESTAMP';
case DbFieldType.Json:
return 'JSONB';
case DbFieldType.Blob:
return 'BYTEA';
default:
return 'TEXT';
}
}
// Basic field types
visitNumberField(field: NumberFieldCore): void {
this.createStandardColumn(field);
}
visitSingleLineTextField(field: SingleLineTextFieldCore): void {
this.createStandardColumn(field);
}
visitLongTextField(field: LongTextFieldCore): void {
this.createStandardColumn(field);
}
visitAttachmentField(field: AttachmentFieldCore): void {
this.createStandardColumn(field);
}
visitCheckboxField(field: CheckboxFieldCore): void {
this.createStandardColumn(field);
}
visitDateField(field: DateFieldCore): void {
this.createStandardColumn(field);
}
visitRatingField(field: RatingFieldCore): void {
this.createStandardColumn(field);
}
visitAutoNumberField(_field: AutoNumberFieldCore): void {
this.context.table.specificType(
this.context.dbFieldName,
'INTEGER GENERATED ALWAYS AS (__auto_number) STORED'
);
(this.context.field as AutoNumberFieldDto).setMetadata({
persistedAsGeneratedColumn: true,
});
}
visitLinkField(field: LinkFieldCore): void {
// Determine potential conflicts with FK column names (including inferred defaults)
const opts = field.options as ILinkFieldOptions;
const conflictNames = new Set<string>();
const rel = opts?.relationship;
const inferredFkName =
opts?.foreignKeyName ??
(rel === Relationship.ManyOne || rel === Relationship.OneOne
? this.context.dbFieldName
: undefined);
const inferredSelfName =
opts?.selfKeyName ??
(rel === Relationship.OneMany && opts?.isOneWay === false
? this.context.dbFieldName
: undefined);
if (inferredFkName) conflictNames.add(inferredFkName);
if (inferredSelfName) conflictNames.add(inferredSelfName);
// Create underlying base column only if no conflict with FK/self columns
if (!this.context.skipBaseColumnCreation && !conflictNames.has(this.context.dbFieldName)) {
this.createStandardColumn(field);
}
// For real link structures, create FK/junction artifacts on non-symmetric side
if (field.isLookup) return;
if (this.context.isSymmetricField || this.isSymmetricField(field)) return;
this.createForeignKeyForLinkField(field);
}
private isSymmetricField(_field: LinkFieldCore): boolean {
// A field is symmetric if it has a symmetricFieldId that points to an existing field
// In practice, when creating symmetric fields, they are created after the main field
// So we can check if this field's symmetricFieldId exists in the database
// For now, we'll rely on the isSymmetricField context flag
return false;
}
private createForeignKeyForLinkField(field: LinkFieldCore): void {
const options = field.options as ILinkFieldOptions;
const { relationship, fkHostTableName, selfKeyName, foreignKeyName, isOneWay, foreignTableId } =
options;
if (
!this.context.knex ||
!this.context.tableId ||
!this.context.tableName ||
!this.context.tableNameMap
) {
return;
}
// Get table names from context
const dbTableName = this.context.tableName;
const foreignDbTableName = this.context.tableNameMap.get(foreignTableId);
if (!foreignDbTableName) {
throw new Error(`Foreign table not found: ${foreignTableId}`);
}
let alterTableSchema: Knex.SchemaBuilder | undefined;
if (relationship === Relationship.ManyMany) {
alterTableSchema = this.context.knex.schema.createTable(fkHostTableName, (table) => {
table.increments('__id').primary();
table
.string(selfKeyName)
.references('__id')
.inTable(dbTableName)
.withKeyName(`fk_${selfKeyName}`);
table
.string(foreignKeyName)
.references('__id')
.inTable(foreignDbTableName)
.withKeyName(`fk_${foreignKeyName}`);
// Add order column for maintaining insertion order
table.integer('__order').nullable();
});
// Set metadata to indicate this field has order column
(this.context.field as LinkFieldDto).setMetadata({ hasOrderColumn: true });
}
if (relationship === Relationship.ManyOne) {
alterTableSchema = this.context.knex.schema.alterTable(fkHostTableName, (table) => {
table
.string(foreignKeyName)
.references('__id')
.inTable(foreignDbTableName)
.withKeyName(`fk_${foreignKeyName}`);
// Add order column for maintaining insertion order
table.integer(`${foreignKeyName}_order`).nullable();
});
// Set metadata to indicate this field has order column
(this.context.field as LinkFieldDto).setMetadata({ hasOrderColumn: true });
}
if (relationship === Relationship.OneMany) {
if (isOneWay) {
alterTableSchema = this.context.knex.schema.createTable(fkHostTableName, (table) => {
table.increments('__id').primary();
table
.string(selfKeyName)
.references('__id')
.inTable(dbTableName)
.withKeyName(`fk_${selfKeyName}`);
table.string(foreignKeyName).references('__id').inTable(foreignDbTableName);
table.unique([selfKeyName, foreignKeyName], {
indexName: `index_${selfKeyName}_${foreignKeyName}`,
});
});
} else {
alterTableSchema = this.context.knex.schema.alterTable(fkHostTableName, (table) => {
table
.string(selfKeyName)
.references('__id')
.inTable(dbTableName)
.withKeyName(`fk_${selfKeyName}`);
// Add order column for maintaining insertion order
table.integer(`${selfKeyName}_order`).nullable();
});
// Set metadata to indicate this field has order column
(this.context.field as LinkFieldDto).setMetadata({ hasOrderColumn: true });
}
}
// assume options is from the main field (user created one)
if (relationship === Relationship.OneOne) {
alterTableSchema = this.context.knex.schema.alterTable(fkHostTableName, (table) => {
if (foreignKeyName === '__id') {
throw new Error('can not use __id for foreignKeyName');
}
table.string(foreignKeyName).references('__id').inTable(foreignDbTableName);
table.unique([foreignKeyName], {
indexName: `index_${foreignKeyName}`,
});
// Add order column for maintaining insertion order
table.integer(`${foreignKeyName}_order`).nullable();
});
// Set metadata to indicate this field has order column
(this.context.field as LinkFieldDto).setMetadata({ hasOrderColumn: true });
}
if (!alterTableSchema) {
throw new Error('alterTableSchema is undefined');
}
// Store the SQL queries to be executed later
for (const sql of alterTableSchema.toSQL()) {
this.sql.push(sql.sql);
}
}
visitRollupField(field: RollupFieldCore): void {
// Always create an underlying base column for rollup fields
this.createStandardColumn(field);
}
visitConditionalRollupField(field: ConditionalRollupFieldCore): void {
this.createStandardColumn(field);
}
// Select field types
visitSingleSelectField(field: SingleSelectFieldCore): void {
this.createStandardColumn(field);
}
visitMultipleSelectField(field: MultipleSelectFieldCore): void {
this.createStandardColumn(field);
}
visitButtonField(field: ButtonFieldCore): void {
this.createStandardColumn(field);
}
visitColorField(field: ColorFieldCore): void {
this.createStandardColumn(field);
}
// Formula field types
visitFormulaField(field: FormulaFieldCore): void {
this.createFormulaColumns(field);
}
visitCreatedTimeField(field: CreatedTimeFieldCore): void {
if (field.isLookup) {
this.createStandardColumn(field);
return;
}
this.context.table.specificType(
this.context.dbFieldName,
'TIMESTAMP GENERATED ALWAYS AS (__created_time) STORED'
);
(this.context.field as CreatedTimeFieldDto).setMetadata({
persistedAsGeneratedColumn: true,
});
}
visitLastModifiedTimeField(field: LastModifiedTimeFieldCore): void {
if (field.isLookup) {
this.createStandardColumn(field);
return;
}
const trackAll = field.isTrackAll();
if (trackAll) {
this.context.table.specificType(
this.context.dbFieldName,
'TIMESTAMP GENERATED ALWAYS AS (__last_modified_time) STORED'
);
(this.context.field as LastModifiedTimeFieldDto).setMetadata({
persistedAsGeneratedColumn: true,
});
return;
}
this.context.table.timestamp(this.context.dbFieldName, { useTz: true });
(this.context.field as LastModifiedTimeFieldDto).setMetadata({
persistedAsGeneratedColumn: false,
});
}
// User field types
visitUserField(field: UserFieldCore): void {
this.createStandardColumn(field);
}
visitCreatedByField(field: CreatedByFieldCore): void {
if (field.isLookup) {
this.createStandardColumn(field);
return;
}
// Persist as a JSON column (stores collaborator payload)
this.createStandardColumn(field);
(this.context.field as CreatedByFieldDto).setMetadata({
persistedAsGeneratedColumn: false,
});
}
visitLastModifiedByField(field: LastModifiedByFieldCore): void {
if (field.isLookup) {
this.createStandardColumn(field);
return;
}
// Persist as a JSON column (stores collaborator payload)
this.createStandardColumn(field);
(this.context.field as LastModifiedByFieldDto).setMetadata({
persistedAsGeneratedColumn: false,
});
}
}