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 @@ -2,7 +2,7 @@ module.exports = {
createDatabase: '',
createSchema: 'CREATE SCHEMA${ifNotExist} "${name}"${authorization}${quota};\n',
createExternalSchema:
"CREATE EXTERNAL SCHEMA${ifNotExist} \"${name}\" FROM ${source}\nDATABASE '${sourceDBName}'${sourceSchemaName}${region}${uri}\nIAM_ROLE '${iamRole}'${secretARN}${catalogRole}${createExternalDatabase};\n",
'CREATE EXTERNAL SCHEMA${ifNotExist} "${name}" FROM ${source}\nDATABASE \'${sourceDBName}\'${sourceSchemaName}${region}${uri}\nIAM_ROLE ${iamRole}${secretARN}${catalogRole}${createExternalDatabase};\n',

createTable:
'CREATE ${temporary}TABLE' +
Expand Down
11 changes: 9 additions & 2 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const defaultTypes = require('./configs/defaultTypes');
const templates = require('./configs/templates');
const types = require('./configs/types');
const { commentIfDeactivated } = require('./helpers/commentDeactivatedHelper');
const { getTableAttributes, getTableConstraints, getTableLikeConstraint } = require('./helpers/tableHelper');
const {
getTableAttributes,
getTableConstraints,
getTableLikeConstraint,
formatIamRole,
} = require('./helpers/tableHelper');

module.exports = (baseProvider, options, app) => {
const { hasType } = app.require('@hackolade/ddl-fe-utils').general;
Expand Down Expand Up @@ -109,6 +114,8 @@ module.exports = (baseProvider, options, app) => {
comment: toString(comment),
});
if (external) {
const iamRoleFormatted = formatIamRole(iamRole);

database = assignTemplates(templates.createExternalSchema, {
name,
ifNotExist,
Expand All @@ -117,7 +124,7 @@ module.exports = (baseProvider, options, app) => {
sourceSchemaName,
region,
uri,
iamRole,
iamRole: iamRoleFormatted,
secretARN,
catalogRole,
createExternalDatabase,
Expand Down
22 changes: 20 additions & 2 deletions forward_engineering/helpers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,33 @@ module.exports = app => {
.join(', ')
: '';

const stripQuotes = value => {
if (!value) return '';
let string = value.trim();

while (/^['"]|['"]$/.test(string)) {
string = string.replaceAll(/^['"]|['"]$/g, '');
}

return string;
};

const parseProps = text => {
if (!text) return '';

return text
.split('\n')
.map(line => line.trim())
.map(line => line.replace(/,$/, '').trim())
.filter(line => line.includes('='))
.map(line => {
const [propertyKey, ...valueParts] = line.split('=');
const propertyValue = valueParts.join('=').trim();

return `'${escape(propertyKey.trim())}'='${escape(propertyValue)}'`;
const cleanKey = stripQuotes(propertyKey);
const cleanValue = stripQuotes(propertyValue);

return `'${escape(cleanKey)}'='${escape(cleanValue)}'`;
})
.join(', ');
};
Expand All @@ -210,7 +225,10 @@ module.exports = app => {
}

if (tableData.rowFormatType === ROW_FORMAT_TYPES.SERDE && tableData.rowFormatSerde) {
let format = `\nROW FORMAT SERDE ${toString(tableData.rowFormatSerde)}`;
const cleanSerdeClass = stripQuotes(tableData.rowFormatSerde);

let format = `\nROW FORMAT SERDE ${toString(cleanSerdeClass)}`;

const serdeProps = parseProps(tableData.serdeProperties);

if (serdeProps) {
Expand Down
14 changes: 14 additions & 0 deletions forward_engineering/helpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,22 @@ const getTableLikeConstraint = (likeTableName, includingDefault, needComma) => {
return likeStatement;
};

const formatIamRole = iamRole => {
if (!iamRole) {
return "''";
}

const normalizedRole = String(iamRole).trim();
if (normalizedRole.toLowerCase() === 'default') {
return 'default';
}

return `'${normalizedRole}'`;
};

module.exports = {
getTableAttributes,
getTableConstraints,
getTableLikeConstraint,
formatIamRole,
};