Skip to content

Commit

Permalink
Merge pull request #20 from hackolade/master
Browse files Browse the repository at this point in the history
Merge original
  • Loading branch information
BearDimonR authored Jul 5, 2022
2 parents 946968f + fa3471d commit 729f0f3
Show file tree
Hide file tree
Showing 12 changed files with 986 additions and 277 deletions.
58 changes: 58 additions & 0 deletions adapter/0.1.9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright © 2016-2018 by IntegrIT S.A. dba Hackolade. All rights reserved.
*
* The copyright to the computer software herein is the property of IntegrIT S.A.
* The software may be used and/or copied only with the written permission of
* IntegrIT S.A. or in accordance with the terms and conditions stipulated in
* the agreement/contract under which the software has been supplied.
*
* {
* "add": {
* "entity": [<names of new property>],
* "container": [<names of new property>],
* "model": [<names of new property>],
* "view": [<names of new property>],
* "field": {
* "<type>": [<names of new property>]
* }
* },
* "delete": {
* "entity": [<names of new property>],
* "container": [<names of new property>],
* "model": [<names of new property>],
* "view": [<names of new property>],
* "field": {
* "<type>": [<names of new property>]
* }
* },
* "modify": {
* "entity": [
* {
* "from": { <properties that identify record> },
* "to": { <properties that need to be changed> }
* }
* ],
* "container": [],
* "model": [],
* "view": [],
* "field": []
* },
* }
*/
{
"modify": {
"entity": [
[
"setRdbmEntityCompositeKeys",
"primaryKey",
"compositePrimaryKey",
"primaryKey",
[
"unique",
"compositePrimaryKey",
"compositeUniqueKey"
]
]
]
}
}
24 changes: 21 additions & 3 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,33 @@ module.exports = {
);
}

const modelDefinitions = JSON.parse(data.modelDefinitions);
const internalDefinitions = JSON.parse(data.internalDefinitions);
const externalDefinitions = JSON.parse(data.externalDefinitions);
const dbVersion = data.modelData[0]?.dbVersion;
const containersScripts = getAlterContainersScripts(collection);
const collectionsScripts = getAlterCollectionsScripts(collection, app, dbVersion);
const collectionsScripts = getAlterCollectionsScripts({
collection,
app,
dbVersion,
modelDefinitions,
internalDefinitions,
externalDefinitions,
});
const viewScripts = getAlterViewScripts(collection, app);
const modelDefinitionsScripts = getAlterModelDefinitionsScripts(collection, app);
const modelDefinitionsScripts = getAlterModelDefinitionsScripts({
collection,
app,
dbVersion,
modelDefinitions,
internalDefinitions,
externalDefinitions,
});
const script = [
...containersScripts,
...modelDefinitionsScripts,
...collectionsScripts,
...viewScripts,
...modelDefinitionsScripts,
].join('\n\n');

