(collection: string, field: RawField, opts?: FieldMutationOptions)
| 501 | } |
| 502 | |
| 503 | async updateField(collection: string, field: RawField, opts?: FieldMutationOptions): Promise<string> { |
| 504 | if (this.accountability && this.accountability.admin !== true) { |
| 505 | throw new ForbiddenError(); |
| 506 | } |
| 507 | |
| 508 | const runPostColumnChange = await this.helpers.schema.preColumnChange(); |
| 509 | const nestedActionEvents: ActionEventParams[] = []; |
| 510 | |
| 511 | // 'type' is required for further checks on schema update |
| 512 | if (field.schema && !field.type) { |
| 513 | const existingType = this.schema.collections[collection]?.fields[field.field]?.type; |
| 514 | if (existingType) field.type = existingType; |
| 515 | } |
| 516 | |
| 517 | try { |
| 518 | const hookAdjustedField = |
| 519 | opts?.emitEvents !== false |
| 520 | ? await emitter.emitFilter( |
| 521 | `fields.update`, |
| 522 | field, |
| 523 | { |
| 524 | keys: [field.field], |
| 525 | collection: collection, |
| 526 | }, |
| 527 | { |
| 528 | database: this.knex, |
| 529 | schema: this.schema, |
| 530 | accountability: this.accountability, |
| 531 | }, |
| 532 | ) |
| 533 | : field; |
| 534 | |
| 535 | const record = field.meta |
| 536 | ? await this.knex.select('id').from('directus_fields').where({ collection, field: field.field }).first() |
| 537 | : null; |
| 538 | |
| 539 | if ( |
| 540 | hookAdjustedField.type && |
| 541 | (hookAdjustedField.type === 'alias' || |
| 542 | this.schema.collections[collection]!.fields[field.field]?.type === 'alias') && |
| 543 | hookAdjustedField.type !== (this.schema.collections[collection]!.fields[field.field]?.type ?? 'alias') |
| 544 | ) { |
| 545 | throw new InvalidPayloadError({ reason: 'Alias type cannot be changed' }); |
| 546 | } |
| 547 | |
| 548 | if (hookAdjustedField.schema) { |
| 549 | const existingColumn = await this.columnInfo(collection, hookAdjustedField.field); |
| 550 | |
| 551 | if (existingColumn.is_primary_key) { |
| 552 | if (hookAdjustedField.schema?.is_nullable === true) { |
| 553 | throw new InvalidPayloadError({ reason: 'Primary key cannot be null' }); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // Sanitize column only when applying snapshot diff as opts is only passed from /utils/apply-diff.ts |
| 558 | const columnToCompare = |
| 559 | opts?.bypassLimits && opts.autoPurgeSystemCache === false ? sanitizeColumn(existingColumn) : existingColumn; |
| 560 |
no test coverage detected