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

🪟 Apply default sync mode logic to new streams #18451

Merged
merged 6 commits into from
Oct 27, 2022
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SyncSchema, SyncSchemaStream } from "core/domain/catalog";
import { DestinationSyncMode, SyncMode } from "core/request/AirbyteClient";
import { DestinationSyncMode, StreamDescriptor, SyncMode } from "core/request/AirbyteClient";

import calculateInitialCatalog from "./calculateInitialCatalog";

Expand All @@ -11,6 +11,7 @@ const mockSyncSchemaStream: SyncSchemaStream = {
sourceDefinedPrimaryKey: [["new_primary_key"]],
jsonSchema: {},
name: "test",
namespace: "namespace-test",
supportedSyncModes: [],
},
config: {
Expand Down Expand Up @@ -535,4 +536,60 @@ describe("calculateInitialCatalog", () => {
// cursor field
expect(calculatedStreams[0].config?.cursorField).toEqual(config?.cursorField);
});

it("should calculate optimal sync mode if stream is new", () => {
const { stream: sourceDefinedStream, config } = mockSyncSchemaStream;

const newStreamDescriptors: StreamDescriptor[] = [{ name: "test", namespace: "namespace-test" }];

const { streams: calculatedStreams } = calculateInitialCatalog(
{
streams: [
{
id: "1",
stream: {
...sourceDefinedStream,
name: "test",
namespace: "namespace-test",
sourceDefinedCursor: true,
defaultCursorField: ["id"],
supportedSyncModes: [SyncMode.incremental],
},
config: {
...config,
destinationSyncMode: DestinationSyncMode.overwrite,
syncMode: SyncMode.incremental,
},
},
{
id: "1",
stream: {
...sourceDefinedStream,
name: "test2",
namespace: "namespace-test",
sourceDefinedCursor: true,
defaultCursorField: ["id"],
supportedSyncModes: [SyncMode.incremental],
},
config: {
...config,
destinationSyncMode: DestinationSyncMode.overwrite,
syncMode: SyncMode.incremental,
},
},
],
},
[DestinationSyncMode.append_dedup],
true,
newStreamDescriptors
);

// new stream has its sync mode calculated
expect(calculatedStreams[0].config?.syncMode).toEqual(SyncMode.incremental);
expect(calculatedStreams[0].config?.destinationSyncMode).toEqual(DestinationSyncMode.append_dedup);

// existing stream remains as-is
expect(calculatedStreams[1].config?.syncMode).toEqual(SyncMode.incremental);
expect(calculatedStreams[1].config?.destinationSyncMode).toEqual(DestinationSyncMode.overwrite);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { SyncSchema, SyncSchemaStream } from "core/domain/catalog";
import { DestinationSyncMode, SyncMode, AirbyteStreamConfiguration } from "core/request/AirbyteClient";
import {
DestinationSyncMode,
SyncMode,
AirbyteStreamConfiguration,
StreamDescriptor,
} from "core/request/AirbyteClient";

const getDefaultCursorField = (streamNode: SyncSchemaStream): string[] => {
if (streamNode.stream?.defaultCursorField?.length) {
Expand Down Expand Up @@ -119,18 +124,24 @@ const getOptimalSyncMode = (
const calculateInitialCatalog = (
schema: SyncSchema,
supportedDestinationSyncModes: DestinationSyncMode[],
isNotCreateMode?: boolean
): SyncSchema => ({
streams: schema.streams.map<SyncSchemaStream>((apiNode, id) => {
const nodeWithId: SyncSchemaStream = { ...apiNode, id: id.toString() };
const nodeStream = verifySourceDefinedProperties(verifySupportedSyncModes(nodeWithId), isNotCreateMode || false);

if (isNotCreateMode) {
return nodeStream;
}

return getOptimalSyncMode(verifyConfigCursorField(nodeStream), supportedDestinationSyncModes);
}),
});
isNotCreateMode?: boolean,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: maybe a matter of preference, but I find negative names for booleans harder to read - in this case I would prefer !isCreateMode to isNotCreateMode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this variable is actually defined elsewhere like this, so I see why you used it! No need to change it.

newStreamDescriptors?: StreamDescriptor[]
): SyncSchema => {
return {
streams: schema.streams.map<SyncSchemaStream>((apiNode, id) => {
const nodeWithId: SyncSchemaStream = { ...apiNode, id: id.toString() };
const nodeStream = verifySourceDefinedProperties(verifySupportedSyncModes(nodeWithId), isNotCreateMode || false);

// if the stream is new since a refresh, we want to verify cursor and get optimal sync modes
const matches = newStreamDescriptors?.some(
(streamId) => streamId.name === nodeStream?.stream?.name && streamId.namespace === nodeStream.stream?.namespace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is streamId accurate? Maybe newStream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only contains the identifying information (name and namespace), not the entire stream object.

);
if (isNotCreateMode && !matches) {
return nodeStream;
}
return getOptimalSyncMode(verifyConfigCursorField(nodeStream), supportedDestinationSyncModes);
}),
};
};

export default calculateInitialCatalog;
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,21 @@ export const useInitialValues = (
destDefinition: DestinationDefinitionSpecificationRead,
isNotCreateMode?: boolean
): FormikConnectionFormValues => {
const { catalogDiff } = connection;

const newStreamDescriptors = catalogDiff?.transforms
.filter((transform) => transform.transformType === "add_stream")
.map((stream) => stream.streamDescriptor);

const initialSchema = useMemo(
() =>
calculateInitialCatalog(
connection.syncCatalog,
destDefinition?.supportedDestinationSyncModes || [],
isNotCreateMode
isNotCreateMode,
newStreamDescriptors
),
[connection.syncCatalog, destDefinition, isNotCreateMode]
[connection.syncCatalog, destDefinition?.supportedDestinationSyncModes, isNotCreateMode, newStreamDescriptors]
);

return useMemo(() => {
Expand Down