Skip to content

Commit

Permalink
Stop using size-limited string fields in db. Fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Nov 5, 2020
1 parent e1a2bc2 commit e07c37c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions mautrix/client/state_store/asyncpg/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ async def upgrade_v1(conn: Connection) -> None:
avatar_url VARCHAR(255),
PRIMARY KEY (room_id, user_id)
)""")


@upgrade_table.register(description="Stop using size-limited string fields")
async def upgrade_v2(conn: Connection) -> None:
await conn.execute("ALTER TABLE mx_room_state ALTER COLUMN room_id TYPE TEXT")
await conn.execute("ALTER TABLE mx_user_profile ALTER COLUMN room_id TYPE TEXT")
await conn.execute("ALTER TABLE mx_user_profile ALTER COLUMN user_id TYPE TEXT")
await conn.execute("ALTER TABLE mx_user_profile ALTER COLUMN displayname TYPE TEXT")
await conn.execute("ALTER TABLE mx_user_profile ALTER COLUMN avatar_url TYPE TEXT")
4 changes: 2 additions & 2 deletions mautrix/client/state_store/sqlalchemy/mx_room_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Optional, Type
import json

from sqlalchemy import Column, String, Boolean, types
from sqlalchemy import Column, Text, Boolean, types

from mautrix.types import (RoomID, PowerLevelStateEventContent as PowerLevels,
RoomEncryptionStateEventContent as EncryptionInfo, Serializable)
Expand Down Expand Up @@ -41,7 +41,7 @@ def process_literal_param(self, value, dialect):
class RoomState(Base):
__tablename__ = "mx_room_state"

room_id: RoomID = Column(String(255), primary_key=True)
room_id: RoomID = Column(Text, primary_key=True)
is_encrypted: bool = Column(Boolean, nullable=True)
has_full_member_list: bool = Column(Boolean, nullable=True)
encryption: EncryptionInfo = Column(SerializableType(EncryptionInfo), nullable=True)
Expand Down
10 changes: 5 additions & 5 deletions mautrix/client/state_store/sqlalchemy/mx_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Optional, Iterable, Dict

from sqlalchemy import Column, String, Enum
from sqlalchemy import Column, Text, Enum

from mautrix.types import RoomID, UserID, ContentURI, Member, Membership
from mautrix.util.db import Base
Expand All @@ -16,11 +16,11 @@
class UserProfile(Base):
__tablename__ = "mx_user_profile"

room_id: RoomID = Column(String(255), primary_key=True)
user_id: UserID = Column(String(255), primary_key=True)
room_id: RoomID = Column(Text, primary_key=True)
user_id: UserID = Column(Text, primary_key=True)
membership: Membership = Column(Enum(Membership), nullable=False, default=Membership.LEAVE)
displayname: str = Column(String, nullable=True)
avatar_url: ContentURI = Column(String(255), nullable=True)
displayname: str = Column(Text, nullable=True)
avatar_url: ContentURI = Column(Text, nullable=True)

def member(self) -> Member:
return Member(membership=self.membership, displayname=self.displayname,
Expand Down
16 changes: 16 additions & 0 deletions mautrix/crypto/store/asyncpg/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ async def add_account_id_column(table: str, pkey_columns: List[str]) -> None:
await add_account_id_column("crypto_olm_session", ["session_id"])
await add_account_id_column("crypto_megolm_inbound_session", ["session_id"])
await add_account_id_column("crypto_megolm_outbound_session", ["room_id"])


@upgrade_table.register(description="Stop using size-limited string fields")
async def upgrade_v3(conn: Connection) -> None:
await conn.execute("ALTER TABLE crypto_account ALTER COLUMN account_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_account ALTER COLUMN device_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_message_index ALTER COLUMN event_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_tracked_user ALTER COLUMN user_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_device ALTER COLUMN user_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_device ALTER COLUMN device_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_device ALTER COLUMN name TYPE TEXT")
await conn.execute("ALTER TABLE crypto_olm_session ALTER COLUMN account_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_megolm_inbound_session ALTER COLUMN account_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_megolm_inbound_session ALTER COLUMN room_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_megolm_outbound_session ALTER COLUMN account_id TYPE TEXT")
await conn.execute("ALTER TABLE crypto_megolm_outbound_session ALTER COLUMN room_id TYPE TEXT")

0 comments on commit e07c37c

Please sign in to comment.