-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dramatically refactored how migrations are performed to work with Dja…
…ngo 1.7
- Loading branch information
Showing
4 changed files
with
189 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CategoriesConfig(AppConfig): | ||
name = 'categories' | ||
verbose_name = "Categories" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(CategoriesConfig, self).__init__(*args, **kwargs) | ||
from django.db.models.signals import class_prepared | ||
class_prepared.connect(handle_class_prepared) | ||
|
||
def ready(self): | ||
from django.db.models.signals import post_migrate | ||
from .migration import migrate_app | ||
|
||
post_migrate.connect(migrate_app) | ||
|
||
|
||
def handle_class_prepared(sender, **kwargs): | ||
""" | ||
See if this class needs registering of fields | ||
""" | ||
from .settings import M2M_REGISTRY, FK_REGISTRY | ||
from .registration import registry | ||
sender_app = sender._meta.app_label | ||
sender_name = sender._meta.model_name | ||
|
||
for key, val in FK_REGISTRY.items(): | ||
app_name, model_name = key.split('.') | ||
if app_name == sender_app and sender_name == model_name: | ||
registry.register_model(app_name, sender, 'ForeignKey', val) | ||
|
||
for key, val in M2M_REGISTRY.items(): | ||
app_name, model_name = key.split('.') | ||
if app_name == sender_app and sender_name == model_name: | ||
registry.register_model(app_name, sender, 'ManyToManyField', val) |
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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, connection | ||
from django.apps import apps | ||
|
||
|
||
def table_exists(table_name): | ||
""" | ||
Check if a table exists in the database | ||
""" | ||
pass | ||
|
||
|
||
def field_exists(app_name, model_name, field_name): | ||
""" | ||
Does the FK or M2M table exist in the database already? | ||
""" | ||
model = apps.get_model(app_name, model_name) | ||
table_name = model._meta.db_table | ||
cursor = connection.cursor() | ||
field_info = connection.introspection.get_table_description(cursor, table_name) | ||
field_names = [f.name for f in field_info] | ||
return field_name in field_names | ||
|
||
|
||
def drop_field(app_name, model_name, field_name): | ||
""" | ||
Drop the given field from the app's model | ||
""" | ||
app_config = apps.get_app_config(app_name) | ||
model = app_config.get_model(model_name) | ||
field = model._meta.get_field(field_name) | ||
with connection.schema_editor() as schema_editor: | ||
schema_editor.remove_field(model, field) | ||
|
||
|
||
def migrate_app(sender, app_config, verbosity=False, *args, **kwargs): | ||
""" | ||
Migrate all models of this app registered | ||
""" | ||
from .registration import registry | ||
|
||
app_name = app_config.label | ||
|
||
fields = [fld for fld in registry._field_registry.keys() if fld.startswith(app_name)] | ||
|
||
with connection.schema_editor() as schema_editor: | ||
for fld in fields: | ||
model_name, field_name = fld.split('.')[1:] | ||
if field_exists(app_name, model_name, field_name): | ||
continue | ||
model = app_config.get_model(model_name) | ||
schema_editor.add_field(model, registry._field_registry[fld]) |
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