File tree Expand file tree Collapse file tree
apps/bfd-pipeline/bfd-pipeline-idr Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import logging
22import os
3- import string
43from collections .abc import Iterator
54from datetime import UTC , date , datetime
65
@@ -132,7 +131,7 @@ def load(
132131 with cur .copy (f"COPY { temp_table } ({ cols_str } ) FROM STDIN" ) as copy : # type: ignore
133132 for row in results :
134133 model_dump = row .model_dump ()
135- copy .write_row ([_remove_non_printable (model_dump [k ]) for k in insert_cols ])
134+ copy .write_row ([_remove_null_bytes (model_dump [k ]) for k in insert_cols ])
136135 copy_timer .stop ()
137136
138137 if len (results ) > 0 :
@@ -206,10 +205,15 @@ def load(
206205 return data_loaded
207206
208207
209- def _remove_non_printable (val : DbType ) -> DbType :
210- # Some IDR values have non-printable characters
208+ def _remove_null_bytes (val : DbType ) -> DbType :
209+ # Some IDR strings have null bytes.
210+ # Postgres doesn't allow these in text fields.
211+ # We can't use a UTF-8 validator here since technically these are valid UTF-8
212+ # and we can't use string.printable because that only contains ASCII fields
213+ # so neither of those validation techniques will remove null bytes
214+ # and still allow other valid UTF-8 characters.
211215 if type (val ) is str :
212- return "" . join ( s for s in val if s in string . printable )
216+ return val . replace ( " \x00 " , "" )
213217 return val
214218
215219
You can’t perform that action at this time.
0 commit comments