41
41
class JSONSchemaToPostgres (JSONSchemaToSQL ):
42
42
"""Convert JSON Schema types to Postgres types."""
43
43
44
- def __init__ (self , * , content_encoding : bool = True ) -> None :
44
+ def __init__ (self , * , content_encoding : bool = True , ** kwargs ) :
45
45
"""Initialize the JSONSchemaToPostgres instance."""
46
- super ().__init__ ()
46
+ super ().__init__ (** kwargs )
47
47
self .content_encoding = content_encoding
48
48
49
+ @classmethod
50
+ def from_config (
51
+ cls : type [JSONSchemaToPostgres ],
52
+ config : dict ,
53
+ * ,
54
+ max_varchar_length : int | None = None ,
55
+ ) -> JSONSchemaToPostgres :
56
+ """Create a JSONSchemaToPostgres instance from a configuration."""
57
+ return cls (
58
+ content_encoding = config .get ("interpret_content_encoding" , False ),
59
+ max_varchar_length = max_varchar_length ,
60
+ )
61
+
49
62
def handle_raw_string (self , schema ):
50
63
"""Handle a raw string type."""
51
64
if self .content_encoding and schema .get ("contentEncoding" ) == "base16" :
@@ -63,6 +76,8 @@ class PostgresConnector(SQLConnector):
63
76
allow_merge_upsert : bool = True # Whether MERGE UPSERT is supported.
64
77
allow_temp_tables : bool = True # Whether temp tables are supported.
65
78
79
+ jsonschema_to_sql_converter = JSONSchemaToPostgres
80
+
66
81
def __init__ (self , config : dict ) -> None :
67
82
"""Initialize a connector to a Postgres database.
68
83
@@ -100,18 +115,6 @@ def __init__(self, config: dict) -> None:
100
115
sqlalchemy_url = url .render_as_string (hide_password = False ),
101
116
)
102
117
103
- @cached_property
104
- def interpret_content_encoding (self ) -> bool :
105
- """Whether to interpret schema contentEncoding to set the column type.
106
-
107
- It is an opt-in feature because it might result in data loss if the
108
- actual data does not match the schema's advertised encoding.
109
-
110
- Returns:
111
- True if the feature is enabled, False otherwise.
112
- """
113
- return self .config .get ("interpret_content_encoding" , False )
114
-
115
118
def prepare_table ( # type: ignore[override] # noqa: PLR0913
116
119
self ,
117
120
full_table_name : str | FullyQualifiedName ,
@@ -258,7 +261,10 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
258
261
@cached_property
259
262
def jsonschema_to_sql (self ) -> JSONSchemaToSQL :
260
263
"""Return a JSONSchemaToSQL instance with custom type handling."""
261
- to_sql = JSONSchemaToPostgres (content_encoding = self .interpret_content_encoding )
264
+ to_sql = JSONSchemaToPostgres .from_config (
265
+ self .config ,
266
+ max_varchar_length = self .max_varchar_length ,
267
+ )
262
268
to_sql .fallback_type = TEXT
263
269
to_sql .register_type_handler ("integer" , BIGINT )
264
270
to_sql .register_type_handler ("object" , JSONB )
0 commit comments