-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Acquire lock on singleton to handle parallel pod deployments (#10)
Co-authored-by: Ronny V <[email protected]>
- Loading branch information
Showing
5 changed files
with
68 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,26 @@ | ||
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist | ||
from django.db import ProgrammingError, models | ||
|
||
from django_migration_zero.exceptions import MissingMigrationZeroConfigRecordError | ||
from django_migration_zero.helpers.logger import get_logger | ||
|
||
|
||
class MigrationZeroConfigurationQuerySet(models.QuerySet): | ||
pass | ||
|
||
|
||
class MigrationZeroConfigurationManager(models.Manager): | ||
def fetch_singleton(self) -> None: | ||
logger = get_logger() | ||
try: | ||
number_records = self.count() | ||
config_singleton = self.select_for_update().get() | ||
except ProgrammingError: | ||
logger.warning( | ||
"The migration zero table is missing. This might be ok for the first installation of " | ||
'"django-migration-zero" but if you see this warning after that point, something went sideways.' | ||
) | ||
return None | ||
|
||
if number_records > 1: | ||
config_singleton = None | ||
except MultipleObjectsReturned as e: | ||
raise MissingMigrationZeroConfigRecordError( | ||
"Too many configuration records detected. There can only be one." | ||
) | ||
|
||
config_singleton = self.all().first() | ||
if not config_singleton: | ||
raise MissingMigrationZeroConfigRecordError("No configuration record found in the database.") | ||
) from e | ||
except ObjectDoesNotExist as e: | ||
raise MissingMigrationZeroConfigRecordError("No configuration record found in the database.") from e | ||
|
||
return config_singleton | ||
|
||
|
||
MigrationZeroConfigurationManager = MigrationZeroConfigurationManager.from_queryset(MigrationZeroConfigurationQuerySet) |
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