From 633cd99a8ca1a805c11a0220a4bb8d7c2e378ff8 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 16:19:44 +0100 Subject: [PATCH] Add migration for Book --- .../20250325_161934_7c0930cb_create_book.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 alembic/versions/20250325_161934_7c0930cb_create_book.py diff --git a/alembic/versions/20250325_161934_7c0930cb_create_book.py b/alembic/versions/20250325_161934_7c0930cb_create_book.py new file mode 100644 index 0000000..ee6d490 --- /dev/null +++ b/alembic/versions/20250325_161934_7c0930cb_create_book.py @@ -0,0 +1,40 @@ +"""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. \ No newline at end of file