add User.alternative_id column

This commit is contained in:
Son 2021-10-11 11:30:10 +02:00
parent f525c951c6
commit fdc23b3107
2 changed files with 45 additions and 0 deletions

View File

@ -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)

View File

@ -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 ###