Skip to content

Commit 261134a

Browse files
authored
feat: Add configurable prop types to neo4j csv publisher (#1993)
* Add configurable prop types to neo4j csv publisher Signed-off-by: Kristen Armes <[email protected]> * Bump databuilder version Signed-off-by: Kristen Armes <[email protected]> Signed-off-by: Kristen Armes <[email protected]>
1 parent 25c4a8d commit 261134a

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

databuilder/databuilder/publisher/neo4j_csv_unwind_publisher.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def init(self, conf: ConfigTree) -> None:
9191
self._publish_reverse_relationships: bool = conf.get_bool(PublishBehaviorConfigs.PUBLISH_REVERSE_RELATIONSHIPS)
9292
self._preserve_adhoc_ui_data = conf.get_bool(PublishBehaviorConfigs.PRESERVE_ADHOC_UI_DATA)
9393
self._preserve_empty_props: bool = conf.get_bool(PublishBehaviorConfigs.PRESERVE_EMPTY_PROPS)
94+
self._prop_types_to_configure: Dict =\
95+
dict(conf.get(Neo4jCsvPublisherConfigs.NEO4J_PROP_TYPES_TO_CONFIGURE, default={}))
9496
if self._add_publisher_metadata and not self._publish_tag:
9597
raise Exception(f'{PublisherConfigs.JOB_PUBLISH_TAG} should not be empty')
9698

@@ -317,23 +319,38 @@ def _create_props_body(self,
317319
template = Template("""
318320
{% for k in record_keys %}
319321
{% if preserve_empty_props %}
320-
{{ identifier }}.{{ k }} = row.{{ k }}
322+
{% if k in prop_types_to_configure %}
323+
{{ identifier }}.{{ k }} = {{ prop_types_to_configure[k] }}(row.{{ k }})
324+
{% else %}
325+
{{ identifier }}.{{ k }} = row.{{ k }}
326+
{% endif %}
321327
{% else %}
322-
{{ identifier }}.{{ k }} = (CASE row.{{ k }} WHEN '' THEN NULL ELSE row.{{ k }} END)
328+
{% if k in prop_types_to_configure %}
329+
{{ identifier }}.{{ k }} =
330+
(CASE row.{{ k }} WHEN '' THEN NULL ELSE {{ prop_types_to_configure[k] }}(row.{{ k }}) END)
331+
{% else %}
332+
{{ identifier }}.{{ k }} = (CASE row.{{ k }} WHEN '' THEN NULL ELSE row.{{ k }} END)
333+
{% endif %}
323334
{% endif %}
324335
{{ ", " if not loop.last else "" }}
325336
{% endfor %}
326337
{% if record_keys and add_publisher_metadata %}
327338
,
328339
{% endif %}
329340
{% if add_publisher_metadata %}
330-
{{ identifier }}.{{ published_tag_prop }} = '{{ publish_tag }}',
341+
{% if published_tag_prop in prop_types_to_configure %}
342+
{{ identifier }}.{{ published_tag_prop }} =
343+
{{ prop_types_to_configure[published_tag_prop] }}('{{ publish_tag }}'),
344+
{% else %}
345+
{{ identifier }}.{{ published_tag_prop }} = '{{ publish_tag }}',
346+
{% endif %}
331347
{{ identifier }}.{{ last_updated_prop }} = timestamp()
332348
{% endif %}
333349
""")
334350

335351
props_body = template.render(record_keys=record_keys,
336352
preserve_empty_props=self._preserve_empty_props,
353+
prop_types_to_configure=self._prop_types_to_configure,
337354
identifier=identifier,
338355
add_publisher_metadata=self._add_publisher_metadata,
339356
published_tag_prop=PublisherConfigs.PUBLISHED_TAG_PROPERTY_NAME,

databuilder/databuilder/publisher/publisher_config_constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ class Neo4jCsvPublisherConfigs:
6464
# NEO4J_VALIDATE_SSL is a boolean indicating whether to validate the server's SSL/TLS
6565
# cert against system CAs
6666
NEO4J_VALIDATE_SSL = 'neo4j_validate_ssl'
67+
68+
# This should be a dict using property names as keys mapped to the function name used to configure a specific
69+
# type for that property. The values of the properties should be in the correct format that the function accepts.
70+
# Example: a config of {'start_time': 'datetime', 'publish_tag': 'date'} where the property values are in the
71+
# format <date>T<time> and <date> would apply datetime(n.start_time) and date(n.publish_tag) in the prop merge
72+
# statement to create the props as DateTime and Date types instead of strings.
73+
NEO4J_PROP_TYPES_TO_CONFIGURE = 'neo4j_prop_types_to_configure'

databuilder/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from setuptools import find_packages, setup
77

8-
__version__ = '7.3.0'
8+
__version__ = '7.4.0'
99

1010
requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
1111
'requirements.txt')

0 commit comments

Comments
 (0)