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
23 changes: 0 additions & 23 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const {
getModifyForeignKeyScriptDtos,
} = require('./alterScriptHelpers/alterForeignKeyHelper');
const { getViewsScripts } = require('./alterScriptHelpers/alterViewHelper');
const { getAddVersioningScriptDto, getEnableArchiveScriptDto } = require('./alterScriptHelpers/alterVersioningHelper');
const { getSchemaOfAlterCollection, getSchemaNameFromCollection } = require('../utils/general');

/**
Expand Down Expand Up @@ -154,27 +153,6 @@ const getAlterCollectionScriptDtos = ({ collection, app, inlineDeltaRelationship
];
};

/**
* Build the ALTER TABLE ... ADD VERSIONING and ALTER TABLE ... ENABLE ARCHIVE statements linking a table to its history
* table or archive table, for every added or modified entity. Runs after all entities in the batch are created, so the
* referenced tables already exist by the time it runs.
*
* @param {{ collection: DeltaModel; relatedSchemas: Record<string, AlterTable> }} params Delta model and sibling
* entities keyed by GUID.
* @returns {AlterScriptDto[]} Alter script DTOs.
*/
const getAlterVersioningScriptDtos = ({ collection, relatedSchemas }) => {
const { added, modified } = getSectionItems(collection.properties?.entities);
const entities = [...added, ...modified];
const addVersioningScriptDto = getAddVersioningScriptDto(relatedSchemas);
const enableArchiveScriptDto = getEnableArchiveScriptDto(relatedSchemas);

return [
...entities.map(item => addVersioningScriptDto(item)),
...entities.map(item => enableArchiveScriptDto(item)),
].filter(dto => dto !== undefined);
};

