Skip to content

Commit

Permalink
[OracleDB] Extended schema-inspector to add AUTO_INCREMENT to columnI…
Browse files Browse the repository at this point in the history
…nfo (directus#5408)

* Extended knex to add AUTO_INCREMENT to oracle columnInfo

* TS: Added missing return type
  • Loading branch information
aidenfoxx authored May 3, 2021
1 parent c653b16 commit f9a01d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"typescript": "^4.0.5"
},
"dependencies": {
"knex-schema-inspector": "^1.2.0",
"knex-schema-inspector": "^1.3.0",
"lodash": "^4.17.21"
},
"gitHead": "71bf628955b5da15ce3070dc09478bc558f243a4"
Expand Down
37 changes: 30 additions & 7 deletions packages/schema/src/dialects/oracledb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import KnexOracle from 'knex-schema-inspector/dist/dialects/oracledb';
import { Column } from 'knex-schema-inspector/dist/types/column';
import { SchemaOverview } from '../types/overview';
import { SchemaInspector } from '../types/schema';
import { mapKeys } from 'lodash';

export default class Oracle extends KnexOracle implements SchemaInspector {
private static _mapColumnAutoIncrement(column: Column): Column {
// Oracle doesn't support AUTO_INCREMENT. Assume all numeric primary
// keys without a default are AUTO_INCREMENT
const hasAutoIncrement = !column.default_value && column.data_type === 'NUMBER' && column.is_primary_key;

return {
...column,
default_value: hasAutoIncrement ? 'AUTO_INCREMENT' : column.default_value,
has_auto_increment: hasAutoIncrement,
};
}

columnInfo(): Promise<Column[]>;
columnInfo(table: string): Promise<Column[]>;
columnInfo(table: string, column: string): Promise<Column>;
async columnInfo(table?: string, column?: string): Promise<Column | Column[]> {
if (column) {
const columnInfo: Column = await super.columnInfo(table!, column!);
return Oracle._mapColumnAutoIncrement(columnInfo);
}

const columnInfo: Column[] = await super.columnInfo(table!);
return columnInfo.map(Oracle._mapColumnAutoIncrement);
}

async overview(): Promise<SchemaOverview> {
type RawColumn = {
TABLE_NAME: string;
Expand Down Expand Up @@ -61,17 +87,14 @@ export default class Oracle extends KnexOracle implements SchemaInspector {
};
}

/**
* Oracle doesn't return AUTO_INCREMENT. Incrementing is done using triggers, and there is no
* nice way to detect if a trigger is an increment trigger. For compatibility sake, assume all
* numeric primary keys AUTO_INCREMENT to prevent authorization throwing a "required value" error.
*/
const isNumericPrimary = column.data_type === 'NUMBER' && overview[column.table_name].primary;
// Oracle doesn't support AUTO_INCREMENT. Assume all numeric primary
// keys without a default are AUTO_INCREMENT
const hasAutoIncrement = !column.default_value && column.data_type === 'NUMBER' && column.column_key === 'P';

overview[column.table_name].columns[column.column_name] = {
...column,
is_nullable: column.is_nullable === 'Y',
default_value: !column.default_value && isNumericPrimary ? 'AUTO_INCREMENT' : column.default_value,
default_value: hasAutoIncrement ? 'AUTO_INCREMENT' : column.default_value,
};
}

Expand Down

0 comments on commit f9a01d6

Please sign in to comment.