From 8bee784aa62f9a1252c131bc8ddf40d284a819dc 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 | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 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..f619fd0353b 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,11 +4,10 @@ import re from alembic import op -from sqlalchemy import select +from sqlalchemy import Column, ForeignKey, Integer, UnicodeText, select +from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker -from h.models import Token, User - 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()