Python django.db.migrations.CreateModel() Examples

The following are code examples for showing how to use django.db.migrations.CreateModel(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 2
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            # Don't allow optimisations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 3
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 4
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 5
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 6
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 7
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 8
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 9
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            # Don't allow optimisations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 10
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 11
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            # Don't allow optimisations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 12
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 6 votes vote down vote up
def reduce_create_model_add_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            # Don't allow optimizations of FKs through models they reference
            if hasattr(other.field, "rel") and other.field.rel:
                for between in in_between:
                    # Check that it doesn't point to the model
                    app_label, object_name = self.model_to_key(other.field.rel.to)
                    if between.references_model(object_name, app_label):
                        return None
                    # Check that it's not through the model
                    if getattr(other.field.rel, "through", None):
                        app_label, object_name = self.model_to_key(other.field.rel.through)
                        if between.references_model(object_name, app_label):
                            return None
            # OK, that's fine
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=operation.fields + [(other.name, other.field)],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 13
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 14
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 15
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 16
Project: chattR   Author: patrickstocklin   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 17
Project: chattR   Author: patrickstocklin   File: executor.py    GNU General Public License v2.0 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 18
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name.lower() == other.name.lower() and
                not operation.options.get("proxy", False)):
            return [] 
Example 19
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name.lower() == other.old_name.lower():
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 20
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 21
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 22
Project: AneMo   Author: jspargo   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 23
Project: AneMo   Author: jspargo   File: executor.py    GNU General Public License v2.0 5 votes vote down vote up
def detect_soft_applied(self, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        apps = project_state.render()
        found_create_migration = False
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.get_table_list(self.connection.cursor()):
                    return False
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration 
Example 24
Project: AneMo   Author: jspargo   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def set_up_test_model(self):
        operations = [migrations.CreateModel(
            "Neighborhood",
            [
                ("id", models.AutoField(primary_key=True)),
                ('name', models.CharField(max_length=100, unique=True)),
                ('geom', fields.MultiPolygonField(srid=4326)),
            ],
        )]
        return self.apply_operations('gis', ProjectState(), operations) 
Example 25
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 26
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 27
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 28
Project: GTDWeb   Author: lanbing510   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 29
Project: GTDWeb   Author: lanbing510   File: executor.py    GNU General Public License v2.0 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 30
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 31
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 32
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 33
Project: Fun5G   Author: OpenWinCon   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 34
Project: Fun5G   Author: OpenWinCon   File: executor.py    Apache License 2.0 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 35
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 36
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 37
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 38
Project: Vigtech-Services   Author: VigTech   File: optimizer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 39
Project: Vigtech-Services   Author: VigTech   File: executor.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 40
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 41
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 42
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 43
Project: DroidPot   Author: WhySoGeeky   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 44
Project: DroidPot   Author: WhySoGeeky   File: executor.py    MIT License 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 45
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 46
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 47
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 48
Project: mudong_blog   Author: mudong1991   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 49
Project: mudong_blog   Author: mudong1991   File: executor.py    GNU General Public License v2.0 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 50
Project: django-mpathy   Author: craigds   File: operations.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def inject_pre_migration_operations(plan=None, apps=global_apps, using=DEFAULT_DB_ALIAS, **kwargs):
    """
    Insert a `LTreeExtension` operation before every planned `CreateModel` operation.
    """
    if plan is None:
        return

    for migration, backward in plan:
        for index, operation in enumerate(migration.operations):
            if isinstance(operation, migrations.CreateModel):
                for name, field in operation.fields:
                    if isinstance(field, LTreeField):
                        migration.operations.insert(index, LTreeExtension())
                        return 
Example 51
Project: django-mpathy   Author: craigds   File: operations.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def inject_post_migration_operations(plan=None, apps=global_apps, using=DEFAULT_DB_ALIAS, **kwargs):
    if plan is None:
        return

    for migration, backward in plan:
        for index, operation in reversed(list(enumerate(migration.operations))):
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                post_migrate_mpathnode(model) 
Example 52
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 53
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 54
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 55
Project: workspace_2016   Author: nwiizo   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 56
Project: workspace_2016   Author: nwiizo   File: executor.py    MIT License 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 57
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name.lower() == other.name.lower() and
                not operation.options.get("proxy", False)):
            return [] 
