49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
"""create books table
|
|
|
|
Revision ID: 1234567890ab
|
|
Revises:
|
|
Create Date: 2023-05-20 10:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
import uuid
|
|
|
|
# revision identifiers
|
|
revision = '1234567890ab'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# Create books table
|
|
op.create_table(
|
|
'books',
|
|
sa.Column('id', sa.String, primary_key=True, default=lambda: str(uuid.uuid4())),
|
|
sa.Column('title', sa.String, nullable=False, index=True),
|
|
sa.Column('description', sa.Text, nullable=True),
|
|
sa.Column('author', sa.String, nullable=False, index=True),
|
|
sa.Column('pages', sa.Integer, nullable=False),
|
|
sa.Column('user_id', sa.String, sa.ForeignKey('users.id')),
|
|
sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
|
|
sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now()),
|
|
)
|
|
|
|
# Create index on title
|
|
op.create_index('ix_books_title', 'books', ['title'])
|
|
|
|
# Create index on author
|
|
op.create_index('ix_books_author', 'books', ['author'])
|
|
|
|
|
|
def downgrade():
|
|
# Drop indexes
|
|
op.drop_index('ix_books_title', 'books')
|
|
op.drop_index('ix_books_author', 'books')
|
|
|
|
# Drop table
|
|
op.drop_table('books')
|
|
```
|
|
|
|
This Alembic migration script creates a `books` table with the specified columns, including a primary key `id` with a UUID default value, indexes on `title` and `author`, a foreign key `user_id` referencing the `users` table, and timestamp columns `created_at` and `updated_at`. The `upgrade()` function creates the table and indexes, while the `downgrade()` function drops the indexes and the table. |