Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move user profile image url into the details field #5697

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions migrations/versions/fd4fc850d7ea_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Convert user details to jsonb and move user profile image url into details column

Revision ID: fd4fc850d7ea
Revises: 89bc7873a3e0
Create Date: 2022-01-31 15:24:16.507888

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from redash.models import db

# revision identifiers, used by Alembic.
revision = 'fd4fc850d7ea'
down_revision = '89bc7873a3e0'
branch_labels = None
depends_on = None


def upgrade():
connection = op.get_bind()

### commands auto generated by Alembic - please adjust! ###
op.alter_column('users', 'details',
existing_type=postgresql.JSON(astext_type=sa.Text()),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True,
existing_server_default=sa.text("'{}'::jsonb"))
### end Alembic commands ###

update_query = """
update users
set details = details::jsonb || ('{"profile_image_url": "' || profile_image_url || '"}')::jsonb
where 1=1
"""
connection.execute(update_query)
op.drop_column("users", "profile_image_url")


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()
op.add_column("users", sa.Column("profile_image_url", db.String(320), nullable=True))

update_query = """
update users set
profile_image_url = details->>'profile_image_url',
details = details - 'profile_image_url' ;
"""

connection.execute(update_query)
db.session.commit()
op.alter_column('users', 'details',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=postgresql.JSON(astext_type=sa.Text()),
existing_nullable=True,
existing_server_default=sa.text("'{}'::json"))

# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ def all(cls, org, group_ids, user_id):
query = (
Dashboard.query.options(
joinedload(Dashboard.user).load_only(
"id", "name", "_profile_image_url", "email"
"id", "name", "details", "email"
)
).distinct(Dashboard.created_at, Dashboard.slug)
.outerjoin(Widget)
Expand Down
6 changes: 4 additions & 2 deletions redash/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class User(
org = db.relationship("Organization", backref=db.backref("users", lazy="dynamic"))
name = Column(db.String(320))
email = Column(EmailType)
_profile_image_url = Column("profile_image_url", db.String(320), nullable=True)
password_hash = Column(db.String(128), nullable=True)
group_ids = Column(
"groups", MutableList.as_mutable(postgresql.ARRAY(key_type("Group"))), nullable=True
Expand All @@ -94,14 +93,17 @@ class User(

disabled_at = Column(db.DateTime(True), default=None, nullable=True)
details = Column(
MutableDict.as_mutable(postgresql.JSON),
MutableDict.as_mutable(postgresql.JSONB),
nullable=True,
server_default="{}",
default={},
)
active_at = json_cast_property(
db.DateTime(True), "details", "active_at", default=None
)
_profile_image_url = json_cast_property(
db.Text(), "details", "profile_image_url", default=None
)
is_invitation_pending = json_cast_property(
db.Boolean(True), "details", "is_invitation_pending", default=False
)
Expand Down