Example 58
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name.lower() == other.old_name.lower():
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 59
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 60
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 61
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: optimizer.py    GNU General Public License v2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 62
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: executor.py    GNU General Public License v2.0 5 votes vote down vote up
def detect_soft_applied(self, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        apps = project_state.render()
        found_create_migration = False
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.get_table_list(self.connection.cursor()):
                    return False
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration 
Example 63
Project: CMPUT410W15-project   Author: CMPUT410W15T02   File: test_operations.py    GNU General Public License v2.0 5 votes vote down vote up
def set_up_test_model(self):
        operations = [migrations.CreateModel(
            "Neighborhood",
            [
                ("id", models.AutoField(primary_key=True)),
                ('name', models.CharField(max_length=100, unique=True)),
                ('geom', fields.MultiPolygonField(srid=4326)),
            ],
        )]
        return self.apply_operations('gis', ProjectState(), operations) 
Example 64
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 65
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 66
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 67
Project: ga_statistics   Author: wolfskaempf   File: optimizer.py    MIT License 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 68
Project: ga_statistics   Author: wolfskaempf   File: executor.py    MIT License 5 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False, project_state
        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_migration = False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration, after_state 
Example 69
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name.lower() == other.name.lower() and
                not operation.options.get("proxy", False)):
            return [] 
Example 70
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name.lower() == other.old_name.lower():
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 71
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 72
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def reduce_create_model_rename_field(self, operation, other, in_between):
        if operation.name.lower() == other.model_name.lower():
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (other.new_name if n == other.old_name else n, v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                )
            ] 
Example 73
Project: alarm   Author: G8bao7   File: optimizer.py    Apache License 2.0 5 votes vote down vote up
def can_optimize_through(self, operation, other, app_label=None):
        """
        Returns True if it's possible to optimize 'operation' with something
        the other side of 'other'. This is possible if, for example, they
        affect different models.
        """
        MODEL_LEVEL_OPERATIONS = (
            migrations.CreateModel,
            migrations.AlterModelTable,
            migrations.AlterUniqueTogether,
            migrations.AlterIndexTogether,
        )
        FIELD_LEVEL_OPERATIONS = (
            migrations.AddField,
            migrations.AlterField,
        )
        # If it's a model level operation, let it through if there's
        # nothing that looks like a reference to us in 'other'.
        if isinstance(operation, MODEL_LEVEL_OPERATIONS):
            if not other.references_model(operation.name, app_label):
                return True
        # If it's field level, only let it through things that don't reference
        # the field (which includes not referencing the model)
        if isinstance(operation, FIELD_LEVEL_OPERATIONS):
            if not other.references_field(operation.model_name, operation.name, app_label):
                return True
        return False 