/**
* Build the view statements.
*
Expand Down Expand Up @@ -312,7 +290,6 @@ const getAlterScriptDtos = (data, app) => {
...containerScriptDtos,
...upsertedTypesScriptDtos,
...getAlterCollectionScriptDtos({ collection, app, inlineDeltaRelationships, relatedSchemas }),
...getAlterVersioningScriptDtos({ collection, relatedSchemas }),
...getAlterRelationshipsScriptDtos({ collection, ignoreRelationshipIDs }),
...getAlterViewScriptDtos({ collection, app }),
...deletedTypesScriptDtos,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,22 @@ const isRowid = ({ type }) => toUpper(type) === DATA_TYPE.rowid;
* @param {IdentityOptions} params Identity options.
* @returns {string} Identity options clause.
*/
const getIdentityOptions = ({ start, increment, minValue, maxValue, cycle, cache, cacheValue, order }) => {
const getIdentityOptions = ({
start,
increment,
minValue,
maxValue,
noMinValue,
noMaxValue,
cycle,
cache,
cacheValue,
order,
}) => {
const startWith = start ? `START WITH ${start}` : '';
const incrementBy = increment ? `INCREMENT BY ${increment}` : '';
const minimumValue = minValue ? `MINVALUE ${minValue}` : '';
const maximumValue = maxValue ? `MAXVALUE ${maxValue}` : '';
const minimumValue = noMinValue ? 'NO MINVALUE' : minValue ? `MINVALUE ${minValue}` : '';
const maximumValue = noMaxValue ? 'NO MAXVALUE' : maxValue ? `MAXVALUE ${maxValue}` : '';
const cacheOption = cacheValue ? `CACHE ${cacheValue}` : cache;

return [startWith, incrementBy, cycle, minimumValue, maximumValue, cacheOption, order].filter(Boolean).join(', ');
Expand All @@ -64,10 +75,27 @@ const getColumnDefault = ({
type,
generated,
generatedColumn,
generatedColumnType,
generatedColumnGenerated,
columnGenerationExpression,
implicitlyHidden,
}) => {
const hiddenClause = implicitlyHidden ? ' IMPLICITLY HIDDEN' : '';

if (isRowid({ type }) && generated) {
return ` GENERATED ${generated}`;
return ` GENERATED ${generated}${hiddenClause}`;
}

if (generatedColumn && generatedColumnType === 'ROW CHANGE TIMESTAMP') {
return ` GENERATED ${generatedColumnGenerated ?? 'ALWAYS'} FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP${hiddenClause}`;
}

if (
generatedColumn &&
generatedColumnType &&
['ROW BEGIN', 'ROW END', 'TRANSACTION START ID'].includes(generatedColumnType)
) {
return ` GENERATED ALWAYS AS ${generatedColumnType}${hiddenClause}`;
}

if (generatedColumn && columnGenerationExpression) {
Expand All @@ -78,8 +106,9 @@ const getColumnDefault = ({

if (isGeneratedIdentity && identity) {
const identityOptions = getIdentityOptions(identity);
const identityOptionsClause = identityOptions ? ` (${identityOptions})` : '';

return ` GENERATED ${identity.generated} AS IDENTITY (${identityOptions})`;
return ` GENERATED ${identity.generated} AS IDENTITY${identityOptionsClause}${hiddenClause}`;
}

if (defaultValue || defaultValue === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* HydratedPartitioning,
* InClauseParams,
* OptionConfig,
* TableOptionsBlock,
* TemporalPeriodsParams
* TableOptionsBlock
* } from '../../../types/ddlProvider'
*/

Expand Down Expand Up @@ -272,33 +271,6 @@ const getPartitioningClause = ({ partitioning }) => {
return '';
};

/**
* Build temporal period clauses.
*
* @param {TemporalPeriodsParams} params Period data.
* @returns {string} Period clauses.
*/
const getTemporalPeriodsClause = ({ periodForSystemTime, periodForBusinessTime }) => {
const clauses = [];

if (periodForSystemTime?.startColumn && periodForSystemTime?.endColumn) {
clauses.push(
`PERIOD FOR SYSTEM_TIME (${wrapInQuotes(periodForSystemTime.startColumn)}, ${wrapInQuotes(periodForSystemTime.endColumn)})`,
);
}

if (periodForBusinessTime?.startColumn && periodForBusinessTime?.endColumn) {
const endInclusive = periodForBusinessTime.endInclusive
? ` ${toUpper(periodForBusinessTime.endInclusive)}`
: '';
clauses.push(
`PERIOD FOR BUSINESS_TIME (${wrapInQuotes(periodForBusinessTime.startColumn)}, ${wrapInQuotes(periodForBusinessTime.endColumn)}${endInclusive})`,
);
}

return clauses.join('\n\t');
};

/**
* Build the AS (fullselect) clause and refresh/maintenance options of a materialized query table.
*
Expand Down Expand Up @@ -362,16 +334,9 @@ const getTableOptions = tableData => {
const inClause = getInClause(tableData);
const structuredOptions = getStructuredTableOptions(tableData);
const partitioning = tableData.inClauseType === 'accelerator' ? '' : getPartitioningClause(tableData);
const temporal =
tableData.inClauseType === 'accelerator'
? ''
: getTemporalPeriodsClause({
periodForSystemTime: tableData.periodForSystemTime,
periodForBusinessTime: tableData.periodForBusinessTime,
});
const tableProperties = tableData.tableProperties ?? '';

const statements = [mqtClause, inClause, structuredOptions.trim(), partitioning, temporal, tableProperties]
const statements = [mqtClause, inClause, structuredOptions.trim(), partitioning, tableProperties]
.filter(Boolean)
.join('\n\t');

Expand All @@ -382,6 +347,5 @@ module.exports = {
getTableOptions,
getInClause,
getPartitioningClause,
getTemporalPeriodsClause,
getMqtClause,
};
50 changes: 48 additions & 2 deletions forward_engineering/ddlProvider/ddlHelpers/table/getTableProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* DividedConstraints,
* ForeignKeyStatement,
* KeyConstraint,
* TablePropsParams
* TablePropsParams,
* TemporalPeriodsParams
* } from '../../../types/ddlProvider'
*/

const toUpper = require('lodash/toUpper');
const templates = require('../../templates');
const { assignTemplates } = require('../../../utils/assignTemplates');
const {
Expand All @@ -19,6 +21,34 @@ const {
const { getOptionsString } = require('../constraint/getOptionsString');
const { joinActivatedAndDeactivatedStatements } = require('../../../utils/joinActivatedAndDeactivatedStatements');

/**
* Build PERIOD SYSTEM_TIME / PERIOD BUSINESS_TIME table elements. Db2 for z/OS places these inline in the table-element
* list (no FOR keyword) - not as a trailing clause after the closing parenthesis.
*
* @param {TemporalPeriodsParams} params Period data.
* @returns {string[]} Period table elements.
*/
const getTemporalPeriodTableElements = ({ periodForSystemTime, periodForBusinessTime }) => {
const clauses = [];

if (periodForSystemTime?.startColumn && periodForSystemTime?.endColumn) {
clauses.push(
`PERIOD SYSTEM_TIME (${wrapInQuotes(periodForSystemTime.startColumn)}, ${wrapInQuotes(periodForSystemTime.endColumn)})`,
);
}

if (periodForBusinessTime?.startColumn && periodForBusinessTime?.endColumn) {
const endInclusive = periodForBusinessTime.endInclusive
? ` ${toUpper(periodForBusinessTime.endInclusive)}`
: '';
clauses.push(
`PERIOD BUSINESS_TIME (${wrapInQuotes(periodForBusinessTime.startColumn)}, ${wrapInQuotes(periodForBusinessTime.endColumn)}${endInclusive})`,
);
}

return clauses;
};

/**
* Extract constraint statement text.
*
Expand Down Expand Up @@ -101,7 +131,15 @@ const getDividedForeignKeyConstraints = ({ foreignKeyConstraints }) => {
* @param {TablePropsParams} params Table props input.
* @returns {string} Table props DDL.
*/
const getTableProps = ({ columns, foreignKeyConstraints, keyConstraints, checkConstraints, isActivated }) => {
const getTableProps = ({
columns,
foreignKeyConstraints,
keyConstraints,
checkConstraints,
periodForSystemTime,
periodForBusinessTime,
isActivated,
}) => {
const dividedKeysConstraints = getDividedKeysConstraints({ keyConstraints, isActivated });
const dividedForeignKeyConstraints = getDividedForeignKeyConstraints({ foreignKeyConstraints });
const keyConstraintsString = generateConstraintsString({
Expand All @@ -116,6 +154,13 @@ const getTableProps = ({ columns, foreignKeyConstraints, keyConstraints, checkCo
dividedConstraints: { activatedItems: checkConstraints ?? [], deactivatedItems: [] },
isParentActivated: isActivated,
});
const temporalPeriodsString = generateConstraintsString({
dividedConstraints: {
activatedItems: getTemporalPeriodTableElements({ periodForSystemTime, periodForBusinessTime }),
deactivatedItems: [],
},
isParentActivated: isActivated,
});
const columnsString = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' });

const tableProps = assignTemplates({
Expand All @@ -125,6 +170,7 @@ const getTableProps = ({ columns, foreignKeyConstraints, keyConstraints, checkCo
foreignKeyConstraints: foreignKeyConstraintsString,
keyConstraints: keyConstraintsString,
checkConstraints: checkConstraintsString,
temporalPeriods: temporalPeriodsString,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const hydrateTemporalPeriod = ({ jsonSchema, period }) => {
startColumn,
endColumn,
endInclusive: periodConfig.endInclusive,
historyTable: periodConfig.historyTable,
};
};

Expand Down
Loading
Loading