Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug with relational mapping tests set with the incorrect database #645

Merged
merged 2 commits into from
Nov 9, 2021
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
5 changes: 5 additions & 0 deletions .changeset/tender-foxes-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-studio': patch
---

Replace `Table|View` with `TableAlias` as the relational mapping source, i.e `MappingElementSource`. Fixes generating mapping test with nested databases (see [#651](https://github.com/finos/legend-studio/issues/651)] for more details).
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,19 @@ import type {
InstanceSetImplementation,
Property,
PackageableElement,
View,
} from '@finos/legend-graph';
import {
Class,
Type,
FlatData,
RootFlatDataRecordType,
View,
Table,
Database,
PRIMITIVE_TYPE,
TableAlias,
TableExplicitReference,
ViewExplicitReference,
} from '@finos/legend-graph';
import { StudioLambdaEditor } from '../../../shared/StudioLambdaEditor';
import type { EditorStore } from '../../../../stores/EditorStore';
Expand Down Expand Up @@ -183,15 +186,21 @@ export const InstanceSetImplementationSourceExplorer = observer(
);
return;
}
const mainTableAlias = new TableAlias();
mainTableAlias.relation =
relations[0] instanceof Table
? TableExplicitReference.create(relations[0])
: ViewExplicitReference.create(relations[0] as View);
mainTableAlias.name = mainTableAlias.relation.value.name;
if (relations.length === 1) {
flowResult(
mappingEditorState.changeClassMappingSourceDriver(
setImplementation,
relations[0],
mainTableAlias,
),
).catch(applicationStore.alertIllegalUnhandledError);
} else {
setSourceElementForSourceSelectorModal(relations[0]);
setSourceElementForSourceSelectorModal(mainTableAlias);
}
}
},
Expand Down Expand Up @@ -306,9 +315,9 @@ export const InstanceSetImplementationSourceExplorer = observer(
selectedType={instanceSetImplementationState.selectedType}
/>
)}
{(srcElement instanceof Table || srcElement instanceof View) && (
{srcElement instanceof TableAlias && (
<TableOrViewSourceTree
relation={srcElement}
relation={srcElement.relation.value}
selectedType={instanceSetImplementationState.selectedType}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import type {
} from '../../../../stores/editor-state/element-editor-state/mapping/MappingEditorState';
import { getMappingElementSource } from '../../../../stores/editor-state/element-editor-state/mapping/MappingEditorState';
import Dialog from '@material-ui/core/Dialog';
import type { InstanceSetImplementation } from '@finos/legend-graph';
import type { InstanceSetImplementation, View } from '@finos/legend-graph';
import {
Class,
RootFlatDataRecordType,
View,
Table,
DEFAULT_DATABASE_SCHEMA_NAME,
TableAlias,
TableExplicitReference,
ViewExplicitReference,
} from '@finos/legend-graph';
import { UnsupportedOperationError } from '@finos/legend-shared';
import { flowResult } from 'mobx';
Expand All @@ -47,8 +49,8 @@ export const getMappingElementSourceFilterText = (
return val.path;
} else if (val instanceof RootFlatDataRecordType) {
return val.owner.name;
} else if (val instanceof Table || val instanceof View) {
return `${val.schema.owner.path}.${val.schema.name}.${val.name}`;
} else if (val instanceof TableAlias) {
return `${val.relation.ownerReference.value.path}.${val.relation.value.schema.name}.${val.relation.value.name}`;
}
throw new UnsupportedOperationError();
};
Expand All @@ -67,12 +69,12 @@ export const getSourceElementLabel = (
sourceLabel = srcElement.name;
} else if (srcElement instanceof RootFlatDataRecordType) {
sourceLabel = srcElement.owner.name;
} else if (srcElement instanceof Table || srcElement instanceof View) {
sourceLabel = `${srcElement.schema.owner.name}.${
srcElement.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
} else if (srcElement instanceof TableAlias) {
sourceLabel = `${srcElement.relation.ownerReference.value.name}.${
srcElement.relation.value.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
? ''
: `${srcElement.schema.name}.`
}${srcElement.name}`;
: `${srcElement.relation.value.schema.name}.`
}${srcElement.relation.value.name}`;
}
return sourceLabel;
};
Expand All @@ -89,13 +91,13 @@ export const buildMappingElementSourceOption = (
label: `${source.owner.owner.name}.${source.owner.name}`,
value: source,
};
} else if (source instanceof Table || source instanceof View) {
} else if (source instanceof TableAlias) {
return {
label: `${source.schema.owner.name}.${
source.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
label: `${source.relation.ownerReference.value.name}.${
source.relation.value.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
? ''
: `${source.schema.name}.`
}${source.name}`,
: `${source.relation.value.schema.name}.`
}${source.relation.value.name}`,
value: source,
};
}
Expand Down Expand Up @@ -131,11 +133,21 @@ export const InstanceSetImplementationSourceSelectorModal = observer(
),
)
.concat(
editorStore.graphManagerState.graph.ownDatabases.flatMap((e) =>
e.schemas.flatMap((schema) =>
(schema.tables as (Table | View)[]).concat(schema.views),
),
),
editorStore.graphManagerState.graph.ownDatabases
.flatMap((e) =>
e.schemas.flatMap((schema) =>
(schema.tables as (Table | View)[]).concat(schema.views),
),
)
.map((relation) => {
const mainTableAlias = new TableAlias();
mainTableAlias.relation =
relation instanceof Table
? TableExplicitReference.create(relation)
: ViewExplicitReference.create(relation);
mainTableAlias.name = mainTableAlias.relation.value.name;
return mainTableAlias;
}),
)
.map(buildMappingElementSourceOption);
const filterOption = createFilter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ import {
FlatData,
FlatDataConnection,
PackageableElementExplicitReference,
Table,
View,
Database,
TableAlias,
DatabaseType,
RelationalDatabaseConnection,
StaticDatasourceSpecification,
Expand All @@ -77,8 +76,8 @@ export const getClassMappingStore = (
return graph.modelStore;
} else if (sourceElement instanceof RootFlatDataRecordType) {
return sourceElement.owner.owner;
} else if (sourceElement instanceof Table || sourceElement instanceof View) {
return sourceElement.schema.owner;
} else if (sourceElement instanceof TableAlias) {
return sourceElement.relation.ownerReference.value;
}
return undefined;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ import {
RootRelationalInstanceSetImplementation,
EmbeddedRelationalInstanceSetImplementation,
AggregationAwareSetImplementation,
Table,
View,
TableAlias,
TableExplicitReference,
ViewExplicitReference,
RelationalInputData,
RelationalInputType,
OperationSetImplementation,
Expand Down Expand Up @@ -153,8 +149,7 @@ export type MappingElementSource =
| Type
| Class
| RootFlatDataRecordType
| View
| Table;
| TableAlias;

/* @MARKER: NEW CLASS MAPPING TYPE SUPPORT --- consider adding class mapping type handler here whenever support for a new one is added to the app */
export const getMappingElementTarget = (
Expand Down Expand Up @@ -206,12 +201,11 @@ export const getMappingElementSource = (
} else if (
mappingElement instanceof RootRelationalInstanceSetImplementation
) {
return mappingElement.mainTableAlias?.relation.value;
return mappingElement.mainTableAlias;
} else if (
mappingElement instanceof EmbeddedRelationalInstanceSetImplementation
) {
return mappingElement.rootInstanceSetImplementation.mainTableAlias?.relation
.value;
return mappingElement.rootInstanceSetImplementation.mainTableAlias;
} else if (mappingElement instanceof AggregationAwareSetImplementation) {
return getMappingElementSource(
mappingElement.mainSetImplementation,
Expand Down Expand Up @@ -854,22 +848,15 @@ export class MappingEditorState extends ElementEditorState {
setImplementation.root,
OptionalPackageableElementExplicitReference.create(newSource),
);
} else if (newSource instanceof Table || newSource instanceof View) {
} else if (newSource instanceof TableAlias) {
const newRootRelationalInstanceSetImplementation =
new RootRelationalInstanceSetImplementation(
setImplementation.id,
this.mapping,
setImplementation.class,
setImplementation.root,
);
const mainTableAlias = new TableAlias();
mainTableAlias.relation =
newSource instanceof Table
? TableExplicitReference.create(newSource)
: ViewExplicitReference.create(newSource);
mainTableAlias.name = mainTableAlias.relation.value.name;
newRootRelationalInstanceSetImplementation.mainTableAlias =
mainTableAlias;
newRootRelationalInstanceSetImplementation.mainTableAlias = newSource;
newSetImp = newRootRelationalInstanceSetImplementation;
} else {
throw new UnsupportedOperationError(
Expand Down Expand Up @@ -1396,9 +1383,11 @@ export class MappingEditorState extends ElementEditorState {
PackageableElementExplicitReference.create(source.owner.owner),
createMockDataForMappingElementSource(source, this.editorStore),
);
} else if (source instanceof Table || source instanceof View) {
} else if (source instanceof TableAlias) {
inputData = new RelationalInputData(
PackageableElementExplicitReference.create(source.schema.owner),
PackageableElementExplicitReference.create(
source.relation.ownerReference.value,
),
createMockDataForMappingElementSource(source, this.editorStore),
RelationalInputType.SQL,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import type {
Connection,
ExecutionResult,
SetImplementation,
Table,
View,
} from '@finos/legend-graph';
import {
extractExecutionResultValues,
Expand All @@ -80,8 +82,6 @@ import {
PureSingleExecution,
RootFlatDataRecordType,
PackageableElementExplicitReference,
Table,
View,
DatabaseType,
RelationalDatabaseConnection,
LocalH2DatasourceSpecification,
Expand All @@ -91,6 +91,7 @@ import {
OperationSetImplementation,
buildSourceInformationSourceId,
PureClientVersion,
TableAlias,
} from '@finos/legend-graph';
import {
ActionAlertActionType,
Expand Down Expand Up @@ -507,11 +508,11 @@ export class MappingExecutionState {
);
}
this.setInputDataState(newRuntimeState);
} else if (source instanceof Table || source instanceof View) {
} else if (source instanceof TableAlias) {
const newRuntimeState = new MappingExecutionRelationalInputDataState(
this.editorStore,
this.mappingEditorState.mapping,
source,
source.relation.value,
);
if (populateWithMockData) {
newRuntimeState.inputData.setData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ import {
RelationalDatabaseConnection,
LocalH2DatasourceSpecification,
DefaultH2AuthenticationStrategy,
Table,
View,
buildSourceInformationSourceId,
PureClientVersion,
TableAlias,
} from '@finos/legend-graph';
import { LambdaEditorState, TAB_SIZE } from '@finos/legend-application';

Expand Down Expand Up @@ -520,13 +519,13 @@ export class MappingTestState {
);
}
this.setInputDataState(newInputDataState);
} else if (source instanceof Table || source instanceof View) {
} else if (source instanceof TableAlias) {
const newInputDataState = new MappingTestRelationalInputDataState(
this.editorStore,
this.mappingEditorState.mapping,
new RelationalInputData(
PackageableElementExplicitReference.create(
guaranteeNonNullable(source.schema.owner),
source.relation.ownerReference.value,
),
'',
RelationalInputType.SQL,
Expand Down