Example 74
Project: alarm   Author: G8bao7   File: executor.py    Apache License 2.0 5 votes vote down vote up
def detect_soft_applied(self, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        apps = project_state.render()
        found_create_migration = False
        # Bail if the migration isn't the first one in its app
        if [name for app, name in migration.dependencies if app == migration.app_label]:
            return False
        # Make sure all create model are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.db_table not in self.connection.introspection.get_table_list(self.connection.cursor()):
                    return False
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration 
Example 75
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 5 votes vote down vote up
def reduce_model_create_delete(self, operation, other, in_between):
        """
        Folds a CreateModel and a DeleteModel into nothing.
        """
        if (operation.name_lower == other.name_lower and
                not operation.options.get("proxy", False)):
            return [] 
Example 76
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 5 votes vote down vote up
def reduce_model_create_rename(self, operation, other, in_between):
        """
        Folds a model rename into its create
        """
        if operation.name_lower == other.old_name_lower:
            return [
                migrations.CreateModel(
                    other.new_name,
                    fields=operation.fields,
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 77
Project: django-wechat-api   Author: crazy-canux   File: optimizer.py    GNU General Public License v3.0 5 votes vote down vote up
def reduce_create_model_alter_field(self, operation, other, in_between):
        if operation.name_lower == other.model_name_lower:
            return [
                migrations.CreateModel(
                    operation.name,
                    fields=[
                        (n, other.field if n == other.name else v)
                        for n, v in operation.fields
                    ],
                    options=operation.options,
                    bases=operation.bases,
                    managers=operation.managers,
                )
            ] 
Example 78
Project: django-move-model   Author: alexei   File: movemodel.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _get_create_model_migration(self, dependencies=[]):
        database_operations = []
        state_operations = []

        model_state = self.to_state.models[self.dest_app, self.model_name.lower()]
        model_opts = self.to_state.apps.get_model(self.dest_app, self.model_name)._meta
        related_fields = {}
        for field in model_opts.local_fields:
            if field.remote_field:
                if field.remote_field.model:
                    if not field.remote_field.parent_link:
                        related_fields[field.name] = field
                if (getattr(field.remote_field, 'through', None) and
                        not field.remote_field.through._meta.auto_created):
                    related_fields[field.name] = field
        for field in model_opts.local_many_to_many:
            if field.remote_field.model:
                related_fields[field.name] = field
            if getattr(field.remote_field, 'through', None) and not field.remote_field.through._meta.auto_created:
                related_fields[field.name] = field

        state_operations.append(
            migrations.CreateModel(
                name=model_state.name,
                fields=[d for d in model_state.fields if d[0] not in related_fields],
                options=model_state.options,
                bases=model_state.bases,
                managers=model_state.managers,
            )
        )

        migration = Migration('create_model', self.dest_app)
        migration.dependencies = dependencies
        migration.operations = [
            migrations.SeparateDatabaseAndState(
                database_operations=database_operations,
                state_operations=state_operations,
            )
        ]
        return migration 
Example 79
Project: openhgsenti   Author: drexly   File: executor.py    Apache License 2.0 4 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables or columns it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel and AddField).
        """
        if migration.initial is None:
            # Bail if the migration isn't the first one in its app
            if any(app == migration.app_label for app, name in migration.dependencies):
                return False, project_state
        elif migration.initial is False:
            # Bail if it's NOT an initial migration
            return False, project_state

        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_model_migration = False
        found_add_field_migration = False
        # Make sure all create model and add field operations are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_model_migration = True
            elif isinstance(operation, migrations.AddField):
                model = apps.get_model(migration.app_label, operation.model_name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue

                table = model._meta.db_table
                db_field = model._meta.get_field(operation.name).column
                fields = self.connection.introspection.get_table_description(self.connection.cursor(), table)
                if db_field not in (f.name for f in fields):
                    return False, project_state
                found_add_field_migration = True
        # If we get this far and we found at least one CreateModel or AddField migration,
        # the migration is considered implicitly applied.
        return (found_create_model_migration or found_add_field_migration), after_state 
Example 80
Project: open-cabinet   Author: CivicTechTO   File: executor.py    MIT License 4 votes vote down vote up
def detect_soft_applied(self, project_state, migration):
        """
        Tests whether a migration has been implicitly applied - that the
        tables or columns it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel and AddField).
        """
        if migration.initial is None:
            # Bail if the migration isn't the first one in its app
            if any(app == migration.app_label for app, name in migration.dependencies):
                return False, project_state
        elif migration.initial is False:
            # Bail if it's NOT an initial migration
            return False, project_state

        if project_state is None:
            after_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        else:
            after_state = migration.mutate_state(project_state)
        apps = after_state.apps
        found_create_model_migration = False
        found_add_field_migration = False
        # Make sure all create model and add field operations are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                    return False, project_state
                found_create_model_migration = True
            elif isinstance(operation, migrations.AddField):
                model = apps.get_model(migration.app_label, operation.model_name)
                if model._meta.swapped:
                    # We have to fetch the model to test with from the
                    # main app cache, as it's not a direct dependency.
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue

                table = model._meta.db_table
                db_field = model._meta.get_field(operation.name).column
                fields = self.connection.introspection.get_table_description(self.connection.cursor(), table)
                if db_field not in (f.name for f in fields):
                    return False, project_state
                found_add_field_migration = True
        # If we get this far and we found at least one CreateModel or AddField migration,
        # the migration is considered implicitly applied.
        return (found_create_model_migration or found_add_field_migration), after_state