40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""create books table
|
|
|
|
Revision ID: 1234567890ab
|
|
Revises:
|
|
Create Date: 2023-05-25 10:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.sql import func
|
|
|
|
|
|
# revision identifiers, used by Alembic
|
|
revision = '1234567890ab'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# Create books table
|
|
op.create_table(
|
|
'books',
|
|
sa.Column('id', sa.Integer, primary_key=True, index=True),
|
|
sa.Column('title', sa.String, index=True),
|
|
sa.Column('author', sa.String, index=True),
|
|
sa.Column('description', sa.String),
|
|
sa.Column('published_year', sa.Integer),
|
|
sa.Column('isbn', sa.String, unique=True),
|
|
sa.Column('created_at', sa.DateTime, server_default=func.now()),
|
|
sa.Column('updated_at', sa.DateTime, server_default=func.now(), onupdate=func.now())
|
|
)
|
|
|
|
|
|
def downgrade():
|
|
# Drop books table
|
|
op.drop_table('books')
|
|
```
|
|
|
|
This Alembic migration script creates a new table called 'books' with the specified columns and constraints. The `upgrade()` function creates the table, while the `downgrade()` function drops the table. Note the use of `server_default=func.now()` for the `created_at` and `updated_at` columns to automatically set the timestamp on record creation and update, respectively. |