(
table: Knex.CreateTableBuilder,
collection: string,
field: RawField | Field,
options?: { attemptConcurrentIndex?: boolean; existing?: Column | null },
)
| 903 | } |
| 904 | |
| 905 | public addColumnToTable( |
| 906 | table: Knex.CreateTableBuilder, |
| 907 | collection: string, |
| 908 | field: RawField | Field, |
| 909 | options?: { attemptConcurrentIndex?: boolean; existing?: Column | null }, |
| 910 | ): void { |
| 911 | let column: Knex.ColumnBuilder; |
| 912 | |
| 913 | // Don't attempt to add a DB column for alias / corrupt fields |
| 914 | if (field.type === 'alias' || field.type === 'unknown') return; |
| 915 | |
| 916 | const existing = options?.existing ?? null; |
| 917 | |
| 918 | if (field.schema?.has_auto_increment) { |
| 919 | if (field.type === 'bigInteger') { |
| 920 | column = table.bigIncrements(field.field); |
| 921 | } else { |
| 922 | column = table.increments(field.field); |
| 923 | } |
| 924 | } else if (field.type === 'string') { |
| 925 | // Use !== undefined so an explicit null (meaning MAX / unbounded) is not swallowed by ??. |
| 926 | const maxLength = |
| 927 | field.schema?.max_length !== undefined ? field.schema.max_length : (existing?.max_length ?? undefined); |
| 928 | |
| 929 | // On MSSQL, null max_length means nvarchar(MAX). Knex's table.text() is the correct way |
| 930 | // to emit that; table.string(col, null) falls back to nvarchar(255). |
| 931 | if (maxLength === null && this.helpers.schema.isOneOfClients(['mssql'])) { |
| 932 | column = table.text(field.field); |
| 933 | } else { |
| 934 | column = table.string(field.field, maxLength ?? undefined); |
| 935 | } |
| 936 | } else if (['float', 'decimal'].includes(field.type)) { |
| 937 | const type = field.type as 'float' | 'decimal'; |
| 938 | |
| 939 | column = table[type]( |
| 940 | field.field, |
| 941 | field.schema?.numeric_precision ?? existing?.numeric_precision ?? DEFAULT_NUMERIC_PRECISION, |
| 942 | field.schema?.numeric_scale ?? existing?.numeric_scale ?? DEFAULT_NUMERIC_SCALE, |
| 943 | ); |
| 944 | } else if (field.type === 'csv') { |
| 945 | column = table.text(field.field); |
| 946 | } else if (field.type === 'hash') { |
| 947 | column = table.string(field.field, 255); |
| 948 | } else if (field.type === 'dateTime') { |
| 949 | column = table.dateTime(field.field, { useTz: false }); |
| 950 | } else if (field.type === 'timestamp') { |
| 951 | column = table.timestamp(field.field, { useTz: true }); |
| 952 | } else if (field.type.startsWith('geometry')) { |
| 953 | column = this.helpers.st.createColumn(table, field); |
| 954 | } else if (KNEX_TYPES.includes(field.type as (typeof KNEX_TYPES)[number])) { |
| 955 | column = table[field.type as (typeof KNEX_TYPES)[number]](field.field); |
| 956 | } else { |
| 957 | throw new InvalidPayloadError({ reason: `Illegal type passed: "${field.type}"` }); |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * The column nullability must be set on every alter or it will be dropped |
| 962 | * This is due to column.alter() not being incremental per https://knexjs.org/guide/schema-builder.html#alter |
no test coverage detected