def to_mutation(row): id, payload, timestamp, transaction = row version, payload = payload.split(':', 1) if version != '0': raise RuntimeError('Cannot parse payload version: %s', version) (schema, table), operation, primary_key_columns, (old, new), configuration_version = pickle.loads(payload) states = {} if old: states['old'] = row_converter.to_protobuf(old) if new: states['new'] = row_converter.to_protobuf(new) assert states, 'at least one state must be set' return MutationOperation( id=id, schema=schema, table=table, operation=getattr(MutationOperation, operation), identity_columns=primary_key_columns, timestamp=to_timestamp(timestamp), transaction=transaction, **states )
def to_mutation(row): id, payload, timestamp, transaction = row version, payload = payload.split(':', 1) if version != '0': raise RuntimeError('Cannot parse payload version: %s', version) (schema, table), operation, primary_key_columns, ( old, new), configuration_version = pickle.loads(payload) states = {} if old: states['old'] = row_converter.to_protobuf(old) if new: states['new'] = row_converter.to_protobuf(new) assert states, 'at least one state must be set' return MutationOperation(id=id, schema=schema, table=table, operation=getattr(MutationOperation, operation), identity_columns=primary_key_columns, timestamp=to_timestamp(timestamp), transaction=transaction, **states)
def loader(table): with connection_lock, connection.cursor('records', cursor_factory=NamedTupleCursor) as cursor: if table.columns: columns = ', '.join(map(quote, table.columns)) else: columns = '*' statement = 'SELECT {columns} FROM {schema}.{name}'.format( columns=columns, schema=quote(table.schema), name=quote(table.name), ) cursor.execute(statement) for row in cursor: converted = row_converter.to_protobuf(row._asdict()) # XXX: This is necessary because of a bug in protocol buffer oneof. yield type(converted).FromString(converted.SerializeToString())