From 80c923767b50abe9fa6cb2263f319045748fe2ee Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 17:28:40 +0100 Subject: [PATCH] Add migration for Book --- .../20250325_172757_033ff255_create_book.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 alembic/versions/20250325_172757_033ff255_create_book.py diff --git a/alembic/versions/20250325_172757_033ff255_create_book.py b/alembic/versions/20250325_172757_033ff255_create_book.py new file mode 100644 index 0000000..05862fd --- /dev/null +++ b/alembic/versions/20250325_172757_033ff255_create_book.py @@ -0,0 +1,53 @@ +"""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.dialects.postgresql import UUID + + +# revision identifiers, used by Alembic +revision = '1234567890ab' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'books', + sa.Column('id', UUID(as_uuid=True), primary_key=True, server_default=sa.text("uuid_generate_v4()")), + sa.Column('title', sa.String, nullable=False, index=True), + sa.Column('author', sa.String, nullable=False, index=True), + sa.Column('description', sa.String, nullable=True), + sa.Column('author_id', UUID(as_uuid=True), nullable=False), + sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False, server_default=sa.func.now()), + sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False, server_default=sa.func.now(), onupdate=sa.func.now()), + sa.ForeignKeyConstraint(['author_id'], ['authors.id'], ondelete='CASCADE'), + ) + op.create_index('idx_books_title_author', 'books', ['title', 'author'], unique=True) + + +def downgrade(): + op.drop_index('idx_books_title_author', 'books') + op.drop_table('books') + +``` + +This Alembic migration script creates a new table called `books` with the following columns: + +- `id` (UUID, primary key, generated using `uuid_generate_v4()`) +- `title` (String, not nullable, indexed) +- `author` (String, not nullable, indexed) +- `description` (String, nullable) +- `author_id` (UUID, not nullable, foreign key referencing `authors.id` table with `ON DELETE CASCADE`) +- `created_at` (TIMESTAMP with timezone, not nullable, default to current time) +- `updated_at` (TIMESTAMP with timezone, not nullable, default to current time, updated on each row update) + +It also creates a unique index on the `title` and `author` columns combined. + +The `downgrade()` function removes the index and drops the `books` table. \ No newline at end of file