Skip to content

Commit

Permalink
feat: enable field selection on disabled stream (#14196)
Browse files Browse the repository at this point in the history
  • Loading branch information
dizel852 committed Oct 1, 2024
1 parent 39a89c5 commit 156f8f9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
isCursor as checkIsCursor,
isPrimaryKey as checkIsPrimaryKey,
} from "../../../syncCatalog/StreamFieldsTable/StreamFieldsTable";
import { updateFieldSelected } from "../../../syncCatalog/SyncCatalog/streamConfigHelpers";
import { getSelectedMandatoryFields, updateFieldSelected } from "../../../syncCatalog/SyncCatalog/streamConfigHelpers";
import { getFieldPathDisplayName } from "../../../syncCatalog/utils";
import { SyncStreamFieldWithId } from "../../formConfig";
import { SyncCatalogUIModel } from "../SyncCatalogTable";
Expand Down Expand Up @@ -64,13 +64,11 @@ export const StreamFieldNameCell: React.FC<StreamFieldNameCellProps> = ({
const isHashed = checkIsFieldHashed(field, config);

const isDisabled =
!config?.selected ||
mode === "readonly" ||
(config.syncMode === SyncMode.incremental && (isCursor || isChildFieldCursor)) ||
(config.destinationSyncMode === DestinationSyncMode.append_dedup && (isPrimaryKey || isChildFieldPrimaryKey)) ||
(config.destinationSyncMode === DestinationSyncMode.overwrite_dedup && (isPrimaryKey || isChildFieldPrimaryKey)) ||
isNestedField;
const showTooltip = isDisabled && mode !== "readonly" && config?.selected;
config?.selected &&
((config.syncMode === SyncMode.incremental && (isCursor || isChildFieldCursor)) ||
(config.destinationSyncMode === DestinationSyncMode.append_dedup && (isPrimaryKey || isChildFieldPrimaryKey)) ||
(config.destinationSyncMode === DestinationSyncMode.overwrite_dedup && (isPrimaryKey || isChildFieldPrimaryKey)));
const showTooltip = isDisabled && mode !== "readonly";

const isFieldSelected = checkIsFieldSelected(field, config);

Expand Down Expand Up @@ -98,9 +96,15 @@ export const StreamFieldNameCell: React.FC<StreamFieldNameCellProps> = ({
numberOfFieldsInStream,
});

const mandatorySelectedFields = getSelectedMandatoryFields(config);

updateStreamField(row.original.streamNode, {
...updatedConfig,
selectedFields: !updatedConfig?.fieldSelectionEnabled ? [] : updatedConfig?.selectedFields,
// any field selection immediately enables the disabled stream
...(isSelected && !config?.selected && { selected: true }),
selectedFields: !updatedConfig?.fieldSelectionEnabled
? []
: [...(updatedConfig?.selectedFields ?? []), ...mandatorySelectedFields],
// remove this field if it was part of hashedFields
hashedFields: config.hashedFields?.filter((f) => !isEqual(f.fieldPath, fieldPath)),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ export const StreamNameCell: React.FC<StreamNameCellProps> = ({
<CheckBox
checkboxSize="sm"
checked={config?.selected}
onChange={({ target: { checked } }) => updateStreamField(row.original.streamNode!, { selected: checked })}
onChange={({ target: { checked } }) =>
updateStreamField(row.original.streamNode!, {
selected: checked,
// enable/disable stream will enable/disable all fields
fieldSelectionEnabled: false,
selectedFields: [],
})
}
data-testid="sync-stream-checkbox"
/>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
updateCursorField,
updateFieldSelected,
toggleAllFieldsSelected,
getSelectedMandatoryFields,
} from "./streamConfigHelpers";

const FIELD_ONE: SyncSchemaField = {
Expand Down Expand Up @@ -405,6 +406,58 @@ describe(`${updateFieldSelected.name}`, () => {
});
});

describe(`${getSelectedMandatoryFields.name}`, () => {
it("returns an empty array if the stream selected(enabled)", () => {
const mandatoryFields = getSelectedMandatoryFields({ ...mockStreamConfiguration, selected: true });
expect(mandatoryFields).toEqual([]);
});

it("returns an empty array if sync mode is full_refresh", () => {
const mandatoryFields = getSelectedMandatoryFields({
...mockStreamConfiguration,
selected: false,
primaryKey: [FIELD_ONE.path, FIELD_TWO.path],
cursorField: FIELD_ONE.path,
syncMode: "full_refresh",
});

expect(mandatoryFields).toEqual([]);
});

it("returns the primary key fields if the destinationSyncMode is append_dedup", () => {
const mandatoryFields = getSelectedMandatoryFields({
...mockStreamConfiguration,
selected: false,
primaryKey: [FIELD_ONE.path, FIELD_TWO.path],
destinationSyncMode: "append_dedup",
});

expect(mandatoryFields).toEqual([{ fieldPath: FIELD_ONE.path }, { fieldPath: FIELD_TWO.path }]);
});

it("returns the primary key fields if the destinationSyncMode is overwrite_dedup", () => {
const mandatoryFields = getSelectedMandatoryFields({
...mockStreamConfiguration,
selected: false,
primaryKey: [FIELD_ONE.path, FIELD_TWO.path],
destinationSyncMode: "overwrite_dedup",
});

expect(mandatoryFields).toEqual([{ fieldPath: FIELD_ONE.path }, { fieldPath: FIELD_TWO.path }]);
});

it("returns the cursor field if the syncMode is incremental", () => {
const mandatoryFields = getSelectedMandatoryFields({
...mockStreamConfiguration,
selected: false,
cursorField: FIELD_ONE.path,
syncMode: "incremental",
});

expect(mandatoryFields).toEqual([{ fieldPath: FIELD_ONE.path }]);
});
});

describe(`${toggleAllFieldsSelected.name}`, () => {
it("unselects all fields if field selection was disabled", () => {
const newStreamConfiguration = toggleAllFieldsSelected({ ...mockStreamConfiguration });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ export function updateFieldSelected({
};
}

/**
* If one of the fields in the disabled stream is selected,
* we need to ensure that mandatory fields are also selected(PK, cursor)
* @param config
*/
export const getSelectedMandatoryFields = (config: AirbyteStreamConfiguration): SelectedFieldInfo[] => {
const mandatoryFields: string[][] = [];

if (!config?.selected) {
if (config?.primaryKey?.length && ["append_dedup", "overwrite_dedup"].includes(config.destinationSyncMode)) {
mandatoryFields.push(...config.primaryKey);
}

if (config?.cursorField?.length && config.syncMode === "incremental") {
mandatoryFields.push(config.cursorField);
}
}

return mandatoryFields.map((fieldPath) => ({ fieldPath }));
};

interface updateFieldHashingArguments {
config: AirbyteStreamConfiguration;
fieldPath: string[];
Expand Down

0 comments on commit 156f8f9

Please sign in to comment.