-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial experimental multi database support
- Loading branch information
Showing
17 changed files
with
266 additions
and
33 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
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
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
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
from django.db import models | ||
|
||
|
||
# Routed to database "main". | ||
class Item(models.Model): | ||
name = models.CharField(max_length=100) # type: str | ||
|
||
|
||
# Routed to database "second". | ||
class SecondItem(models.Model): | ||
name = models.CharField(max_length=100) # type: str |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class DbRouter: | ||
def db_for_read(self, model, **hints): | ||
if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem': | ||
return 'second' | ||
return None | ||
|
||
def db_for_write(self, model, **hints): | ||
if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem': | ||
return 'second' | ||
return None | ||
|
||
def allow_migrate(self, db, app_label, model_name=None, **hints): | ||
if app_label == 'app' and model_name == 'seconditem': | ||
return db == 'second' |
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 |
---|---|---|
|
@@ -27,3 +27,5 @@ | |
"OPTIONS": {}, | ||
} | ||
] | ||
|
||
DATABASE_ROUTERS = ['pytest_django_test.db_router.DbRouter'] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
from .settings_base import * # noqa: F401 F403 | ||
|
||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": "/should_not_be_accessed", | ||
} | ||
}, | ||
"replica": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": "/should_not_be_accessed", | ||
"TEST": { | ||
"MIRROR": "default", | ||
}, | ||
}, | ||
"second": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": "/should_not_be_accessed", | ||
}, | ||
} |
Oops, something went wrong.