Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reports/
.coverage
13 changes: 2 additions & 11 deletions backend/alembic/versions/0007_timetable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Create annual timetables, kholle bounds, audiences and revocable ICS subscriptions."""
"""Create annual timetables, availability patterns, audiences and revocable ICS subscriptions."""

from uuid import uuid4

Expand All @@ -13,7 +13,7 @@


def upgrade() -> None:
"""Create timetable storage, availability bounds, archive policy and personal calendar links."""
"""Create timetables, availability patterns, archive policy and personal calendar links."""
op.create_table(
"timetable_calendars",
sa.Column("id", sa.BigInteger(), sa.Identity(), primary_key=True),
Expand Down Expand Up @@ -61,15 +61,6 @@ def upgrade() -> None:
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.Column("school_year", sa.Integer(), nullable=False),
sa.Column("ranges", postgresql.JSONB(), nullable=True),
sa.Column("kholle_min_hours", sa.Integer(), nullable=False, server_default="0"),
sa.Column("kholle_max_hours", sa.Integer(), nullable=False, server_default="0"),
sa.CheckConstraint(
"kholle_min_hours >= 0", name=op.f("ck_teacher_availability_kholle_min_hours")
),
sa.CheckConstraint(
"kholle_max_hours >= kholle_min_hours",
name=op.f("ck_teacher_availability_kholle_max_hours"),
),
sa.Column("revision", sa.String(32), nullable=False),
sa.PrimaryKeyConstraint("user_id", "school_year"),
sa.ForeignKeyConstraint(["user_id"], ["sophikron.users.id"], ondelete="CASCADE"),
Expand Down
29 changes: 1 addition & 28 deletions backend/alembic/versions/0008_kholloscope.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Consolidated kholloscope preparation, durable drafts and administrator capabilities.

This unreleased baseline replaces the former 0008 through 0014 revisions.
Apply it to a fresh database or one still at 0007; never stamp an old schema to match it.
Apply it to a fresh database or the cleaned 0007 baseline; never stamp an old schema to match it.
"""

