Skip to content

Commit

Permalink
cast query_results data back to text (#6713)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Chubatiuk <[email protected]>
  • Loading branch information
AndrewChubatiuk and Andrew Chubatiuk authored Jan 18, 2024
1 parent cd03da3 commit a54171f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ def upgrade():
type_=JSONB(astext_type=sa.Text()),
postgresql_using='layout::jsonb',
server_default=sa.text("'{}'::jsonb"))
op.alter_column('query_results', 'data',
existing_type=sa.Text(),
type_=JSONB(astext_type=sa.Text()),
nullable=True,
postgresql_using='data::jsonb',
server_default=sa.text("'{}'::jsonb"))
op.alter_column('changes', 'change',
existing_type=JSON(astext_type=sa.Text()),
type_=JSONB(astext_type=sa.Text()),
Expand Down Expand Up @@ -124,11 +118,6 @@ def downgrade():
type_=sa.Text(),
postgresql_using='layout::text',
server_default=sa.text("'{}'::text"))
op.alter_column('query_results', 'data',
existing_type=JSONB(astext_type=sa.Text()),
type_=sa.Text(),
postgresql_using='data::text',
server_default=sa.text("'{}'::text"))
op.alter_column('changes', 'change',
existing_type=JSONB(astext_type=sa.Text()),
type_=JSON(astext_type=sa.Text()),
Expand Down
3 changes: 2 additions & 1 deletion redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from redash.models.types import (
Configuration,
EncryptedConfiguration,
JSONText,
MutableDict,
MutableList,
json_cast_property,
Expand Down Expand Up @@ -315,7 +316,7 @@ class QueryResult(db.Model, BelongsToOrgMixin):
data_source = db.relationship(DataSource, backref=backref("query_results"))
query_hash = Column(db.String(32), index=True)
query_text = Column("query", db.Text)
data = Column(MutableDict.as_mutable(JSONB), nullable=True)
data = Column(JSONText, nullable=True)
runtime = Column(DOUBLE_PRECISION)
retrieved_at = Column(db.DateTime(True))

Expand Down
17 changes: 17 additions & 0 deletions redash/models/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy.types import TypeDecorator
from sqlalchemy_utils import EncryptedType

from redash.utils import json_dumps, json_loads
from redash.utils.configuration import ConfigurationContainer

from .base import db
Expand All @@ -28,6 +29,22 @@ def process_result_value(self, value, dialect):
)


# Utilized for cases when JSON size is bigger than JSONB (255MB) or JSON (10MB) limit
class JSONText(TypeDecorator):
impl = db.Text

def process_bind_param(self, value, dialect):
if value is None:
return value

return json_dumps(value)

def process_result_value(self, value, dialect):
if not value:
return value
return json_loads(value)


class MutableDict(Mutable, dict):
@classmethod
def coerce(cls, key, value):
Expand Down

0 comments on commit a54171f

Please sign in to comment.