-
Notifications
You must be signed in to change notification settings - Fork 3
shanbady/separate database router and schema for program certificates #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shanbady
merged 5 commits into
main
from
shanbady/separate-database-router-for-program-certificates2
Mar 18, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27b7dd2
adding working migrations to use external schema
shanbady 2ffe04d
adding migration to set programcertificates to unmanaged
shanbady 8f31eea
fixing tests and adding test for routers
shanbady 4258c9b
fixing router argument
shanbady 07d2eb6
adding doc
shanbady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| from django.conf import settings | ||
|
|
||
|
|
||
| class ReadOnlyModelError(Exception): | ||
| """Raised when a write was attempted on a read-only model""" | ||
|
|
||
|
|
||
| class ExternalSchemaRouter: | ||
| def db_for_write(self, model, **meta): # noqa: ARG002 | ||
| model_name = model._meta.model_name # noqa: SLF001 | ||
| if model_name in settings.EXTERNAL_MODELS: | ||
| exception_message = f"model {model_name} is read only" | ||
| raise ReadOnlyModelError(exception_message) |
This file contains hidden or 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,12 @@ | ||
| import pytest | ||
|
|
||
| from main.routers import ReadOnlyModelError | ||
| from profiles.factories import ProgramCertificateFactory | ||
|
|
||
|
|
||
| def test_external_tables_are_readonly(): | ||
| """ | ||
| Test that external tables cannot be written to | ||
| """ | ||
| with pytest.raises(ReadOnlyModelError): | ||
| ProgramCertificateFactory(user_email="test@test.com", program_title="test") |
This file contains hidden or 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
95 changes: 95 additions & 0 deletions
95
profiles/migrations/0018_alter_programletter_certificate_and_more.py
This file contains hidden or 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,95 @@ | ||
| # Generated by Django 4.2.11 on 2024-03-14 23:16 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("profiles", "0017_programletter"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunSQL('DROP TABLE "external.programcertificate" CASCADE'), | ||
| migrations.CreateModel( | ||
| name="ProgramCertificate", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("user_edxorg_id", models.IntegerField(blank=True, null=True)), | ||
| ("micromasters_program_id", models.IntegerField(blank=True, null=True)), | ||
| ("mitxonline_program_id", models.IntegerField(blank=True, null=True)), | ||
| ( | ||
| "user_edxorg_username", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_email", | ||
| models.CharField(blank=False, max_length=256), | ||
| ), | ||
| ( | ||
| "program_title", | ||
| models.CharField(blank=False, max_length=256), | ||
| ), | ||
| ( | ||
| "user_gender", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_address_city", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_first_name", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_last_name", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_full_name", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_year_of_birth", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_country", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_address_postal_code", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_street_address", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_address_state_or_territory", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "user_mitxonline_username", | ||
| models.CharField(blank=True, max_length=256), | ||
| ), | ||
| ( | ||
| "program_completion_timestamp", | ||
| models.DateTimeField(blank=True, null=True), | ||
| ), | ||
| ], | ||
| options={"db_table": '"external"."programcertificate"'}, | ||
| ), | ||
| migrations.AlterModelTable( | ||
| name="programcertificate", | ||
| table='"external"."programcertificate"', | ||
| ), | ||
| ] |
26 changes: 26 additions & 0 deletions
26
profiles/migrations/0019_alter_programcertificate_options_and_more.py
This file contains hidden or 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,26 @@ | ||
| # Generated by Django 4.2.11 on 2024-03-15 13:43 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("profiles", "0018_alter_programletter_certificate_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterModelOptions( | ||
| name="programcertificate", | ||
| options={"managed": False}, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="programletter", | ||
| name="certificate", | ||
| field=models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="profiles.programcertificate", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun.