Skip to content

Commit

Permalink
Refactor JSON type handling by moving to aana.storage.types and remov…
Browse files Browse the repository at this point in the history
…ing custom_types module
  • Loading branch information
Aleksandr Movchan committed Dec 13, 2024
1 parent 25662c9 commit 6a411fa
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion aana/alembic/versions/5ad873484aa3_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from alembic import op
from sqlalchemy.schema import CreateSequence, Sequence

from aana.storage.custom_types import JSON
from aana.storage.types import JSON

# revision identifiers, used by Alembic.
revision: str = "5ad873484aa3"
Expand Down
40 changes: 0 additions & 40 deletions aana/storage/custom_types.py

This file was deleted.

2 changes: 1 addition & 1 deletion aana/storage/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
)
from sqlalchemy.orm import Mapped, mapped_column

from aana.storage.custom_types import JSON
from aana.storage.models.base import BaseEntity, TimeStampEntity, timestamp
from aana.storage.types import JSON


class Status(str, Enum):
Expand Down
2 changes: 1 addition & 1 deletion aana/storage/models/transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from sqlalchemy import CheckConstraint, Sequence
from sqlalchemy.orm import Mapped, mapped_column

from aana.storage.custom_types import JSON
from aana.storage.models.base import BaseEntity, TimeStampEntity
from aana.storage.types import JSON

if TYPE_CHECKING:
from aana.core.models.asr import (
Expand Down
2 changes: 1 addition & 1 deletion aana/storage/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqlalchemy import create_engine, event

from aana.exceptions.runtime import EmptyMigrationsException
from aana.storage.custom_types import JSON
from aana.storage.types import JSON
from aana.utils.core import get_module_dir
from aana.utils.json import jsonify

Expand Down
41 changes: 40 additions & 1 deletion aana/storage/types.py
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()

0 comments on commit 6a411fa

Please sign in to comment.