|
| 1 | +"""empty message |
| 2 | +
|
| 3 | +Revision ID: cb01e050df85 |
| 4 | +Revises: 05267fabef38 |
| 5 | +Create Date: 2024-04-10 13:12:57.109831 |
| 6 | +
|
| 7 | +""" |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +import sqlalchemy as sa |
| 12 | +import sqlmodel |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision: str = 'cb01e050df85' |
| 16 | +down_revision: Union[str, None] = '05267fabef38' |
| 17 | +branch_labels: Union[str, Sequence[str], None] = None |
| 18 | +depends_on: Union[str, Sequence[str], None] = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + # ### commands auto generated by Alembic - please adjust! ### |
| 23 | + op.create_table('localauthsession', |
| 24 | + sa.Column('id', sa.Integer(), nullable=False), |
| 25 | + sa.Column('user_id', sa.Integer(), nullable=False), |
| 26 | + sa.Column('session_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 27 | + sa.Column('expiration', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False), |
| 28 | + sa.PrimaryKeyConstraint('id') |
| 29 | + ) |
| 30 | + op.create_index(op.f('ix_localauthsession_session_id'), 'localauthsession', ['session_id'], unique=True) |
| 31 | + op.create_index(op.f('ix_localauthsession_user_id'), 'localauthsession', ['user_id'], unique=False) |
| 32 | + op.create_table('localuser', |
| 33 | + sa.Column('id', sa.Integer(), nullable=False), |
| 34 | + sa.Column('username', sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 35 | + sa.Column('password_hash', sa.LargeBinary(), nullable=False), |
| 36 | + sa.Column('enabled', sa.Boolean(), nullable=False), |
| 37 | + sa.PrimaryKeyConstraint('id') |
| 38 | + ) |
| 39 | + op.create_index(op.f('ix_localuser_username'), 'localuser', ['username'], unique=True) |
| 40 | + op.execute("INSERT INTO localuser SELECT * FROM user;") |
| 41 | + op.execute("INSERT INTO localauthsession SELECT * FROM authsession;") |
| 42 | + op.drop_index('ix_authsession_session_id', table_name='authsession') |
| 43 | + op.drop_index('ix_authsession_user_id', table_name='authsession') |
| 44 | + op.drop_table('authsession') |
| 45 | + naming_convention = { |
| 46 | + "fk": |
| 47 | + "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", |
| 48 | + } |
| 49 | + with op.batch_alter_table('userinfo', naming_convention=naming_convention) as batch_op: |
| 50 | + batch_op.drop_constraint('fk_userinfo_user_id_user', type_='foreignkey') |
| 51 | + batch_op.create_foreign_key('fk_userinfo_user_id_localuser', 'localuser', ['user_id'], ['id']) |
| 52 | + op.drop_index('ix_user_username', table_name='user') |
| 53 | + op.drop_table('user') |
| 54 | + # ### end Alembic commands ### |
| 55 | + |
| 56 | + |
| 57 | +def downgrade() -> None: |
| 58 | + # ### commands auto generated by Alembic - please adjust! ### |
| 59 | + op.create_table('user', |
| 60 | + sa.Column('id', sa.INTEGER(), nullable=False), |
| 61 | + sa.Column('username', sa.VARCHAR(), nullable=False), |
| 62 | + sa.Column('password_hash', sa.BLOB(), nullable=False), |
| 63 | + sa.Column('enabled', sa.BOOLEAN(), nullable=False), |
| 64 | + sa.PrimaryKeyConstraint('id') |
| 65 | + ) |
| 66 | + op.create_index('ix_user_username', 'user', ['username'], unique=1) |
| 67 | + op.create_table('authsession', |
| 68 | + sa.Column('id', sa.INTEGER(), nullable=False), |
| 69 | + sa.Column('user_id', sa.INTEGER(), nullable=False), |
| 70 | + sa.Column('session_id', sa.VARCHAR(), nullable=False), |
| 71 | + sa.Column('expiration', sa.DATETIME(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False), |
| 72 | + sa.PrimaryKeyConstraint('id') |
| 73 | + ) |
| 74 | + op.create_index('ix_authsession_user_id', 'authsession', ['user_id'], unique=False) |
| 75 | + op.create_index('ix_authsession_session_id', 'authsession', ['session_id'], unique=1) |
| 76 | + op.execute("INSERT INTO user SELECT * FROM localuser;") |
| 77 | + op.execute("INSERT INTO authsession SELECT * FROM localauthsession;") |
| 78 | + naming_convention = { |
| 79 | + "fk": |
| 80 | + "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", |
| 81 | + } |
| 82 | + with op.batch_alter_table('userinfo', naming_convention=naming_convention) as batch_op: |
| 83 | + batch_op.drop_constraint('fk_userinfo_user_id_localuser', type_='foreignkey') |
| 84 | + batch_op.create_foreign_key('fk_userinfo_user_id_user', 'localuser', ['user_id'], ['id']) |
| 85 | + op.drop_index(op.f('ix_localuser_username'), table_name='localuser') |
| 86 | + op.drop_table('localuser') |
| 87 | + op.drop_index(op.f('ix_localauthsession_user_id'), table_name='localauthsession') |
| 88 | + op.drop_index(op.f('ix_localauthsession_session_id'), table_name='localauthsession') |
| 89 | + op.drop_table('localauthsession') |
| 90 | + # ### end Alembic commands ### |
0 commit comments