Skip to content

Commit 02f6b2f

Browse files
authored
HCK-16374: external schema table issue & table props (#80)
* HCK-16374: add unquoted default IAM ROLE * HCK-16376: add clean properties helper * HCK-16376: update RegExp, SonarCloud * HCK-16376: update stripQuotes, SonarCloud
1 parent 3645f35 commit 02f6b2f

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

forward_engineering/configs/templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
createDatabase: '',
33
createSchema: 'CREATE SCHEMA${ifNotExist} "${name}"${authorization}${quota};\n',
44
createExternalSchema:
5-
"CREATE EXTERNAL SCHEMA${ifNotExist} \"${name}\" FROM ${source}\nDATABASE '${sourceDBName}'${sourceSchemaName}${region}${uri}\nIAM_ROLE '${iamRole}'${secretARN}${catalogRole}${createExternalDatabase};\n",
5+
'CREATE EXTERNAL SCHEMA${ifNotExist} "${name}" FROM ${source}\nDATABASE \'${sourceDBName}\'${sourceSchemaName}${region}${uri}\nIAM_ROLE ${iamRole}${secretARN}${catalogRole}${createExternalDatabase};\n',
66

77
createTable:
88
'CREATE ${temporary}TABLE' +

forward_engineering/ddlProvider.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const defaultTypes = require('./configs/defaultTypes');
44
const templates = require('./configs/templates');
55
const types = require('./configs/types');
66
const { commentIfDeactivated } = require('./helpers/commentDeactivatedHelper');
7-
const { getTableAttributes, getTableConstraints, getTableLikeConstraint } = require('./helpers/tableHelper');
7+
const {
8+
getTableAttributes,
9+
getTableConstraints,
10+
getTableLikeConstraint,
11+
formatIamRole,
12+
} = require('./helpers/tableHelper');
813

914
module.exports = (baseProvider, options, app) => {
1015
const { hasType } = app.require('@hackolade/ddl-fe-utils').general;
@@ -109,6 +114,8 @@ module.exports = (baseProvider, options, app) => {
109114
comment: toString(comment),
110115
});
111116
if (external) {
117+
const iamRoleFormatted = formatIamRole(iamRole);
118+
112119
database = assignTemplates(templates.createExternalSchema, {
113120
name,
114121
ifNotExist,
@@ -117,7 +124,7 @@ module.exports = (baseProvider, options, app) => {
117124
sourceSchemaName,
118125
region,
119126
uri,
120-
iamRole,
127+
iamRole: iamRoleFormatted,
121128
secretARN,
122129
catalogRole,
123130
createExternalDatabase,

forward_engineering/helpers/general.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,33 @@ module.exports = app => {
188188
.join(', ')
189189
: '';
190190

191+
const stripQuotes = value => {
192+
if (!value) return '';
193+
let string = value.trim();
194+
195+
while (/^['"]|['"]$/.test(string)) {
196+
string = string.replaceAll(/^['"]|['"]$/g, '');
197+
}
198+
199+
return string;
200+
};
201+
191202
const parseProps = text => {
192203
if (!text) return '';
193204

194205
return text
195206
.split('\n')
196207
.map(line => line.trim())
208+
.map(line => line.replace(/,$/, '').trim())
197209
.filter(line => line.includes('='))
198210
.map(line => {
199211
const [propertyKey, ...valueParts] = line.split('=');
200212
const propertyValue = valueParts.join('=').trim();
201213

202-
return `'${escape(propertyKey.trim())}'='${escape(propertyValue)}'`;
214+
const cleanKey = stripQuotes(propertyKey);
215+
const cleanValue = stripQuotes(propertyValue);
216+
217+
return `'${escape(cleanKey)}'='${escape(cleanValue)}'`;
203218
})
204219
.join(', ');
205220
};
@@ -210,7 +225,10 @@ module.exports = app => {
210225
}
211226

212227
if (tableData.rowFormatType === ROW_FORMAT_TYPES.SERDE && tableData.rowFormatSerde) {
213-
let format = `\nROW FORMAT SERDE ${toString(tableData.rowFormatSerde)}`;
228+
const cleanSerdeClass = stripQuotes(tableData.rowFormatSerde);
229+
230+
let format = `\nROW FORMAT SERDE ${toString(cleanSerdeClass)}`;
231+
214232
const serdeProps = parseProps(tableData.serdeProperties);
215233

216234
if (serdeProps) {

forward_engineering/helpers/tableHelper.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,22 @@ const getTableLikeConstraint = (likeTableName, includingDefault, needComma) => {
5050
return likeStatement;
5151
};
5252

53+
const formatIamRole = iamRole => {
54+
if (!iamRole) {
55+
return "''";
56+
}
57+
58+
const normalizedRole = String(iamRole).trim();
59+
if (normalizedRole.toLowerCase() === 'default') {
60+
return 'default';
61+
}
62+
63+
return `'${normalizedRole}'`;
64+
};
65+
5366
module.exports = {
5467
getTableAttributes,
5568
getTableConstraints,
5669
getTableLikeConstraint,
70+
formatIamRole,
5771
};

0 commit comments

Comments
 (0)