feat: Affectation des enseignants aux classes et matières
Permet aux administrateurs d'associer un enseignant à une classe pour une matière donnée au sein d'une année scolaire. Cette brique est nécessaire pour construire les emplois du temps et les carnets de notes par la suite. Le modèle impose l'unicité du triplet enseignant × classe × matière par année scolaire, avec réactivation automatique d'une affectation retirée plutôt que duplication. L'isolation multi-tenant est garantie au niveau du repository (findById/get filtrent par tenant_id).
This commit is contained in:
55
backend/migrations/Version20260212100000.php
Normal file
55
backend/migrations/Version20260212100000.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Creates the teacher_assignments table for Story 2.8.
|
||||
*
|
||||
* Models the ternary relationship Teacher × Class × Subject, scoped by
|
||||
* academic year and tenant. Supports multi-subject per teacher and
|
||||
* co-teaching (multiple teachers per subject/class).
|
||||
*/
|
||||
final class Version20260212100000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Create teacher_assignments table (Story 2.8)';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS teacher_assignments (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL,
|
||||
teacher_id UUID NOT NULL,
|
||||
school_class_id UUID NOT NULL,
|
||||
subject_id UUID NOT NULL,
|
||||
academic_year_id UUID NOT NULL,
|
||||
start_date TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
end_date TIMESTAMPTZ,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'active',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE(teacher_id, school_class_id, subject_id, academic_year_id)
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_tenant ON teacher_assignments(tenant_id)');
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_teacher ON teacher_assignments(teacher_id)');
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_class ON teacher_assignments(school_class_id)');
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_year ON teacher_assignments(academic_year_id)');
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_teacher_tenant_status ON teacher_assignments(teacher_id, tenant_id, status)');
|
||||
$this->addSql('CREATE INDEX idx_teacher_assignments_class_tenant_status ON teacher_assignments(school_class_id, tenant_id, status)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP TABLE IF EXISTS teacher_assignments');
|
||||
}
|
||||
}
|
||||
52
backend/migrations/Version20260212200000.php
Normal file
52
backend/migrations/Version20260212200000.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Adds tenant_id to the UNIQUE constraint on teacher_assignments.
|
||||
*
|
||||
* The original constraint (teacher_id, school_class_id, subject_id, academic_year_id)
|
||||
* allowed the same tuple to exist across different tenants, which is correct,
|
||||
* but did not scope uniqueness per tenant. Including tenant_id ensures that
|
||||
* uniqueness is enforced within each tenant boundary.
|
||||
*/
|
||||
final class Version20260212200000 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Add tenant_id to teacher_assignments UNIQUE constraint for multi-tenant safety';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE teacher_assignments
|
||||
DROP CONSTRAINT IF EXISTS teacher_assignments_teacher_id_school_class_id_subject_id_acad_key
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE teacher_assignments
|
||||
ADD CONSTRAINT teacher_assignments_tenant_teacher_class_subject_year_key
|
||||
UNIQUE (tenant_id, teacher_id, school_class_id, subject_id, academic_year_id)
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE teacher_assignments
|
||||
DROP CONSTRAINT IF EXISTS teacher_assignments_tenant_teacher_class_subject_year_key
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE teacher_assignments
|
||||
ADD CONSTRAINT teacher_assignments_teacher_id_school_class_id_subject_id_acad_key
|
||||
UNIQUE (teacher_id, school_class_id, subject_id, academic_year_id)
|
||||
SQL);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user