From fdc23b3107f5a8176bb2d7f71f57f0bb36d7d06b Mon Sep 17 00:00:00 2001 From: Son Date: Mon, 11 Oct 2021 11:30:10 +0200 Subject: [PATCH] add User.alternative_id column --- app/models.py | 11 ++++++ .../versions/2021_101111_2fbcad5527d7_.py | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 migrations/versions/2021_101111_2fbcad5527d7_.py diff --git a/app/models.py b/app/models.py index 28ad898b..0aea0193 100644 --- a/app/models.py +++ b/app/models.py @@ -350,6 +350,17 @@ class User(db.Model, ModelMixin, UserMixin, PasswordOracle): db.Boolean, default=False, nullable=False, server_default="0" ) + # used for flask-login as an "alternative token" + # cf https://flask-login.readthedocs.io/en/latest/#alternative-tokens + alternative_id = db.Column(db.String(128), unique=True, nullable=True) + + # implement flask-login "alternative token" + def get_id(self): + if self.alternative_id: + return self.alternative_id + else: + return str(self.id) + @classmethod def create(cls, email, name="", password=None, **kwargs): user: User = super(User, cls).create(email=email, name=name, **kwargs) diff --git a/migrations/versions/2021_101111_2fbcad5527d7_.py b/migrations/versions/2021_101111_2fbcad5527d7_.py new file mode 100644 index 00000000..eda06621 --- /dev/null +++ b/migrations/versions/2021_101111_2fbcad5527d7_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: 2fbcad5527d7 +Revises: 0b1c9ea11aef +Create Date: 2021-10-11 11:17:46.750252 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2fbcad5527d7' +down_revision = '0b1c9ea11aef' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('alternative_id', sa.String(length=128), nullable=True)) + op.create_unique_constraint(None, 'users', ['alternative_id']) + + # set alternative_id to id + op.execute('UPDATE users SET alternative_id = id') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'users', type_='unique') + op.drop_column('users', 'alternative_id') + # ### end Alembic commands ###