"""Initial migration with users table Revision ID: 001_initial Revises: Create Date: 2024-01-01 00:00:00.000000 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '001_initial' down_revision = None branch_labels = None depends_on = None def upgrade() -> None: # Создание таблицы users op.create_table( 'users', sa.Column('id', sa.Integer(), nullable=False), sa.Column('username', sa.String(), nullable=False), sa.Column('hashed_password', sa.String(), nullable=False), sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'), sa.Column('is_superuser', sa.Boolean(), nullable=False, server_default='false'), sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')), sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')), sa.PrimaryKeyConstraint('id') ) op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) # Создание пользователя admin/admin по умолчанию # Пароль будет хеширован при первом входе через UserService # Используем простой временный хеш, который будет обновлен при первом входе import bcrypt temp_hash = bcrypt.hashpw(b'admin', bcrypt.gensalt()).decode('utf-8') # Используем параметризованный запрос op.execute( sa.text(""" INSERT INTO users (username, hashed_password, is_active, is_superuser) VALUES ('admin', :hash, true, true) ON CONFLICT (username) DO NOTHING; """).bindparams(hash=temp_hash) ) def downgrade() -> None: op.drop_index(op.f('ix_users_username'), table_name='users') op.drop_index(op.f('ix_users_id'), table_name='users') op.drop_table('users')