Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
'CREATE${orReplace}PROCEDURE ${name} (${arguments})\nAS $$\n${statement}\n$$ LANGUAGE plpgsql${securityMode}${configurationParameter};\n',

columnDefinition:
'"${name}" ${type}${default}${encoding}${distKey}${sortKey}${notNull}${unique}${primaryKey}${inlineConstraints}${references}',
'"${name}" ${type}${default}${identity}${encoding}${distKey}${sortKey}${notNull}${unique}${primaryKey}${inlineConstraints}${references}',

compoundSortKey: '${sortStyle} SORTKEY (${keys})',
compoundUniqueKey: 'UNIQUE (${keys})',
Expand Down
6 changes: 5 additions & 1 deletion forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ module.exports = (baseProvider, options, app) => {
parseProps,
getRowFormat,
getStoredAs,
getIdentityDefinition,
} = require('./helpers/general')(app);
const {
decorateType,
getDefault,
getQuota,
getIdentity,
getUri,
getARN,
getSourceSchemaNameForExternalSchema,
Expand Down Expand Up @@ -307,6 +309,7 @@ module.exports = (baseProvider, options, app) => {
default: !_.isUndefined(columnDefinition.default)
? ' DEFAULT ' + getDefault(columnDefinition.type, columnDefinition.default)
: '',
identity: getIdentity(columnDefinition.identity),
distKey: columnDefinition.distKey ? ' DISTKEY' : '',
sortKey: columnDefinition.sortKey ? ' SORTKEY' : '',
primaryKey: columnDefinition.primaryKey ? ' PRIMARY KEY' : '',
Expand Down Expand Up @@ -487,7 +490,8 @@ module.exports = (baseProvider, options, app) => {
sortKey: jsonSchema.sortKey && !jsonSchema.compositeSortKey,
primaryKey: columnDefinition.primaryKey && !jsonSchema.compositePrimaryKey,
encoding: jsonSchema.encoding,
references: '',
identity: getIdentityDefinition(jsonSchema),
reference: '',
};
},

Expand Down
48 changes: 34 additions & 14 deletions forward_engineering/helpers/columnDefinitionHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
const { toUpper, toLower, chain, partition, get } = require('lodash');
const { commentIfDeactivated } = require('./commentDeactivatedHelper');

module.exports = app => {
Expand All @@ -7,23 +7,23 @@ module.exports = app => {
const { toString } = require('./general')(app);

const decorateType = (type, columnDefinition) => {
type = _.toUpper(type);
type = toUpper(type);
let resultType = type;

if (isTimestamp(type)) {
if (columnDefinition.timePrecision && !_.isNaN(columnDefinition.timePrecision)) {
if (columnDefinition.timePrecision && !Number.isNaN(columnDefinition.timePrecision)) {
resultType = `${type}(${columnDefinition.timePrecision})`;
}
}

if (['VARCHAR', 'CHAR', 'CHARACTER', 'VARBYTE'].includes(type)) {
if (columnDefinition.length && !_.isNaN(columnDefinition.length)) {
if (columnDefinition.length && !Number.isNaN(columnDefinition.length)) {
resultType = `${type}(${columnDefinition.length})`;
}
}

if (['NUMBER', 'DECIMAL', 'NUMERIC'].includes(type)) {
if (!isNaN(columnDefinition.scale) && !isNaN(columnDefinition.precision)) {
if (!Number.isNaN(columnDefinition.scale) && !Number.isNaN(columnDefinition.precision)) {
resultType = `${type}(${Number(columnDefinition.precision)},${Number(columnDefinition.scale)})`;
}
}
Expand All @@ -33,23 +33,42 @@ module.exports = app => {

const isString = type =>
['VARCHAR', 'NVARCHAR', 'TEXT', 'CHAR', 'CHARACTER VARYING', 'BPCHAR', 'CHARACTER', 'NCHAR'].includes(
_.toUpper(type),
toUpper(type),
);

const isTimestamp = type => ['TIME', 'TIMESTAMP', 'TIMESTAMPTZ', 'TIMETZ'].includes(_.toUpper(type));
const isTimestamp = type => ['TIME', 'TIMESTAMP', 'TIMESTAMPTZ', 'TIMETZ'].includes(toUpper(type));

const escapeString = str => str.replace(/^'([\S\s]+)'$/, '$1').replace(/'/g, "''");

const getDefault = (type, defaultValue) => {
const constantsValues = ['current_timestamp', 'current_user', 'null'];

if (isString(type) && !constantsValues.includes(_.toLower(defaultValue))) {
if (isString(type) && !constantsValues.includes(toLower(defaultValue))) {
return `'${escapeString(String(defaultValue))}'`;
} else if (_.toUpper(type) === 'BOOLEAN') {
return _.toUpper(defaultValue);
} else {
return defaultValue;
} else if (toUpper(type) === 'BOOLEAN') {
return toUpper(defaultValue);
}

return defaultValue;
};

const hasValue = value => value !== '' && value !== null && value !== undefined;

const getIdentity = identity => {
if (!identity) {
return '';
}

const prefix = identity.generateIdentity === 'by default' ? 'GENERATED BY DEFAULT AS IDENTITY' : 'IDENTITY';

if (!hasValue(identity.seed) && !hasValue(identity.step)) {
return ` ${prefix}`;
}

const seed = Number(get(identity, 'seed', 0));
const step = Number(get(identity, 'step', 1));

return ` ${prefix}(${seed}, ${step})`;
};

const getQuota = (quotaSize, isUnlimited) => {
Expand Down Expand Up @@ -92,7 +111,7 @@ module.exports = app => {
};

const getColumnComments = (tableName, columnDefinitions) => {
return _.chain(columnDefinitions)
return chain(columnDefinitions)
.filter('comment')
.map(columnData => {
const comment = assignTemplates(templates.comment, {
Expand All @@ -110,7 +129,7 @@ module.exports = app => {
const createColumnsStatements = columns => columns.map(column => column.statement).join(',\n\t');

const getColumnsDefinitions = (columns, isParentActivated) => {
const [activatedColumns, deactivatedColumns] = _.partition(
const [activatedColumns, deactivatedColumns] = partition(
columns,
column => !isParentActivated || column?.isActivated,
);
Expand All @@ -126,6 +145,7 @@ module.exports = app => {

return {
getQuota,
getIdentity,
getSourceSchemaNameForExternalSchema,
getUri,
getARN,
Expand Down
13 changes: 13 additions & 0 deletions forward_engineering/helpers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ module.exports = app => {
.join(', ');
};

const getIdentityDefinition = jsonSchema => {
if (jsonSchema.defaultOption !== 'Identity') {
return null;
}

return {
generateIdentity: jsonSchema.generateIdentity,
seed: jsonSchema.identity?.seed,
step: jsonSchema.identity?.step,
};
};

const getRowFormat = tableData => {
if (tableData.rowFormatType === ROW_FORMAT_TYPES.DELIMITED && tableData.rowFormatDelimited) {
return `\nROW FORMAT DELIMITED ${tableData.rowFormatDelimited}`;
Expand Down Expand Up @@ -276,5 +288,6 @@ module.exports = app => {
parseProps,
getRowFormat,
getStoredAs,
getIdentityDefinition,
};
};
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.