Skip to content

Commit

Permalink
Merge pull request #43 from VitaliiBedletskyi/master
Browse files Browse the repository at this point in the history
HCK-4009 Support extended ASCII characters
  • Loading branch information
mtseluiko authored Sep 22, 2023
2 parents e1d7216 + d88536a commit 78305cd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
35 changes: 22 additions & 13 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
generateViewSelectStatement,
escapeQuotes,
clearEmptyStatements,
prepareConstraintName
prepareConstraintName,
wrapByBackticks
} = require('./helpers/utils');

module.exports = (baseProvider, options, app) => {
Expand Down Expand Up @@ -106,7 +107,7 @@ module.exports = (baseProvider, options, app) => {
const partitionsStatement = commentIfDeactivated(partitions, { isActivated: isPartitionActivated });

const foreignKeysConstraintsStatements = foreignKeyConstraints.map(({statement}) => statement)
const compositePkFieldsNamesList = primaryKey.flatMap(compositePK => compositePK?.compositePrimaryKey.map(({name: columnName}) => columnName))
const compositePkFieldsNamesList = primaryKey.flatMap(compositePK => compositePK?.compositePrimaryKey.map(key => wrapByBackticks(key.name)))
const compositePrimaryKeyOutlineConstraint = compositePkFieldsNamesList.length ? `PRIMARY KEY (${compositePkFieldsNamesList.join(', ')}) NOT ENFORCED`: ''
const foreignKeysConstraintsStatement = foreignKeysConstraintsStatements.join(',\n')
const statementsToAddInsideTable = [activatedColumns.join(',\n'), deActivatedColumns.join(',\n'), compositePrimaryKeyOutlineConstraint, foreignKeysConstraintsStatement]
Expand Down Expand Up @@ -143,12 +144,20 @@ module.exports = (baseProvider, options, app) => {

if (!viewData.materialized) {
if (isActivated && !allDeactivated) {
const activated = viewData.keys.filter(key => key.isActivated).map(key => (key.alias || key.name)).filter(Boolean);
const deActivated = viewData.keys.filter(key => !key.isActivated).map(key => (key.alias || key.name)).filter(Boolean);
const activated = viewData.keys
.filter(key => key.isActivated)
.map(key => (key.alias || key.name))
.filter(Boolean)
.map(wrapByBackticks);
const deActivated = viewData.keys
.filter(key => !key.isActivated)
.map(key => (key.alias || key.name))
.filter(Boolean)
.map(wrapByBackticks);

columns = activated.join(', ') + (deActivated.length ? `/* ${deActivated.join(', ')} */` : '');
} else {
columns = viewData.keys.map(key => (key.alias || key.name)).filter(Boolean).join(', ');
columns = viewData.keys.map(key => wrapByBackticks(key.alias || key.name)).filter(Boolean).join(', ');
}
}
const isPartitionActivated = isActivatedPartition({
Expand Down Expand Up @@ -480,8 +489,8 @@ module.exports = (baseProvider, options, app) => {
alterColumnOptions(tableName, columnName, description) {
return assignTemplates(templates.alterColumnOptions, {
description: escapeQuotes(description),
tableName,
columnName,
tableName: wrapByBackticks(tableName),
columnName: wrapByBackticks(columnName),
});
},

Expand All @@ -491,16 +500,16 @@ module.exports = (baseProvider, options, app) => {
);

return assignTemplates(templates.alterColumnType, {
columnName: columnDefinition.name,
columnName: wrapByBackticks(columnDefinition.name),
type: columnSchema,
tableName,
tableName: wrapByBackticks(columnDefinition.name),
});
},

alterColumnDropNotNull(tableName, columnName) {
return assignTemplates(templates.alterColumnDropNotNull, {
columnName,
tableName,
columnName: wrapByBackticks(columnName),
tableName: wrapByBackticks(columnName),
});
},

Expand All @@ -509,7 +518,7 @@ module.exports = (baseProvider, options, app) => {

return assignTemplates(templates.alterTableAddColumn, {
tableName: fullTableName,
column,
column: column,
});
},

Expand All @@ -518,7 +527,7 @@ module.exports = (baseProvider, options, app) => {

return assignTemplates(templates.alterTableDropColumn, {
tableName: fullTableName,
columnName,
columnName: wrapByBackticks(columnName),
});
},

Expand Down
8 changes: 4 additions & 4 deletions forward_engineering/helpers/general.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { escapeQuotes, getTimestamp } = require('./utils');
const { escapeQuotes, getTimestamp, wrapByBackticks} = require('./utils');

module.exports = app => {
const _ = app.require('lodash');
Expand Down Expand Up @@ -118,8 +118,8 @@ module.exports = app => {

const foreignKeysToString = keys => {
if (Array.isArray(keys)) {
const activatedKeys = keys.filter(key => _.get(key, 'isActivated', true)).map(key => key.name.trim());
const deactivatedKeys = keys.filter(key => !_.get(key, 'isActivated', true)).map(key => key.name.trim());
const activatedKeys = keys.filter(key => _.get(key, 'isActivated', true)).map(key => wrapByBackticks(key.name.trim()));
const deactivatedKeys = keys.filter(key => !_.get(key, 'isActivated', true)).map(key => wrapByBackticks(key.name.trim()));
const deactivatedKeysAsString = deactivatedKeys.length
? commentIfDeactivated(deactivatedKeys, { isActivated: false }, true)
: '';
Expand All @@ -130,7 +130,7 @@ module.exports = app => {
};

const foreignActiveKeysToString = keys => {
return keys.map(key => key.name.trim()).join(', ');
return keys.map(key => wrapByBackticks(key.name.trim())).join(', ');
};

return {
Expand Down
17 changes: 11 additions & 6 deletions forward_engineering/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const escapeQuotes = (str = '') => {
return str.replace(/(")/gi, '\\$1').replace(/\n/gi, '\\n');
};

const wrapByBackticks = (str = '') => {
return `\`${str}\``;
}

const getPartitioningByIngestionTime = (partitioningType) => {
if (!partitioningType) {
return '_PARTITIONDATE';
Expand Down Expand Up @@ -63,7 +67,7 @@ const getTablePartitioning = ({
}

if (partitioning === 'By time-unit column' && partitionTimeColumn) {
return `PARTITION BY DATE(${partitionTimeColumn})`;
return `PARTITION BY DATE(${wrapByBackticks(partitionTimeColumn)})`;
}

if (partitioning === 'By integer-range') {
Expand All @@ -84,11 +88,11 @@ const getClusteringKey = (clusteringKey, isParentActivated) => {
return '';
}

const activated = clusteringKey.filter(key => key.isActivated).map(key => key.name).join(', ');
const deActivated = clusteringKey.filter(key => !key.isActivated).map(key => key.name).join(', ');
const activated = clusteringKey.filter(key => key.isActivated).map(key => wrapByBackticks(key.name)).join(', ');
const deActivated = clusteringKey.filter(key => !key.isActivated).map(key => wrapByBackticks(key.name)).join(', ');

if (!isParentActivated) {
return '\nCLUSTER BY ' + clusteringKey.map(key => key.name).join(', ');
return '\nCLUSTER BY ' + clusteringKey.map(key => wrapByBackticks(key.name)).join(', ');
}

if (activated.length === 0) {
Expand Down Expand Up @@ -288,7 +292,7 @@ const getColumnSchema = (deps) => ({ type, description, dataTypeMode, name, json
}

return assignTemplates(templates.columnDefinition, {
name,
name: name && wrapByBackticks(name),
type: dataType,
primaryKey,
notNull,
Expand All @@ -298,7 +302,7 @@ const getColumnSchema = (deps) => ({ type, description, dataTypeMode, name, json

const generateViewSelectStatement = (getFullName, isActivated) => ({ columns, projectId, datasetName }) => {
const keys = columns.reduce((tables, key) => {
let column = key.name;
let column = wrapByBackticks(key.name);

if (key.alias) {
column = `${column} as ${key.alias}`;
Expand Down Expand Up @@ -353,4 +357,5 @@ module.exports = {
escapeQuotes,
clearEmptyStatements,
prepareConstraintName,
wrapByBackticks,
};
1 change: 0 additions & 1 deletion validation/validationRegularExpressions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
{
"name": "^[A-Za-z_][A-Za-z0-9_]{0,299}$"
}

0 comments on commit 78305cd

Please sign in to comment.