-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Separate task processor database #68
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
Merged
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6519b76
WIP
emyller 2ce1196
WIP: New PoC for processing tasks from multiple databases
emyller 32cba38
Consume tasks from multiple databases
emyller 1f95cc0
Merge remote-tracking branch 'github/main' into feat/separate-task-pr…
emyller ec6c2e5
WIP: Fix managing tasks when in multi-database mode
emyller c6c4acf
Cover the database router with tests
emyller 768dd77
Improve guardrail testing
emyller 09ec777
Improve database configuration for testing
emyller 0dbc71f
Make ALL tests contextualized with given database
emyller 521c7c9
Improve typing
emyller ce81064
Make database use more explicit
emyller bda53cd
Improve router docstrings
emyller afecfcc
Remove redundancy
emyller 2b32dd2
Revert "Remove redundancy"
emyller 6bb822c
Merge remote-tracking branch 'github/main' into feat/separate-task-pr…
emyller 3fef273
Move task processor database settings validation to the right place
emyller 0131747
Improve database configuration in tests
emyller 5386854
Merge remote-tracking branch 'github/main' into feat/separate-task-pr…
emyller 3768746
Delete obsolete comment
emyller dc82f8e
Refactor validation
emyller 0bb02eb
Remove unecessary documentation
emyller a6b54fb
Improve test coverage
emyller 49da185
Try and improve fixture name
emyller 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
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 |
|---|---|---|
| @@ -1,19 +1,39 @@ | ||
| # A Compose file with minimal dependencies to be able to run Flagsmith, including its test suite, locally (not in Docker). | ||
| # Minimal dependencies to run Flagsmith non-Dockerized, including its test | ||
| # suite. Currently, settings are analogous to CI for compatibility. | ||
|
|
||
| # TODO: Use Docker Compose in CI to run tests. | ||
|
|
||
| name: flagsmith | ||
|
|
||
| volumes: | ||
| pg_data: | ||
| default-database: | ||
| task-processor-database: | ||
|
|
||
| services: | ||
| db: | ||
| default-database: | ||
| image: postgres:15.5-alpine | ||
| pull_policy: always | ||
| restart: unless-stopped | ||
| volumes: | ||
| - pg_data:/var/lib/postgresql/data | ||
| - default-database:/var/lib/postgresql/data | ||
| ports: | ||
| - 5432:5432 | ||
| environment: | ||
| POSTGRES_DB: flagsmith | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_HOST_AUTH_METHOD: trust | ||
| healthcheck: | ||
| test: pg_isready -Upostgres | ||
| interval: 1s | ||
| timeout: 30s | ||
|
|
||
| task-processor-database: | ||
| image: postgres:15.5-alpine | ||
| restart: unless-stopped | ||
| volumes: | ||
| - task-processor-database:/var/lib/postgresql/data | ||
| ports: | ||
| - 5433:5432 | ||
| environment: | ||
| POSTGRES_HOST_AUTH_METHOD: trust | ||
| healthcheck: | ||
| test: pg_isready -Upostgres | ||
| interval: 1s | ||
| timeout: 30s |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| from django.conf import settings | ||
| from django.db.models import Model | ||
|
|
||
|
|
||
| class TaskProcessorRouter: | ||
| """ | ||
| Routing of database operations for task processor models | ||
| """ | ||
|
|
||
| route_app_labels = ["task_processor"] | ||
|
|
||
| @property | ||
| def is_enabled(self) -> bool: | ||
| return "task_processor" in settings.TASK_PROCESSOR_DATABASES | ||
emyller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def db_for_read(self, model: type[Model], **hints: None) -> str | None: | ||
| """ | ||
| If enabled, route "task_processor" models to the a se database | ||
emyller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| if not self.is_enabled: | ||
| return None | ||
|
|
||
| if model._meta.app_label in self.route_app_labels: | ||
| return "task_processor" | ||
|
|
||
| return None | ||
|
|
||
| def db_for_write(self, model: type[Model], **hints: None) -> str | None: | ||
| """ | ||
emyller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Attempts to write task processor models go to 'task_processor' database. | ||
| """ | ||
| if not self.is_enabled: | ||
| return None | ||
|
|
||
| if model._meta.app_label in self.route_app_labels: | ||
| return "task_processor" | ||
|
|
||
| return None | ||
|
|
||
| def allow_relation(self, obj1: Model, obj2: Model, **hints: None) -> bool | None: | ||
| """ | ||
| Relations between objects are allowed if both objects are | ||
| in the task processor database. | ||
| """ | ||
| if not self.is_enabled: | ||
| return None | ||
|
|
||
| both_objects_from_task_processor = ( | ||
| obj1._meta.app_label in self.route_app_labels | ||
| and obj2._meta.app_label in self.route_app_labels | ||
| ) | ||
|
|
||
| if both_objects_from_task_processor: | ||
| return True | ||
|
|
||
| return None | ||
|
|
||
| def allow_migrate( | ||
| self, | ||
| db: str, | ||
| app_label: str, | ||
| **hints: None, | ||
| ) -> bool | None: | ||
| """ | ||
| Allow migrations for task processor models to run in both databases | ||
| NOTE: Even if, from a fresh install, the task processor tables are not | ||
emyller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| required in both databases, this is required to allow for easier | ||
| transition between a single database and a multi-database setup. | ||
| """ | ||
| if not self.is_enabled: | ||
| return None | ||
|
|
||
| if app_label in self.route_app_labels: | ||
| return db in ["default", "task_processor"] | ||
|
|
||
| return None | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.