language-learning-app/api/alembic/versions/20260407_0007_add_dictionary_tables.py

89 lines
3.5 KiB
Python

"""add dictionary tables
Revision ID: 0007
Revises: 0006
Create Date: 2026-04-07
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "0007"
down_revision: Union[str, None] = "0006"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"dictionary_lemma",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("headword", sa.Text(), nullable=False),
sa.Column("language", sa.String(2), nullable=False),
sa.Column("pos_raw", sa.Text(), nullable=False),
sa.Column("pos_normalised", sa.Text(), nullable=True),
sa.Column("gender", sa.Text(), nullable=True),
sa.Column("tags", postgresql.ARRAY(sa.Text()), nullable=False, server_default="{}"),
)
op.create_index("ix_dictionary_lemma_headword_language", "dictionary_lemma", ["headword", "language"])
op.create_index("ix_dictionary_lemma_language", "dictionary_lemma", ["language"])
op.create_table(
"dictionary_sense",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column(
"lemma_id",
postgresql.UUID(as_uuid=True),
sa.ForeignKey("dictionary_lemma.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("sense_index", sa.Integer(), nullable=False),
sa.Column("gloss", sa.Text(), nullable=False, server_default=""),
sa.Column("topics", postgresql.ARRAY(sa.Text()), nullable=False, server_default="{}"),
sa.Column("tags", postgresql.ARRAY(sa.Text()), nullable=False, server_default="{}"),
)
op.create_index("ix_dictionary_sense_lemma_id", "dictionary_sense", ["lemma_id"])
op.create_table(
"dictionary_wordform",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column(
"lemma_id",
postgresql.UUID(as_uuid=True),
sa.ForeignKey("dictionary_lemma.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("form", sa.Text(), nullable=False),
sa.Column("tags", postgresql.ARRAY(sa.Text()), nullable=False, server_default="{}"),
)
op.create_index("ix_dictionary_wordform_lemma_id", "dictionary_wordform", ["lemma_id"])
op.create_index("ix_dictionary_wordform_form", "dictionary_wordform", ["form"])
op.create_table(
"dictionary_lemma_raw",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column(
"lemma_id",
postgresql.UUID(as_uuid=True),
sa.ForeignKey("dictionary_lemma.id", ondelete="CASCADE"),
nullable=False,
unique=True,
),
sa.Column("language", sa.String(2), nullable=False),
sa.Column("raw", postgresql.JSONB(), nullable=False),
)
def downgrade() -> None:
op.drop_table("dictionary_lemma_raw")
op.drop_index("ix_dictionary_wordform_form", table_name="dictionary_wordform")
op.drop_index("ix_dictionary_wordform_lemma_id", table_name="dictionary_wordform")
op.drop_table("dictionary_wordform")
op.drop_index("ix_dictionary_sense_lemma_id", table_name="dictionary_sense")
op.drop_table("dictionary_sense")
op.drop_index("ix_dictionary_lemma_language", table_name="dictionary_lemma")
op.drop_index("ix_dictionary_lemma_headword_language", table_name="dictionary_lemma")
op.drop_table("dictionary_lemma")