Beispiel #1
0
def digest(connection, *args):
    """Return a digest hash for a set of arguments.

    This is mostly used as part of the index/constraint name generation
    processes. It offers compatibility with a range of Django versions.

    Args:
        connection (object):
            The database connection.

        *args (tuple):
            The positional arguments used to build the digest hash out of.

    Returns:
        str:
        The resulting digest hash.
    """
    if (BaseDatabaseSchemaEditor and
        hasattr(BaseDatabaseSchemaEditor, '_digest')):
        # Django >= 1.8
        #
        # Note that _digest() is a classmethod that is common across all
        # database backends. We don't need to worry about using a
        # per-instance version. If that changes, we'll need to create a
        # SchemaEditor.
        return BaseDatabaseSchemaEditor._digest(*args)
    else:
        # Django < 1.8
        return connection.creation._digest(*args)
Beispiel #2
0
    def _sql_indexes_for_field(self, model, field):
        """
        Return the CREATE INDEX SQL statements for a single model field

        :param model:
        :param field:
        :return:
        """
        def qn(name):
            if name.startswith('"') and name.endswith('"'):
                return name  # Quoting once is enough.
            return '"%s"' % name

        max_name_length = 63

        if field.db_index and not field.unique:
            i_name = "%s_%s" % (
                model._meta.db_table,
                BaseDatabaseSchemaEditor._digest(field.column),
            )
            return [
                "CREATE INDEX %s ON %s(%s)" % (
                    qn(truncate_name(i_name, max_name_length)),
                    qn(model._meta.db_table),
                    qn(field.column),
                )
            ]
        return []
Beispiel #3
0
def digest(connection, *args):
    """Return a digest hash for a set of arguments.

    This is mostly used as part of the index/constraint name generation
    processes. It offers compatibility with a range of Django versions.

    Args:
        connection (object):
            The database connection.

        *args (tuple):
            The positional arguments used to build the digest hash out of.

    Returns:
        str:
        The resulting digest hash.
    """
    if (BaseDatabaseSchemaEditor
            and hasattr(BaseDatabaseSchemaEditor, '_digest')):
        # Django >= 1.8
        #
        # Note that _digest() is a classmethod that is common across all
        # database backends. We don't need to worry about using a
        # per-instance version. If that changes, we'll need to create a
        # SchemaEditor.
        return BaseDatabaseSchemaEditor._digest(*args)
    else:
        # Django < 1.8
        return connection.creation._digest(*args)