Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created May 8, 2021 22:32
Show Gist options
  • Save iKlotho/479e8aa8469b3c647b3b67d2720e1108 to your computer and use it in GitHub Desktop.
Save iKlotho/479e8aa8469b3c647b3b67d2720e1108 to your computer and use it in GitHub Desktop.
Using Django post_migrate signal
######################################
############# models.py ##############
######################################
from architect.commands import partition
from django.db import ProgrammingError
def create_partitions(sender, **kwargs):
"""
After running migrations, go through each of the models
in the app and ensure the partitions have been setup
"""
paths = {model.__module__ for model in sender.get_models()}
for path in paths:
try:
partition.run(dict(module=path))
except ProgrammingError:
# Possibly because models were just un-migrated or
# fields have been changed that effect Architect
print(f"Unable to apply partitions for module '{path}'")
else:
print(f"Applied partitions for module '{path}'")
######################################
############# apps.py ################
######################################
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from .models import create_partitions
class CoreConfig(AppConfig):
name = 'core'
def ready(self):
# Hook up Architect to the post migrations signal
post_migrate.connect(create_partitions, sender=self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment