mirror of
https://github.com/directus/directus.git
synced 2026-01-23 04:58:00 -05:00
Added correct null parsing to Directus schemas (#13873)
* Added correct null parsing to Directus schemas * Bump knex-schema-inspector To make sure we don't upset MariaDB folks Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -128,7 +128,7 @@
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"keyv": "^4.3.0",
|
||||
"knex": "^2.1.0",
|
||||
"knex-schema-inspector": "^2.0.0",
|
||||
"knex-schema-inspector": "^2.0.1",
|
||||
"ldapjs": "^2.3.3",
|
||||
"liquidjs": "^9.37.0",
|
||||
"lodash": "^4.17.21",
|
||||
|
||||
@@ -588,11 +588,6 @@ export class FieldsService {
|
||||
) {
|
||||
const precision = field.schema.default_value.match(REGEX_BETWEEN_PARENS)![1];
|
||||
column.defaultTo(this.knex.fn.now(Number(precision)));
|
||||
} else if (
|
||||
typeof field.schema.default_value === 'string' &&
|
||||
['"null"', 'null'].includes(field.schema.default_value.toLowerCase())
|
||||
) {
|
||||
column.defaultTo(null);
|
||||
} else {
|
||||
column.defaultTo(field.schema.default_value);
|
||||
}
|
||||
|
||||
@@ -10,20 +10,8 @@ export default function getDefaultValue(
|
||||
): string | boolean | number | Record<string, any> | any[] | null {
|
||||
const type = getLocalType(column);
|
||||
|
||||
let defaultValue = column.default_value ?? null;
|
||||
const defaultValue = column.default_value ?? null;
|
||||
if (defaultValue === null) return null;
|
||||
if (defaultValue === 'null') return null;
|
||||
if (defaultValue === 'NULL') return null;
|
||||
|
||||
// Check if the default is wrapped in an extra pair of quotes, this happens in SQLite / MariaDB
|
||||
if (
|
||||
typeof defaultValue === 'string' &&
|
||||
((defaultValue.startsWith(`'`) && defaultValue.endsWith(`'`)) ||
|
||||
(defaultValue.startsWith(`"`) && defaultValue.endsWith(`"`)))
|
||||
) {
|
||||
defaultValue = defaultValue.slice(1, -1);
|
||||
}
|
||||
|
||||
if (defaultValue === '0000-00-00 00:00:00') return null;
|
||||
|
||||
switch (type) {
|
||||
|
||||
684
package-lock.json
generated
684
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -47,7 +47,7 @@
|
||||
"typescript": "4.5.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"knex-schema-inspector": "^2.0.0",
|
||||
"knex-schema-inspector": "^2.0.1",
|
||||
"lodash": "^4.17.21"
|
||||
},
|
||||
"gitHead": "24621f3934dc77eb23441331040ed13c676ceffd"
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import KnexOracle from 'knex-schema-inspector/dist/dialects/oracledb';
|
||||
import KnexOracle, { parseDefaultValue } 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 { stripQuotes } from '../utils/strip-quotes';
|
||||
|
||||
export default class Oracle extends KnexOracle implements SchemaInspector {
|
||||
private static _mapColumnAutoIncrement(column: Column): Column {
|
||||
@@ -102,7 +101,7 @@ export default class Oracle extends KnexOracle implements SchemaInspector {
|
||||
...column,
|
||||
is_nullable: column.is_nullable === 'Y',
|
||||
is_generated: column.is_generated === 'YES',
|
||||
default_value: hasAutoIncrement ? 'AUTO_INCREMENT' : stripQuotes(column.default_value),
|
||||
default_value: hasAutoIncrement ? 'AUTO_INCREMENT' : parseDefaultValue(column.default_value),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import KnexSQLite from 'knex-schema-inspector/dist/dialects/sqlite';
|
||||
import KnexSQLite, { parseDefaultValue } from 'knex-schema-inspector/dist/dialects/sqlite';
|
||||
import extractMaxLength from 'knex-schema-inspector/dist/utils/extract-max-length';
|
||||
import extractType from 'knex-schema-inspector/dist/utils/extract-type';
|
||||
import { SchemaOverview } from '../types/overview';
|
||||
import { SchemaInspector } from '../types/schema';
|
||||
import { stripQuotes } from '../utils/strip-quotes';
|
||||
|
||||
type RawColumn = {
|
||||
cid: number;
|
||||
@@ -42,7 +41,7 @@ export default class SQLite extends KnexSQLite implements SchemaInspector {
|
||||
default_value:
|
||||
column.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table)
|
||||
? 'AUTO_INCREMENT'
|
||||
: stripQuotes(column.dflt_value),
|
||||
: parseDefaultValue(column.dflt_value),
|
||||
is_nullable: column.notnull == 0,
|
||||
is_generated: column.hidden !== 0,
|
||||
data_type: extractType(column.type),
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Strip leading/trailing quotes from a string
|
||||
*/
|
||||
export function stripQuotes(value: string | null): string | null {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const trimmed = value.trim();
|
||||
|
||||
if ((trimmed.startsWith(`'`) && trimmed.endsWith(`'`)) || (trimmed.startsWith('"') && trimmed.endsWith('"'))) {
|
||||
return trimmed.slice(1, -1);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user