Detect autoincrement for CockroachDB

This commit is contained in:
ian
2022-08-30 03:19:23 +08:00
parent 7974dc275e
commit 816c998291
4 changed files with 21 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import { KNEX_TYPES } from '@directus/shared/constants';
import { Field } from '@directus/shared/types';
import { Options, SchemaHelper } from '../types';
export class SchemaHelperCockroachDb extends SchemaHelper {
@@ -10,4 +11,15 @@ export class SchemaHelperCockroachDb extends SchemaHelper {
): Promise<void> {
await this.changeToTypeByCopy(table, column, type, options);
}
processField(field: Field): void {
if (
field.schema?.default_value &&
['integer', 'bigInteger'].includes(field.type) &&
typeof field.schema.default_value === 'string' &&
field.schema.default_value.startsWith('nextval(')
) {
field.schema.has_auto_increment = true;
}
}
}

View File

@@ -1,5 +1,5 @@
import { KNEX_TYPES } from '@directus/shared/constants';
import { Field, Type } from '@directus/shared/types';
import { Field } from '@directus/shared/types';
import { Options, SchemaHelper } from '../types';
export class SchemaHelperOracle extends SchemaHelper {
@@ -12,17 +12,15 @@ export class SchemaHelperOracle extends SchemaHelper {
await this.changeToTypeByCopy(table, column, type, options);
}
processFieldType(field: Field): Type {
processField(field: Field): void {
if (field.type === 'integer') {
if (field.schema?.numeric_precision === 20) {
return 'bigInteger';
field.type = 'bigInteger';
} else if (field.schema?.numeric_precision === 1) {
return 'boolean';
field.type = 'boolean';
} else if (field.schema?.numeric_precision || field.schema?.numeric_scale) {
return 'decimal';
field.type = 'decimal';
}
}
return field.type;
}
}

View File

@@ -1,7 +1,7 @@
import { getDatabaseClient } from '../../index';
import { DatabaseHelper } from '../types';
import { KNEX_TYPES } from '@directus/shared/constants';
import { Field, Type } from '@directus/shared/types';
import { Field } from '@directus/shared/types';
type Clients = 'mysql' | 'postgres' | 'cockroachdb' | 'sqlite' | 'oracle' | 'mssql' | 'redshift';
@@ -92,7 +92,7 @@ export abstract class SchemaHelper extends DatabaseHelper {
return;
}
processFieldType(field: Field): Type {
return field.type;
processField(_field: Field): void {
return;
}
}

View File

@@ -178,7 +178,7 @@ export class FieldsService {
field.type = 'dateTime';
}
field.type = this.helpers.schema.processFieldType(field);
this.helpers.schema.processField(field);
}
return result;