Skip to content

Commit d063508

Browse files
committed
Made JSON column compatible with SQLite and PostgreSQL
1 parent 410c715 commit d063508

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

backend/models/project.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pydantic import BaseModel, Field, field_validator
77
from sqlalchemy import (
8+
JSON,
89
Boolean,
910
Column,
1011
DateTime,
@@ -66,6 +67,24 @@ def process_result_value(self, value, dialect):
6667
return value
6768

6869

70+
class CrossDatabaseJSON(TypeDecorator):
71+
"""
72+
Platform-independent JSON type.
73+
74+
Uses PostgreSQL's JSONB type for better performance,
75+
otherwise uses standard JSON type.
76+
"""
77+
78+
impl = JSON
79+
cache_ok = True
80+
81+
def load_dialect_impl(self, dialect):
82+
if dialect.name == "postgresql":
83+
return dialect.type_descriptor(JSONB())
84+
else:
85+
return dialect.type_descriptor(JSON())
86+
87+
6988
class ProjectStatusEnum(str, Enum):
7089
"""Project status enumeration"""
7190

@@ -90,7 +109,7 @@ class ProjectTable(Base):
90109
csv_path: Mapped[str] = mapped_column(Text, nullable=False)
91110
row_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
92111
column_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
93-
columns_metadata = Column(JSONB, nullable=True)
112+
columns_metadata = Column(CrossDatabaseJSON, nullable=True)
94113
status: Mapped[ProjectStatusEnum] = mapped_column(
95114
SQLEnum(ProjectStatusEnum), nullable=False, default=ProjectStatusEnum.UPLOADING
96115
)

0 commit comments

Comments
 (0)