const applyDropStatements = data.options?.additionalOptions?.some(
Expand All @@ -49,6 +66,7 @@ module.exports = {
generateContainerScript(data, logger, callback, app) {
try {
data.jsonSchema = data.collections[0];
data.internalDefinitions = Object.values(data.internalDefinitions)[0];
this.generateScript(data, logger, callback, app);
} catch (error) {
logger.log('error', { message: error.message, stack: error.stack }, 'Oracle Forward-Engineering Error');
Expand Down
15 changes: 12 additions & 3 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = (baseProvider, options, app) => {
assignTemplates,
});

const { getUserDefinedType } = require('./helpers/udtHelper')({
const { getUserDefinedType, isNotPlainType } = require('./helpers/udtHelper')({
_,
commentIfDeactivated,
assignTemplates,
Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports = (baseProvider, options, app) => {
return schemaStatement;
},

hydrateColumn({ columnDefinition, jsonSchema, schemaData }) {
hydrateColumn({ columnDefinition, jsonSchema, schemaData, definitionJsonSchema = {} }) {
const dbVersion = schemaData.dbVersion;
const type = jsonSchema.$ref ? columnDefinition.type : _.toUpper(jsonSchema.mode || jsonSchema.type);
return {
Expand All @@ -125,7 +125,7 @@ module.exports = (baseProvider, options, app) => {
uniqueKeyOptions: jsonSchema.uniqueKeyOptions,
nullable: columnDefinition.nullable,
default: columnDefinition.default,
comment: jsonSchema.description,
comment: jsonSchema.refDescription || jsonSchema.description || definitionJsonSchema.description,
isActivated: columnDefinition.isActivated,
scale: columnDefinition.scale,
precision: columnDefinition.precision,
Expand All @@ -144,6 +144,15 @@ module.exports = (baseProvider, options, app) => {
};
},

hydrateJsonSchemaColumn(jsonSchema, definitionJsonSchema) {
if (!jsonSchema.$ref || _.isEmpty(definitionJsonSchema) || isNotPlainType(definitionJsonSchema)) {
return jsonSchema;
}

jsonSchema = _.omit(jsonSchema, '$ref');
return { ...definitionJsonSchema, ...jsonSchema };
},

convertColumnDefinition(columnDefinition, template = templates.columnDefinition) {
const type = replaceTypeByVersion(columnDefinition.type, columnDefinition.dbVersion);

Expand Down
32 changes: 25 additions & 7 deletions forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ const getAlterContainersScripts = collection => {
return [].concat(addContainersScripts).concat(deleteContainersScripts);
};

const getAlterCollectionsScripts = (collection, app, dbVersion) => {
const getAlterCollectionsScripts = ({
collection,
app,
dbVersion,
modelDefinitions,
internalDefinitions,
externalDefinitions,
}) => {
const createCollectionsScripts = []
.concat(collection.properties?.entities?.properties?.added?.items)
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.filter(collection => collection.compMod?.created)
.map(getAddCollectionScript(app, dbVersion));
.map(getAddCollectionScript({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }));
const deleteCollectionScripts = []
.concat(collection.properties?.entities?.properties?.deleted?.items)
.filter(Boolean)
Expand All @@ -49,7 +56,7 @@ const getAlterCollectionsScripts = (collection, app, dbVersion) => {
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.filter(collection => !collection.compMod)
.flatMap(getAddColumnScript(app));
.flatMap(getAddColumnScript({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }));
const deleteColumnScripts = []
.concat(collection.properties?.entities?.properties?.deleted?.items)
.filter(Boolean)
Expand Down Expand Up @@ -92,14 +99,21 @@ const getAlterViewScripts = (collection, app) => {
return [...deleteViewsScripts, ...createViewsScripts].map(script => script.trim());
};

const getAlterModelDefinitionsScripts = (collection, app, dbVersion) => {
const getAlterModelDefinitionsScripts = ({
collection,
app,
dbVersion,
modelDefinitions,
internalDefinitions,
externalDefinitions,
}) => {
const createUdtScripts = []
.concat(collection.properties?.modelDefinitions?.properties?.added?.items)
.filter(Boolean)
.map(item => Object.values(item.properties)[0])
.map(item => ({ ...item, ...(app.require('lodash').omit(item.role, 'properties') || {}) }))
.filter(item => item.compMod?.created)
.map(getCreateUdtScript(app, dbVersion));
.map(getCreateUdtScript({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }));
const deleteUdtScripts = []
.concat(collection.properties?.modelDefinitions?.properties?.deleted?.items)
.filter(Boolean)
Expand All @@ -114,7 +128,9 @@ const getAlterModelDefinitionsScripts = (collection, app, dbVersion) => {
.filter(item => !item.compMod)
.map(item => ({ ...item, ...(app.require('lodash').omit(item.role, 'properties') || {}) }))
.filter(item => item.childType === 'object_udt')
.flatMap(getAddColumnToTypeScript(app));
.flatMap(
getAddColumnToTypeScript({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }),
);
const deleteColumnScripts = []
.concat(collection.properties?.modelDefinitions?.properties?.deleted?.items)
.filter(Boolean)
Expand All @@ -139,7 +155,9 @@ const getAlterModelDefinitionsScripts = (collection, app, dbVersion) => {
...addColumnScripts,
...deleteColumnScripts,
...modifyColumnScripts,
].map(script => script.trim());
]
.filter(Boolean)
.map(script => script.trim());
};

module.exports = {
Expand Down
144 changes: 83 additions & 61 deletions forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
const { checkFieldPropertiesChanged } = require('./common');

const getAddCollectionScript = (app, dbVersion) => collection => {
const _ = app.require('lodash');
const { getEntityName } = require('../../utils/general')(_);
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(_);
const ddlProvider = require('../../ddlProvider')(null, null, app);

const schemaName = collection.compMod.keyspaceName;
const schemaData = { schemaName, dbVersion };
const jsonSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) =>
createColumnDefinitionBySchema({
name,
jsonSchema: column,
parentJsonSchema: jsonSchema,
ddlProvider,
const getAddCollectionScript =
({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }) =>
collection => {
const _ = app.require('lodash');
const { getEntityName } = require('../../utils/general')(_);
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(app);
const ddlProvider = require('../../ddlProvider')(null, null, app);
const { getDefinitionByReference } = app.require('@hackolade/ddl-fe-utils');

const schemaName = collection.compMod.keyspaceName;
const schemaData = { schemaName, dbVersion };
const jsonSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) => {
const definitionJsonSchema = getDefinitionByReference({
propertySchema: column,
modelDefinitions,
internalDefinitions,
externalDefinitions,
});

return createColumnDefinitionBySchema({
name,
jsonSchema: column,
parentJsonSchema: jsonSchema,
ddlProvider,
schemaData,
definitionJsonSchema,
});
});
const checkConstraints = (jsonSchema.chkConstr || []).map(check =>
ddlProvider.createCheckConstraint(ddlProvider.hydrateCheckConstraint(check)),
);
const tableData = {
name: getEntityName(jsonSchema),
columns: columnDefinitions.map(data => ddlProvider.convertColumnDefinition(data)),
checkConstraints: checkConstraints,
foreignKeyConstraints: [],
schemaData,
}),
);
const checkConstraints = (jsonSchema.chkConstr || []).map(check =>
ddlProvider.createCheckConstraint(ddlProvider.hydrateCheckConstraint(check)),
);
const tableData = {
name: getEntityName(jsonSchema),
columns: columnDefinitions.map(data => ddlProvider.convertColumnDefinition(data)),
checkConstraints: checkConstraints,
foreignKeyConstraints: [],
schemaData,
columnDefinitions,
};
const hydratedTable = ddlProvider.hydrateTable({ tableData, entityData: [jsonSchema], jsonSchema });
columnDefinitions,
};
const hydratedTable = ddlProvider.hydrateTable({ tableData, entityData: [jsonSchema], jsonSchema });

return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated);
};
return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated);
};

const getDeleteCollectionScript = app => collection => {
const _ = app.require('lodash');
Expand All @@ -47,33 +58,44 @@ const getDeleteCollectionScript = app => collection => {
return `DROP TABLE ${fullName};`;
};

const getAddColumnScript = (app, dbVersion) => collection => {
const _ = app.require('lodash');
const { getEntityName } = require('../../utils/general')(_);
const { getNamePrefixedWithSchemaName } = require('../general')({ _ });
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(_);
const ddlProvider = require('../../ddlProvider')(null, null, app);

const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = getEntityName(collectionSchema);
const schemaName = collectionSchema.compMod?.keyspaceName;
const fullName = getNamePrefixedWithSchemaName(tableName, schemaName);
const schemaData = { schemaName, dbVersion };

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => !jsonSchema.compMod)
.map(([name, jsonSchema]) =>
createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
schemaData,
}),
)
.map(data => ddlProvider.convertColumnDefinition(data))
.map(script => `ALTER TABLE ${fullName} ADD (${script});`);
};
const getAddColumnScript =
({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }) =>
collection => {
const _ = app.require('lodash');
const { getEntityName } = require('../../utils/general')(_);
const { getNamePrefixedWithSchemaName } = require('../general')({ _ });
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(app);
const ddlProvider = require('../../ddlProvider')(null, null, app);
const { getDefinitionByReference } = app.require('@hackolade/ddl-fe-utils');

const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = getEntityName(collectionSchema);
const schemaName = collectionSchema.compMod?.keyspaceName;
const fullName = getNamePrefixedWithSchemaName(tableName, schemaName);
const schemaData = { schemaName, dbVersion };

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => !jsonSchema.compMod)
.map(([name, jsonSchema]) => {
const definitionJsonSchema = getDefinitionByReference({
propertySchema: jsonSchema,
modelDefinitions,
internalDefinitions,
externalDefinitions,
});

return createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
schemaData,
definitionJsonSchema,
});
})
.map(data => ddlProvider.convertColumnDefinition(data))
.map(script => `ALTER TABLE ${fullName} ADD (${script});`);
};

const getDeleteColumnScript = app => collection => {
const _ = app.require('lodash');
Expand Down Expand Up @@ -112,9 +134,9 @@ const getModifyColumnScript = app => collection => {
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
.map(
([name, jsonSchema]) =>
`ALTER TABLE ${fullName} MODIFY (${wrapInQuotes(name)} ${
_.toUpper(jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type)
});`,
`ALTER TABLE ${fullName} MODIFY (${wrapInQuotes(name)} ${_.toUpper(
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type,
)});`,
);

return [...renameColumnScripts, ...changeTypeScripts];
Expand Down
Loading

0 comments on commit 729f0f3

Please sign in to comment.