From 6b9e78bbf2f46b13489db1e4e2f9126f1e263cea Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 17 Oct 2023 14:45:37 +0200 Subject: [PATCH] database:columns - if the schema is empty, set the column to text and use json dumps/loads --- acacore/database/column.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/acacore/database/column.py b/acacore/database/column.py index cae16ba..c133906 100644 --- a/acacore/database/column.py +++ b/acacore/database/column.py @@ -1,4 +1,6 @@ from datetime import datetime +from json import dumps +from json import loads from pathlib import Path from typing import Callable from typing import Generic @@ -59,9 +61,12 @@ def _schema_to_column(name: str, schema: dict, defs: Optional[dict[str, dict]] = else: raise TypeError(f"Cannot recognize type from schema {schema!r}") elif schema_any_of: - if (schema_any_of[-1].get("type", None) != "null" and len(schema_any_of) > 1) or len(schema_any_of) > 2: + if not schema_any_of[0]: + sql_type, to_entry, from_entry = "text", lambda x: dumps(x, default=str), lambda x: loads(x, default=str) + elif (schema_any_of[-1].get("type", None) != "null" and len(schema_any_of) > 1) or len(schema_any_of) > 2: raise TypeError(f"Cannot recognize type from schema {schema!r}") - return _schema_to_column(name, {**schema_any_of[0], **schema}, defs) + else: + return _schema_to_column(name, {**schema_any_of[0], **schema}, defs) else: raise TypeError(f"Cannot recognize type from schema {schema!r}")