(column: Column, table: DatabaseTable, changedProperties: Set<string>)
| 678 | } |
| 679 | |
| 680 | alterTableColumn(column: Column, table: DatabaseTable, changedProperties: Set<string>): string[] { |
| 681 | const sql: string[] = []; |
| 682 | const typeChanged = changedProperties.has('type') || changedProperties.has('collation'); |
| 683 | const defaultChanged = changedProperties.has('default'); |
| 684 | // Postgres can't implicitly cast a stored DEFAULT across array element types |
| 685 | // (e.g. `'{}'::text[]` -> `numeric(3,2)[]`), so drop+re-set it around ALTER TYPE. |
| 686 | const fromColumn = typeChanged ? table.getColumn(column.name) : undefined; |
| 687 | const needsArrayDefaultRecast = |
| 688 | typeChanged && |
| 689 | fromColumn?.default != null && |
| 690 | fromColumn.type.endsWith(']') && |
| 691 | column.type.endsWith(']') && |
| 692 | fromColumn.type !== column.type; |
| 693 | const recastDefault = needsArrayDefaultRecast && !defaultChanged; |
| 694 | |
| 695 | if ((defaultChanged && column.default == null) || needsArrayDefaultRecast) { |
| 696 | sql.push(`alter table ${table.getQuotedName()} alter column ${this.quote(column.name)} drop default`); |
| 697 | } |
| 698 | |
| 699 | if (typeChanged) { |
| 700 | let type = column.type + (column.generated ? ` generated always as ${column.generated}` : ''); |
| 701 | |
| 702 | if (column.nativeEnumName) { |
| 703 | const parts = type.split('.'); |
| 704 | |
| 705 | if (parts.length === 2 && parts[0] === '*' && table.schema) { |
| 706 | type = `${table.schema}.${parts[1]}`; |
| 707 | } else if (parts.length === 1) { |
| 708 | type = this.getTableName(type, table.schema); |
| 709 | } |
| 710 | |
| 711 | type = this.quote(type); |
| 712 | } |
| 713 | |
| 714 | const collateClause = column.collation ? ` ${this.getCollateSQL(column.collation)}` : ''; |
| 715 | |
| 716 | sql.push( |
| 717 | `alter table ${table.getQuotedName()} alter column ${this.quote(column.name)} type ${type + collateClause + this.castColumn(column.name, type)}`, |
| 718 | ); |
| 719 | } |
| 720 | |
| 721 | if ((defaultChanged && column.default != null) || recastDefault) { |
| 722 | sql.push( |
| 723 | `alter table ${table.getQuotedName()} alter column ${this.quote(column.name)} set default ${column.default}`, |
| 724 | ); |
| 725 | } |
| 726 | |
| 727 | if (changedProperties.has('nullable')) { |
| 728 | const action = column.nullable ? 'drop' : 'set'; |
| 729 | sql.push(`alter table ${table.getQuotedName()} alter column ${this.quote(column.name)} ${action} not null`); |
| 730 | } |
| 731 | |
| 732 | return sql; |
| 733 | } |
| 734 | |
| 735 | /** Returns the bare `collate <name>` clause for column DDL. Overridden by PostgreSQL to quote the identifier. */ |
| 736 | protected getCollateSQL(collation: string): string { |
nothing calls this directly
no test coverage detected