-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a couple of cascade deletes for foreign key relations
- Loading branch information
Showing
4 changed files
with
120 additions
and
23 deletions.
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
105 changes: 105 additions & 0 deletions
105
backend/migrations/20230525091923_alter-tables-add-cascade-deletes.ts
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,105 @@ | ||
import { Knex } from "knex" | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.raw(` | ||
ALTER TABLE "exercise_completion_required_actions" | ||
DROP CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey", | ||
ADD CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey" | ||
FOREIGN KEY ("exercise_completion_id") REFERENCES "exercise_completion"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "open_university_registration_link" | ||
DROP CONSTRAINT "open_university_registration_link_course_id_fkey", | ||
ADD CONSTRAINT "open_university_registration_link_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "organization_translation" | ||
DROP CONSTRAINT "organization_translation_organization_id_fkey", | ||
ADD CONSTRAINT "organization_translation_organization_id_fkey" | ||
FOREIGN KEY ("organization_id") REFERENCES "organization"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "study_module_translation" | ||
DROP CONSTRAINT "study_module_translation_study_module_id_fkey", | ||
ADD CONSTRAINT "study_module_translation_study_module_id_fkey" | ||
FOREIGN KEY ("study_module_id") REFERENCES "study_module"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_alias" | ||
DROP CONSTRAINT "course_alias_course_id_fkey", | ||
ADD CONSTRAINT "course_alias_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_translation" | ||
DROP CONSTRAINT "course_translation_course_id_fkey", | ||
ADD CONSTRAINT "course_translation_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_variant" | ||
DROP CONSTRAINT "course_variant_course_id_fkey", | ||
ADD CONSTRAINT "course_variant_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE CASCADE; | ||
`) | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.raw(` | ||
ALTER TABLE "exercise_completion_required_actions" | ||
DROP CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey", | ||
ADD CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey" | ||
FOREIGN KEY ("exercise_completion_id") REFERENCES "exercise_completion"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "open_university_registration_link" | ||
DROP CONSTRAINT "open_university_registration_link_course_id_fkey", | ||
ADD CONSTRAINT "open_university_registration_link_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "organization_translation" | ||
DROP CONSTRAINT "organization_translation_organization_id_fkey", | ||
ADD CONSTRAINT "organization_translation_organization_id_fkey" | ||
FOREIGN KEY ("organization_id") REFERENCES "organization"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "study_module_translation" | ||
DROP CONSTRAINT "study_module_translation_study_module_id_fkey", | ||
ADD CONSTRAINT "study_module_translation_study_module_id_fkey" | ||
FOREIGN KEY ("study_module_id") REFERENCES "study_module"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_alias" | ||
DROP CONSTRAINT "course_alias_course_id_fkey", | ||
ADD CONSTRAINT "course_alias_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_translation" | ||
DROP CONSTRAINT "course_translation_course_id_fkey", | ||
ADD CONSTRAINT "course_translation_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
await knex.raw(` | ||
ALTER TABLE "course_variant" | ||
DROP CONSTRAINT "course_variant_course_id_fkey", | ||
ADD CONSTRAINT "course_variant_course_id_fkey" | ||
FOREIGN KEY ("course_id") REFERENCES "course"("id") | ||
ON DELETE SET NULL; | ||
`) | ||
} |
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
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