Skip to content

Commit

Permalink
fix: remove field name customization
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx committed Aug 16, 2024
1 parent abeadca commit 5d49daf
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
5 changes: 1 addition & 4 deletions src/adapt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ export {

export {convertBigQuerySchemaToStorageTableSchema} from './schema';

export {
withChangeType,
withChangeSequenceNumber,
} from './options';
export {withChangeType, withChangeSequenceNumber} from './options';
19 changes: 13 additions & 6 deletions src/adapt/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@
// limitations under the License.

export type AdaptOptions = {
changeSequenceNumberFieldName: string;
addChangeSequenceNumber: boolean;
changeTypeFieldName: string;
addChangeType: boolean;
};

export type AdaptOption = (opts: AdaptOptions) => AdaptOptions;

export function withChangeType(fieldName?: string): AdaptOption {
/**
* Add pseudocolumn `_CHANGE_TYPE` for BigQuery Change Data Capture.
* Used to define the type of change to be professed for each row.
* The pseudocolumn `_CHANGE_TYPE` only accepts the values UPSERT and DELETE.
* See more: https://cloud.google.com/bigquery/docs/change-data-capture#specify_changes_to_existing_records
*/
export function withChangeType(): AdaptOption {
return (opts: AdaptOptions) => ({
...opts,
changeTypeFieldName: fieldName || 'changeType',
addChangeType: true,
});
}

export function withChangeSequenceNumber(fieldName?: string): AdaptOption {
/**
* Add pseudocolumn `_CHANGE_SEQUENCE_NUMBER` for BigQuery Change Data Capture.
* Used to change behavior of ordering records with same primary key.
* See more: https://cloud.google.com/bigquery/docs/change-data-capture#manage_custom_ordering
*/
export function withChangeSequenceNumber(): AdaptOption {
return (opts: AdaptOptions) => ({
...opts,
changeSequenceNumberFieldName: fieldName || 'changeSequenceNumber',
addChangeSequenceNumber: true,
});
}
12 changes: 2 additions & 10 deletions src/adapt/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ function convertStorageSchemaToFileDescriptorInternal(
...opts: AdaptOption[]
): FileDescriptorSet {
let adaptOpts: AdaptOptions = {
changeSequenceNumberFieldName: 'changeSequenceNumber',
addChangeSequenceNumber: false,
changeTypeFieldName: 'changeType',
addChangeType: false,
};
opts.forEach(f => {
Expand Down Expand Up @@ -177,30 +175,24 @@ function convertStorageSchemaToFileDescriptorInternal(
name: '_CHANGE_SEQUENCE_NUMBER',
type: 'STRING',
mode: 'REQUIRED',
description:
'pseudocolumn only accepts values, written in a fixed format written in hexadecimal, separated into sections by a forward slash',
},
991,
scope,
useProto3
);
//fdp.jsonName = adaptOpts.changeSequenceNumberFieldName;
fields.push(fdp);
}
if (adaptOpts.addChangeType) {
const fdp = convertTableFieldSchemaToFieldDescriptorProto(
{
name: '_CHANGE_TYPE',
type: 'STRING',
mode: 'REQUIRED',
description:
'pseudocolumn to indicate the type of change. Only accepts `INSERT`, `UPSERT` and `DELETE`',
mode: 'REQUIRED',
},
992,
scope,
useProto3
);
fdp.jsonName = adaptOpts.changeTypeFieldName;
fields.push(fdp);
}
}
Expand Down Expand Up @@ -330,7 +322,7 @@ function convertTableFieldSchemaToFieldDescriptorProto(
type: pType,
label: label,
options: {
packed: shouldPackType(pType, label, useProto3),
packed: shouldPackType(pType, label, useProto3),
},
proto3Optional: isProto3Optional(label, useProto3),
});
Expand Down
2 changes: 1 addition & 1 deletion system-test/managed_writer_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ describe('managedwriter.WriterClient', () => {
adapt.convertStorageSchemaToProto2Descriptor(
storageSchema,
'root',
adapt.withChangeType('_CHANGE_TYPE')
adapt.withChangeType()
);

const row1 = {
Expand Down
8 changes: 4 additions & 4 deletions test/adapt/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ describe('Adapt Protos', () => {
const protoDescriptor = adapt.convertStorageSchemaToProto2Descriptor(
storageSchema,
'Test',
adapt.withChangeType('change'),
adapt.withChangeSequenceNumber('seq')
adapt.withChangeType(),
adapt.withChangeSequenceNumber()
);
assert.notEqual(protoDescriptor, null);
if (!protoDescriptor) {
Expand All @@ -106,8 +106,8 @@ describe('Adapt Protos', () => {
const raw = {
id: 1,
username: 'Alice',
change: 'INSERT',
seq: 'FF',
_CHANGE_TYPE: 'INSERT',
_CHANGE_SEQUENCE_NUMBER: 'FF',
};
const serialized = TestProto.encode(raw).finish();
const decoded = TestProto.decode(serialized).toJSON();
Expand Down

0 comments on commit 5d49daf

Please sign in to comment.