import sqlalchemy as sa
Expand Down Expand Up @@ -52,15 +52,6 @@ def upgrade() -> None:
sa.ForeignKeyConstraint(["subject_id"], ["sophikron.subjects.id"], ondelete="RESTRICT"),
schema="sophikron",
)
for name in ("kholle_max_hours", "kholle_min_hours"):
op.drop_constraint(
op.f(f"ck_teacher_availability_{name}"),
"teacher_availability",
type_="check",
schema="sophikron",
)
for name in ("kholle_min_hours", "kholle_max_hours"):
op.drop_column("teacher_availability", name, schema="sophikron")
op.create_table(
"kholloscope_workflows",
sa.Column("school_year", sa.Integer(), primary_key=True),
Expand Down Expand Up @@ -149,24 +140,6 @@ def downgrade() -> None:
)
op.drop_column("timetable_rooms", "kholle_capacity", schema="sophikron")
op.drop_table("kholloscope_workflows", schema="sophikron")
for name in ("kholle_min_hours", "kholle_max_hours"):
op.add_column(
"teacher_availability",
sa.Column(name, sa.Integer(), nullable=False, server_default="0"),
schema="sophikron",
)
op.create_check_constraint(
op.f("ck_teacher_availability_kholle_min_hours"),
"teacher_availability",
"kholle_min_hours >= 0",
schema="sophikron",
)
op.create_check_constraint(
op.f("ck_teacher_availability_kholle_max_hours"),
"teacher_availability",
"kholle_max_hours >= kholle_min_hours",
schema="sophikron",
)
op.drop_table("kholle_wishes", schema="sophikron")
op.drop_column("timetable_school_years", "created_at", schema="sophikron")
op.drop_column("timetable_calendars", "kholle_settings", schema="sophikron")
167 changes: 167 additions & 0 deletions backend/alembic/versions/0009_curricula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""Release baseline for curricula, competency DAGs, optional promotions and bounded undo."""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "0009_curricula"
down_revision = "0008_kholloscope"
branch_labels = None
depends_on = None


def upgrade() -> None:
"""Add programme storage without changing calendars or existing promotions."""
op.create_table(
"curricula",
sa.Column("id", sa.BigInteger(), sa.Identity(always=True), primary_key=True),
sa.Column("title", sa.String(160), nullable=False),
sa.Column("description", sa.Text()),
sa.Column("source_url", sa.String(2000)),
sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
sa.Column("version", sa.BigInteger(), nullable=False, server_default="0"),
sa.CheckConstraint("length(btrim(title)) > 0", name="title"),
schema="sophikron",
)
op.create_table(
"curriculum_subjects",
sa.Column("curriculum_id", sa.BigInteger(), primary_key=True),
sa.Column("subject_id", sa.BigInteger(), primary_key=True),
sa.ForeignKeyConstraint(["curriculum_id"], ["sophikron.curricula.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["subject_id"], ["sophikron.subjects.id"], ondelete="RESTRICT"),
schema="sophikron",
)
op.create_table(
"competencies",
sa.Column("id", sa.BigInteger(), sa.Identity(always=True), primary_key=True),
sa.Column("curriculum_id", sa.BigInteger(), nullable=False),
sa.Column("subject_id", sa.BigInteger(), nullable=False),
sa.Column("name", sa.String(300), nullable=False),
sa.Column("semester", sa.Integer()),
sa.Column("knowledge", sa.Text()),
sa.Column("comment", sa.Text()),
sa.Column("color", sa.String(7)),
sa.Column("position", sa.Integer(), nullable=False, server_default="0"),
sa.ForeignKeyConstraint(
["curriculum_id", "subject_id"],
[
"sophikron.curriculum_subjects.curriculum_id",
"sophikron.curriculum_subjects.subject_id",
],
ondelete="CASCADE",
),
sa.UniqueConstraint("curriculum_id", "subject_id", "id", name="uq_competency_scope"),
sa.CheckConstraint("length(btrim(name)) > 0", name="name"),
sa.CheckConstraint("semester BETWEEN 1 AND 4", name="semester"),
sa.CheckConstraint("color ~ '^#[0-9A-Fa-f]{6}$'", name="color"),
sa.CheckConstraint("position >= 0", name="position"),
schema="sophikron",
)
op.create_index(
"ix_competencies_curriculum_id", "competencies", ["curriculum_id"], schema="sophikron"
)
op.create_table(
"competency_links",
sa.Column("curriculum_id", sa.BigInteger(), nullable=False),
sa.Column("subject_id", sa.BigInteger(), nullable=False),
sa.Column("parent_id", sa.BigInteger(), primary_key=True),
sa.Column("child_id", sa.BigInteger(), primary_key=True),
*(
sa.ForeignKeyConstraint(
["curriculum_id", "subject_id", endpoint],
[
"sophikron.competencies.curriculum_id",
"sophikron.competencies.subject_id",
"sophikron.competencies.id",
],
ondelete="CASCADE",
name=f"fk_competency_links_{endpoint}",
)
for endpoint in ("parent_id", "child_id")
),
sa.CheckConstraint("parent_id <> child_id", name="different_nodes"),
schema="sophikron",
)
op.create_index(
"ix_competency_links_child_id", "competency_links", ["child_id"], schema="sophikron"
)
op.add_column("promotions", sa.Column("curriculum_id", sa.BigInteger()), schema="sophikron")
op.create_foreign_key(
"fk_promotions_curriculum_id_curricula",
"promotions",
"curricula",
["curriculum_id"],
["id"],
source_schema="sophikron",
referent_schema="sophikron",
ondelete="RESTRICT",
)
op.create_index(
"ix_promotions_curriculum_id", "promotions", ["curriculum_id"], schema="sophikron"
)
op.create_table(
"curriculum_undo",
sa.Column("id", sa.BigInteger(), sa.Identity(always=True), primary_key=True),
sa.Column(
"actor_id",
sa.BigInteger(),
sa.ForeignKey("sophikron.users.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"curriculum_id",
sa.BigInteger(),
sa.ForeignKey("sophikron.curricula.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"subject_id",
sa.BigInteger(),
sa.ForeignKey("sophikron.subjects.id", ondelete="RESTRICT"),
nullable=False,
),
sa.Column("node_id", sa.BigInteger(), nullable=False),
sa.Column("after_version", sa.BigInteger(), nullable=False),
sa.Column("payload", postgresql.JSONB(), nullable=False),
schema="sophikron",
)

for code, roles in (
("curricula.read", ("admin", "teacher", "kholleur")),
("curricula.edit", ("teacher",)),
("curricula.manage", ("admin",)),
):
op.execute(
sa.text(
"INSERT INTO sophikron.permissions (code, description) VALUES (:c, :c)"
).bindparams(c=code)
)
for role in roles:
op.execute(
sa.text(
"INSERT INTO sophikron.role_permissions (role_id, permission_id) "
"SELECT r.id, p.id FROM sophikron.roles r CROSS JOIN sophikron.permissions p "
"WHERE r.code = :r AND p.code = :c"
).bindparams(r=role, c=code)
)


def downgrade() -> None:
"""Remove only programme storage and the optional promotion association."""
op.execute("DELETE FROM sophikron.permissions WHERE code LIKE 'curricula.%'")
op.drop_index("ix_promotions_curriculum_id", table_name="promotions", schema="sophikron")
op.drop_constraint(
"fk_promotions_curriculum_id_curricula",
"promotions",
schema="sophikron",
type_="foreignkey",
)
op.drop_column("promotions", "curriculum_id", schema="sophikron")
for table in (
"curriculum_undo",
"competency_links",
"competencies",
"curriculum_subjects",
"curricula",
):
op.drop_table(table, schema="sophikron")
2 changes: 2 additions & 0 deletions backend/app/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from app.api.v1 import (
admin,
auth,
curricula,
dashboard,
groups,
kholloscope,
Expand All @@ -20,6 +21,7 @@
router = APIRouter()
v1 = APIRouter(prefix="/api/v1")
v1.include_router(auth.router)
v1.include_router(curricula.router)
v1.include_router(admin.router)
v1.include_router(dashboard.router)
v1.include_router(groups.router)
Expand Down
Loading
Loading