From 044224bdd3c5cbb0c616be66bdac7752092de304 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Thu, 22 Feb 2024 11:47:09 +0000 Subject: [PATCH] Fix migration error in `token` due to missing `userid` field Replace imports from `h.models` with local ORM classes. The ORM classes in `h.models` assume the DB has the latest schema, which is not the case when running migrations. The new code matches how other migrations work. Fixes https://github.com/hypothesis/h/issues/8541 --- ...713b_back_fill_the_token_user_id_column.py | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/h/migrations/versions/8e3417e3713b_back_fill_the_token_user_id_column.py b/h/migrations/versions/8e3417e3713b_back_fill_the_token_user_id_column.py index 3255df65133..98cfc5d7ec8 100644 --- a/h/migrations/versions/8e3417e3713b_back_fill_the_token_user_id_column.py +++ b/h/migrations/versions/8e3417e3713b_back_fill_the_token_user_id_column.py @@ -4,10 +4,9 @@ import re from alembic import op -from sqlalchemy import select -from sqlalchemy.orm import sessionmaker - -from h.models import Token, User +from sqlalchemy import Column, ForeignKey, Integer, UnicodeText, select +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import Mapped, sessionmaker revision = "8e3417e3713b" down_revision = "8fcdcefd8c6f" @@ -16,6 +15,29 @@ log = logging.getLogger(__name__) +Base = declarative_base() + + +class Token(Base): + __tablename__ = "token" + + id = Column(Integer, primary_key=True) + + # Legacy `userid` column. + userid = Column(UnicodeText()) + + # Replacement foreign key. + user_id = Column(Integer, ForeignKey("user.id")) + + +class User(Base): + __tablename__ = "user" + + id = Column(Integer, primary_key=True) + username = Column(UnicodeText(), nullable=False) + authority = Column(UnicodeText(), nullable=False) + + def split_userid(userid): return re.match(r"^acct:([^@]+)@(.*)$", userid).groups()