-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JSON type handling by moving to aana.storage.types and remov…
…ing custom_types module
- Loading branch information
Aleksandr Movchan
committed
Dec 13, 2024
1 parent
25662c9
commit 6a411fa
Showing
6 changed files
with
44 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
from typing import TypeAlias | ||
|
||
from sqlalchemy import String | ||
import orjson | ||
from snowflake.sqlalchemy.custom_types import VARIANT as SnowflakeVariantType | ||
from sqlalchemy import String, func | ||
from sqlalchemy.types import JSON as SqlAlchemyJSON | ||
from sqlalchemy.types import TypeDecorator | ||
|
||
MediaIdSqlType: TypeAlias = String(36) | ||
|
||
|
||
class VARIANT(SnowflakeVariantType): | ||
"""Extends VARIANT type for better SqlAlchemy support.""" | ||
|
||
def bind_expression(self, bindvalue): | ||
"""Wraps value with PARSE_JSON for Snowflake.""" | ||
return func.PARSE_JSON(bindvalue) | ||
|
||
def result_processor(self, dialect, coltype): | ||
"""Convert JSON string to Python dictionary when retrieving.""" | ||
|
||
def process(value): | ||
if value is None: | ||
return None | ||
try: | ||
return orjson.loads(value) | ||
except (ValueError, TypeError): | ||
return value # Return raw value if not valid JSON | ||
|
||
return process | ||
|
||
|
||
class JSON(TypeDecorator): | ||
"""Custom JSON type that supports Snowflake-specific and standard dialects.""" | ||
|
||
impl = SqlAlchemyJSON # Default to standard SQLAlchemy JSON | ||
# impl = VARIANT # Default to Snowflake VARIANT | ||
|
||
def load_dialect_impl(self, dialect): | ||
"""Load dialect-specific implementation.""" | ||
if dialect.name == "snowflake": | ||
return VARIANT() | ||
else: | ||
return SqlAlchemyJSON() |