-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: created base migration and schema for students records and cla…
…sses relationship - created `students_records_classes` table - created `StudentsRecords.StudentRecordClassRelationship` schema - added classes fields to `StudentRecord` schema
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/lanttern/students_records/student_record_class_relationship.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Lanttern.StudentsRecords.StudentRecordClassRelationship do | ||
@moduledoc """ | ||
The `StudentRecordClassRelationship` schema (join table) | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@primary_key false | ||
schema "students_records_classes" do | ||
field :student_record_id, :id, primary_key: true | ||
field :class_id, :id, primary_key: true | ||
field :school_id, :id | ||
end | ||
|
||
@doc false | ||
def changeset(student_record_class_relationship, attrs) do | ||
student_record_class_relationship | ||
|> cast(attrs, [:class_id, :student_record_id, :school_id]) | ||
|> validate_required([:class_id, :student_record_id, :school_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
priv/repo/migrations/20241212195844_create_students_records_classes.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Lanttern.Repo.Migrations.CreateStudentsRecordsClasses do | ||
use Ecto.Migration | ||
|
||
def change do | ||
# creating unique constraints to allow composite foreign keys. | ||
# this guarantees, in the database level, that record and class | ||
# belong to the same school | ||
|
||
# removing existing "classes_school_id_index" to prevent unnecessary index | ||
drop index(:classes, [:school_id]) | ||
create unique_index(:classes, [:school_id, :id]) | ||
|
||
create table(:students_records_classes, primary_key: false) do | ||
# in the future we will handle how to cascade class and school deletion to ss records | ||
add :class_id, references(:classes, with: [school_id: :school_id], on_delete: :nothing), | ||
null: false | ||
|
||
add :student_record_id, | ||
references(:students_records, with: [school_id: :school_id], on_delete: :delete_all), | ||
null: false | ||
|
||
add :school_id, references(:schools, on_delete: :nothing), null: false | ||
end | ||
|
||
create index(:students_records_classes, [:class_id]) | ||
create unique_index(:students_records_classes, [:student_record_id, :class_id]) | ||
end | ||
end |