Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve default value parsing #17124

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface DatabaseDescription {
export interface ColumnDescription {
type: string;
allowNull: boolean;
defaultValue: string;
defaultValue: unknown;
primaryKey: boolean;
autoIncrement: boolean;
comment: string | null;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/dialects/db2/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,17 @@ export class Db2Query extends AbstractQuery {
if (this.isDescribeQuery()) {
result = {};
for (const _result of data) {
if (_result.Default) {
_result.Default = _result.Default.replace("('", '').replace("')", '').replaceAll("'", '');
}
const defaultValue = {
raw: _result.Default,
parsed: _result.Default
? _result.Default.replace("('", '').replace("')", '').replaceAll("'", '')
: undefined,
};

result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: _result.IsNull === 'Y',
defaultValue: _result.Default,
defaultValue,
primaryKey: _result.KeySeq > 0,
autoIncrement: _result.IsIdentity === 'Y',
comment: _result.Comment,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/dialects/ibmi/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ export class IBMiQuery extends AbstractQuery {
? _result.Type.replace(enumRegex, 'ENUM')
: _result.DATA_TYPE.toUpperCase(),
allowNull: _result.IS_NULLABLE === 'Y',
defaultValue: _result.COLUMN_DEFAULT,
defaultValue: {
raw: _result.COLUMN_DEFAULT,
parsed: _result.COLUMN_DEFAULT,
},
primaryKey: _result.CONSTRAINT_TYPE === 'PRIMARY KEY',
autoIncrement: _result.IS_GENERATED !== 'IDENTITY_GENERATION',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { literal } from '../../expression-builders/literal';
import type { ColumnDescription } from '../abstract/query-interface.types';

const NUMBER_TYPES = ['INT', 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'FLOAT', 'DOUBLE'];

export function parseDefaultValue(
rawDefaultValue: string | null,
field: Omit<ColumnDescription, 'defaultValue'>,
): unknown {
if (field.autoIncrement || rawDefaultValue === null) {
return undefined;
}

if (rawDefaultValue === 'NULL') {
return null;
}

if (
NUMBER_TYPES.some(type => field.type.startsWith(type)) &&
rawDefaultValue &&
!Number.isNaN(Number(rawDefaultValue))
) {
return Number(rawDefaultValue);
}

if (field.type.startsWith('DECIMAL(')) {
return rawDefaultValue;
}

if (rawDefaultValue.startsWith("'") && rawDefaultValue.endsWith("'")) {
return rawDefaultValue.slice(1, -1).replaceAll("''", "'").replaceAll('\\\\', '\\');
}

if (!Number.isNaN(Number(rawDefaultValue))) {
return rawDefaultValue;
}

return literal(rawDefaultValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ export class MariaDbQueryGeneratorTypeScript extends AbstractQueryGenerator {
}

describeTableQuery(tableName: TableOrModel) {
return `SHOW FULL COLUMNS FROM ${this.quoteTable(tableName)};`;
const table = this.extractTableDetails(tableName);

return `SELECT
TABLE_NAME AS 'Table',
COLUMN_NAME AS 'Field',
COLUMN_DEFAULT AS 'Default',
IS_NULLABLE AS 'Null',
COLUMN_TYPE AS 'Type',
EXTRA AS 'Extra',
COLUMN_COMMENT AS 'Comment',
COLUMN_KEY AS 'Key'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE ${table.schema ? `TABLE_SCHEMA = ${this.escape(table.schema)}` : ''}
AND TABLE_NAME = ${this.escape(table.tableName)}
ORDER BY TABLE_NAME, ORDINAL_POSITION`;
}

listTablesQuery(options?: ListTablesQueryOptions) {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/dialects/mariadb/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { AbstractQuery } = require('../abstract/query');
const sequelizeErrors = require('../../errors');
const DataTypes = require('../../data-types');
const { logger } = require('../../utils/logger');
const { parseDefaultValue } = require('./default-value-parser-internal');

const ER_DUP_ENTRY = 1062;
const ER_DEADLOCK = 1213;
Expand Down Expand Up @@ -154,17 +155,20 @@ export class MariaDbQuery extends AbstractQuery {
result = {};

for (const _result of data) {
result[_result.Field] = {
const field = {
type: _result.Type.toLowerCase().startsWith('enum')
? _result.Type.replace(/^enum/i, 'ENUM')
: _result.Type.toUpperCase(),
allowNull: _result.Null === 'YES',
defaultValue: _result.Default,
primaryKey: _result.Key === 'PRI',
autoIncrement:
Object.hasOwn(_result, 'Extra') && _result.Extra.toLowerCase() === 'auto_increment',
comment: _result.Comment ? _result.Comment : null,
};

field.defaultValue = parseDefaultValue(_result.Default, field);

result[_result.Field] = field;
}

return result;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/dialects/mssql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,17 @@ export class MsSqlQuery extends AbstractQuery {
if (this.isDescribeQuery()) {
const result = {};
for (const _result of data) {
if (_result.Default) {
_result.Default = _result.Default.replace("('", '').replace("')", '').replaceAll("'", '');
}
const defaultValue = {
raw: _result.Default,
parsed: _result.Default
? _result.Default.replace("('", '').replace("')", '').replaceAll("'", '')
: undefined,
};

result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: _result.IsNull === 'YES',
defaultValue: _result.Default,
defaultValue,
primaryKey: _result.Constraint === 'PRIMARY KEY',
autoIncrement: _result.IsIdentity === 1,
comment: _result.Comment,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { literal } from '../../expression-builders/literal';
import type { ColumnDescription } from '../abstract/query-interface.types';

const NUMBER_TYPES = ['INT', 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'BIGINT', 'FLOAT', 'DOUBLE'];

export function parseDefaultValue(
rawDefaultValue: string | null,
field: Omit<ColumnDescription, 'defaultValue'>,
extra: string,
): unknown {
if (extra?.toUpperCase().includes('DEFAULT_GENERATED')) {
if (rawDefaultValue) {
return literal(rawDefaultValue);
}

return undefined;
}

if (field.autoIncrement) {
return undefined;
}

if (rawDefaultValue === null) {
return null;
}

if (
NUMBER_TYPES.includes(field.type) &&
rawDefaultValue &&
!Number.isNaN(Number(rawDefaultValue))
) {
return Number(rawDefaultValue);
}

return rawDefaultValue;
}
9 changes: 7 additions & 2 deletions packages/core/src/dialects/mysql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NodeUtil from 'node:util';
import forOwn from 'lodash/forOwn';
import map from 'lodash/map';
import zipObject from 'lodash/zipObject';
import { parseDefaultValue } from './default-value-parser-internal';

const { AbstractQuery } = require('../abstract/query');
const sequelizeErrors = require('../../errors');
Expand Down Expand Up @@ -136,17 +137,21 @@ export class MySqlQuery extends AbstractQuery {

for (const _result of data) {
const enumRegex = /^enum/i;
result[_result.Field] = {

const field = {
type: enumRegex.test(_result.Type)
? _result.Type.replace(enumRegex, 'ENUM')
: _result.Type.toUpperCase(),
allowNull: _result.Null === 'YES',
defaultValue: _result.Default,
primaryKey: _result.Key === 'PRI',
autoIncrement:
Object.hasOwn(_result, 'Extra') && _result.Extra.toLowerCase() === 'auto_increment',
comment: _result.Comment ? _result.Comment : null,
};

field.defaultValue = parseDefaultValue(_result.Default, field, _result.Extra);

result[_result.Field] = field;
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { literal } from '../../expression-builders/literal';
import type { ColumnDescription } from '../abstract/query-interface.types';

export function parseDefaultValue(
rawDefaultValue: string | null,
field: Omit<ColumnDescription, 'defaultValue'>,
): unknown {
if (rawDefaultValue === null && !field.allowNull) {
return undefined;
}

if (rawDefaultValue === null || rawDefaultValue.startsWith('NULL::')) {
return null;
}

if (['true', 'false'].includes(rawDefaultValue) && field.type === 'BOOLEAN') {
return rawDefaultValue === 'true';
}

if (field.type !== 'NUMERIC') {
if (rawDefaultValue && !Number.isNaN(Number(rawDefaultValue))) {
return Number(rawDefaultValue);
}

if (rawDefaultValue.endsWith('::numeric') || rawDefaultValue.endsWith('::integer')) {
const unQuote = rawDefaultValue.replace(/^'/, '').replace(/'?::.*$/, '');

return Number(unQuote);
}
} else if (!rawDefaultValue.startsWith("'")) {
return rawDefaultValue;
}

if (
(field.type === 'JSON' && rawDefaultValue.endsWith('::json')) ||
(field.type === 'JSONB' && rawDefaultValue.endsWith('::jsonb'))
) {
const json = rawDefaultValue
.replace(/^'/, '')
.replace(/'?::jsonb?$/, '')
.replaceAll("''", "'");

return JSON.parse(json);
}

if (rawDefaultValue.startsWith("'")) {
return parseStringValue(rawDefaultValue);
}

return literal(rawDefaultValue);
}

function parseStringValue(rawDefaultValue: string): string | undefined {
let buffer = '';

for (let i = 1; i < rawDefaultValue.length; i += 1) {
const char = rawDefaultValue[i];

if (char === "'") {
if (rawDefaultValue[i + 1] === "'") {
i += 1;
} else {
return buffer;
}
}

buffer += char;
}

// Unable to parse string, return undefined
return undefined;
}
25 changes: 4 additions & 21 deletions packages/core/src/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { AbstractQuery } = require('../abstract/query');
const { QueryTypes } = require('../../query-types');
const sequelizeErrors = require('../../errors');
const { logger } = require('../../utils/logger');
const { parseDefaultValue } = require('./default-value-parser-internal');

const debug = logger.debugContext('sql:pg');

Expand Down Expand Up @@ -222,35 +223,17 @@ export class PostgresQuery extends AbstractQuery {
const result = {};

for (const row of rows) {
result[row.Field] = {
const field = {
type: row.Type.toUpperCase(),
allowNull: row.Null === 'YES',
defaultValue: row.Default,
comment: row.Comment,
special: row.special ? this.sequelize.queryGenerator.fromArray(row.special) : [],
primaryKey: row.Constraint === 'PRIMARY KEY',
};

if (result[row.Field].type === 'BOOLEAN') {
result[row.Field].defaultValue = { false: false, true: true }[
result[row.Field].defaultValue
];
field.defaultValue = parseDefaultValue(row.Default, field);

if (result[row.Field].defaultValue === undefined) {
result[row.Field].defaultValue = null;
}
}

if (typeof result[row.Field].defaultValue === 'string') {
result[row.Field].defaultValue = result[row.Field].defaultValue.replaceAll("'", '');

if (result[row.Field].defaultValue.includes('::')) {
const split = result[row.Field].defaultValue.split('::');
if (split[1].toLowerCase() !== 'regclass)') {
result[row.Field].defaultValue = split[0];
}
}
}
result[row.Field] = field;
}

return result;
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/dialects/snowflake/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ export class SnowflakeQuery extends AbstractQuery {
result[_result.Field] = {
type: _result.Type.toUpperCase(),
allowNull: _result.Null === 'YES',
defaultValue: _result.Default,
defaultValue: {
raw: _result.Default,
parsed: _result.Default,
},
primaryKey: _result.Key === 'PRI',
autoIncrement:
Object.hasOwn(_result, 'Extra') && _result.Extra.toLowerCase() === 'auto_increment',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { literal } from '../../expression-builders/literal';
import type { ColumnDescription } from '../abstract/query-interface.types';

const NUMBER_TYPES = ['INTEGER', 'REAL'];

export function parseDefaultValue(
rawDefaultValue: string | null,
field: Omit<ColumnDescription, 'defaultValue'>,
): unknown {
if (rawDefaultValue === null) {
// Column schema omits any "DEFAULT ..."
return undefined;
} else if (rawDefaultValue === 'NULL') {
// Column schema is a "DEFAULT NULL"
return null;
}

if (NUMBER_TYPES.includes(field.type) && !Number.isNaN(Number(rawDefaultValue))) {
return Number(rawDefaultValue);
}

if (rawDefaultValue?.startsWith("'") && rawDefaultValue.endsWith("'")) {
return rawDefaultValue.slice(1, -1).replaceAll("''", "'");
}

return literal(rawDefaultValue);
}