Skip to content

Commit a93a12b

Browse files
rubenvereeckenRuben Vereeckenedgarrmondragon
authored
feat: Support string and int arrays (#330)
Like the title said, this adds support for - `ARRAY(BIGINT())` - `ARRAY(TEXT())` Technically this is a breaking change. Where previously all array types were `ARRAY(JSONB())`, now there's more nuance. Unlikely that many people rely on this behaviour, but we had to write a migration. Should we increment the major version (or at least the minor) to signal this change? Or introduce a feature switch? --------- Co-authored-by: Ruben Vereecken <rubenvereecken@gmail.com> Co-authored-by: Edgar Ramírez Mondragón <16805946+edgarrmondragon@users.noreply.github.com>
1 parent 20142a1 commit a93a12b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

target_postgres/connector.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def prepare_table( # type: ignore[override]
143143
column_object = None
144144
if property_name in columns:
145145
column_object = columns[property_name]
146+
146147
self.prepare_column(
147148
full_table_name=table.fullname,
148149
column_name=property_name,
@@ -246,6 +247,14 @@ def to_sql_type(self, jsonschema_type: dict) -> sa.types.TypeEngine: # type: ig
246247
json_type_dict["format"] = jsonschema_type["format"]
247248
if encoding := jsonschema_type.get("contentEncoding", False):
248249
json_type_dict["contentEncoding"] = encoding
250+
# Figure out array type, but only if there's a single type
251+
# (no array union types)
252+
if (
253+
"items" in jsonschema_type
254+
and "type" in jsonschema_type["items"]
255+
and isinstance(jsonschema_type["items"]["type"], str)
256+
):
257+
json_type_dict["items"] = jsonschema_type["items"]["type"]
249258
json_type_array.append(json_type_dict)
250259
else:
251260
msg = "Invalid format for jsonschema type: not str or list."
@@ -282,7 +291,13 @@ def pick_individual_type(self, jsonschema_type: dict):
282291
if "object" in jsonschema_type["type"]:
283292
return JSONB()
284293
if "array" in jsonschema_type["type"]:
285-
return ARRAY(JSONB())
294+
items_type = jsonschema_type.get("items")
295+
if "string" == items_type:
296+
return ARRAY(TEXT())
297+
if "integer" == items_type:
298+
return ARRAY(BIGINT())
299+
else:
300+
return ARRAY(JSONB())
286301

287302
# string formats
288303
if jsonschema_type.get("format") == "date-time":

0 commit comments

Comments
 (0)