Adds category groups
This commit is contained in:
parent
b8142f4f99
commit
78e545589d
@ -0,0 +1,69 @@
|
|||||||
|
"""Category groups and relationships
|
||||||
|
|
||||||
|
Revision ID: 83f4c9837f6e
|
||||||
|
Revises: 2d0891f1be11
|
||||||
|
Create Date: 2022-12-04 15:10:51.924875+00:00
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = "83f4c9837f6e"
|
||||||
|
down_revision = "2d0891f1be11"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table(
|
||||||
|
"categories_groups",
|
||||||
|
sa.Column("name", sa.String(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint("name", name=op.f("pk_categories_groups")),
|
||||||
|
schema="transactions",
|
||||||
|
)
|
||||||
|
op.add_column(
|
||||||
|
"categories_available",
|
||||||
|
sa.Column("group", sa.String(), nullable=True),
|
||||||
|
schema="transactions",
|
||||||
|
)
|
||||||
|
op.create_foreign_key(
|
||||||
|
op.f("fk_categories_available_group_categories_groups"),
|
||||||
|
"categories_available",
|
||||||
|
"categories_groups",
|
||||||
|
["group"],
|
||||||
|
["name"],
|
||||||
|
source_schema="transactions",
|
||||||
|
referent_schema="transactions",
|
||||||
|
)
|
||||||
|
op.create_foreign_key(
|
||||||
|
op.f("fk_categorized_name_categories_available"),
|
||||||
|
"categorized",
|
||||||
|
"categories_available",
|
||||||
|
["name"],
|
||||||
|
["name"],
|
||||||
|
source_schema="transactions",
|
||||||
|
referent_schema="transactions",
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint(
|
||||||
|
op.f("fk_categorized_name_categories_available"),
|
||||||
|
"categorized",
|
||||||
|
schema="transactions",
|
||||||
|
type_="foreignkey",
|
||||||
|
)
|
||||||
|
op.drop_constraint(
|
||||||
|
op.f("fk_categories_available_group_categories_groups"),
|
||||||
|
"categories_available",
|
||||||
|
schema="transactions",
|
||||||
|
type_="foreignkey",
|
||||||
|
)
|
||||||
|
op.drop_column("categories_available", "group", schema="transactions")
|
||||||
|
op.drop_table("categories_groups", schema="transactions")
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -92,14 +92,32 @@ idfk = Annotated[
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryGroup(Base):
|
||||||
|
__tablename__ = "categories_groups"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Category(Base):
|
||||||
|
__tablename__ = "categories_available"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(primary_key=True)
|
||||||
|
group: Mapped[Optional[str]] = mapped_column(ForeignKey(CategoryGroup.name))
|
||||||
|
|
||||||
|
rules: Mapped[Optional[set[CategoryRule]]] = relationship(
|
||||||
|
back_populates="category", cascade="all, delete-orphan", passive_deletes=True
|
||||||
|
)
|
||||||
|
categorygroup: Mapped[Optional[CategoryGroup]] = relationship()
|
||||||
|
|
||||||
|
|
||||||
class TransactionCategory(Base):
|
class TransactionCategory(Base):
|
||||||
__tablename__ = "categorized"
|
__tablename__ = "categorized"
|
||||||
|
|
||||||
id: Mapped[idfk] = mapped_column(primary_key=True)
|
id: Mapped[idfk] = mapped_column(primary_key=True)
|
||||||
name: Mapped[str]
|
name: Mapped[str] = mapped_column(ForeignKey(Category.name))
|
||||||
|
|
||||||
original: Mapped[Transaction] = relationship(back_populates="category")
|
original: Mapped[Transaction] = relationship(back_populates="category")
|
||||||
category: Mapped[AvailableCategory] = relationship(back_populates="category")
|
category: Mapped[Category] = relationship()
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Category({self.name})"
|
return f"Category({self.name})"
|
||||||
@ -137,23 +155,12 @@ class Tag(Base):
|
|||||||
original: Mapped[Transaction] = relationship(back_populates="tags")
|
original: Mapped[Transaction] = relationship(back_populates="tags")
|
||||||
|
|
||||||
|
|
||||||
class AvailableCategory(Base):
|
|
||||||
__tablename__ = "categories_available"
|
|
||||||
|
|
||||||
name: Mapped[str] = mapped_column(primary_key=True)
|
|
||||||
|
|
||||||
rules: Mapped[Optional[set[CategoryRule]]] = relationship(
|
|
||||||
back_populates="original", cascade="all, delete-orphan", passive_deletes=True
|
|
||||||
)
|
|
||||||
category: Mapped[TransactionCategory] = relationship(back_populates="category")
|
|
||||||
|
|
||||||
|
|
||||||
class CategoryRule(Base):
|
class CategoryRule(Base):
|
||||||
__tablename__ = "categories_rules"
|
__tablename__ = "categories_rules"
|
||||||
|
|
||||||
name: Mapped[str] = mapped_column(
|
name: Mapped[str] = mapped_column(
|
||||||
ForeignKey(AvailableCategory.name, ondelete="CASCADE"), primary_key=True
|
ForeignKey(Category.name, ondelete="CASCADE"), primary_key=True
|
||||||
)
|
)
|
||||||
rule: Mapped[str] = mapped_column(primary_key=True)
|
rule: Mapped[str] = mapped_column(primary_key=True)
|
||||||
|
|
||||||
category: Mapped[AvailableCategory] = relationship(back_populates="rules")
|
category: Mapped[Category] = relationship(back_populates="rules")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user