From bdccf5036226b08494ce0e5f819c68176aa7e847 Mon Sep 17 00:00:00 2001 From: Kai Chappell Date: Fri, 20 Jun 2025 15:10:37 +0100 Subject: [PATCH] extend complexity fields to text --- backend/alembic.ini | 3 +- .../alembic/versions/001_initial_schema.py | 10 ++-- ...98b82d_extend_complexity_fields_to_text.py | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 backend/alembic/versions/11f3a598b82d_extend_complexity_fields_to_text.py diff --git a/backend/alembic.ini b/backend/alembic.ini index 54a71d3..96fd33f 100644 --- a/backend/alembic.ini +++ b/backend/alembic.ini @@ -2,7 +2,8 @@ script_location = alembic prepend_sys_path = . version_path_separator = os -sqlalchemy.url = postgresql+asyncpg://codetutor:codetutor@localhost:5432/codetutor +# URL is set programmatically in alembic/env.py from environment variables +sqlalchemy.url = [post_write_hooks] diff --git a/backend/alembic/versions/001_initial_schema.py b/backend/alembic/versions/001_initial_schema.py index e93b651..5814b92 100644 --- a/backend/alembic/versions/001_initial_schema.py +++ b/backend/alembic/versions/001_initial_schema.py @@ -19,9 +19,8 @@ depends_on: str | Sequence[str] | None = None def upgrade() -> None: - # Create difficulty enum - difficulty_enum = postgresql.ENUM("easy", "medium", "hard", name="difficulty_enum") - difficulty_enum.create(op.get_bind()) + # Create difficulty enum using raw SQL to avoid SQLAlchemy auto-creation issues + op.execute("CREATE TYPE difficulty_enum AS ENUM ('easy', 'medium', 'hard')") # Create questions table op.create_table( @@ -31,7 +30,7 @@ def upgrade() -> None: sa.Column("slug", sa.String(255), unique=True, nullable=False), sa.Column( "difficulty", - sa.Enum("easy", "medium", "hard", name="difficulty_enum"), + postgresql.ENUM("easy", "medium", "hard", name="difficulty_enum", create_type=False), nullable=False, ), sa.Column("description", sa.Text, nullable=False), @@ -158,5 +157,4 @@ def downgrade() -> None: op.drop_table("explanations") op.drop_table("questions") - difficulty_enum = postgresql.ENUM("easy", "medium", "hard", name="difficulty_enum") - difficulty_enum.drop(op.get_bind()) + op.execute("DROP TYPE IF EXISTS difficulty_enum") diff --git a/backend/alembic/versions/11f3a598b82d_extend_complexity_fields_to_text.py b/backend/alembic/versions/11f3a598b82d_extend_complexity_fields_to_text.py new file mode 100644 index 0000000..a029c55 --- /dev/null +++ b/backend/alembic/versions/11f3a598b82d_extend_complexity_fields_to_text.py @@ -0,0 +1,52 @@ +"""extend complexity fields to text + +Revision ID: 11f3a598b82d +Revises: 001 +Create Date: 2025-04-12 20:44:46.031680 + +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "11f3a598b82d" +down_revision: str | None = "001" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.alter_column( + "explanations", + "time_complexity", + existing_type=sa.VARCHAR(length=50), + type_=sa.Text(), + existing_nullable=False, + ) + op.alter_column( + "explanations", + "space_complexity", + existing_type=sa.VARCHAR(length=50), + type_=sa.Text(), + existing_nullable=False, + ) + + +def downgrade() -> None: + op.alter_column( + "explanations", + "space_complexity", + existing_type=sa.Text(), + type_=sa.VARCHAR(length=50), + existing_nullable=False, + ) + op.alter_column( + "explanations", + "time_complexity", + existing_type=sa.Text(), + type_=sa.VARCHAR(length=50), + existing_nullable=False, + )