Beispiel #1
0
    def setUp(self):
        def create_index_name(table_name, column_names, suffix):
            return ', '.join("%s_%s_%s" % (table_name, column_name, suffix)
                             for column_name in column_names)

        self.reference = IndexName('table', ['first_column', 'second_column'],
                                   'suffix', create_index_name)
Beispiel #2
0
    def _create_unique_sql(self, model, columns, name=None, condition=None):
        def create_unique_name(*args, **kwargs):
            return self.quote_name(self._create_index_name(*args, **kwargs))

        table = Table(model._meta.db_table, self.quote_name)
        if name is None:
            name = IndexName(model._meta.db_table, columns, '_uniq', create_unique_name)
        else:
            name = self.quote_name(name)
        columns = Columns(table, columns, self.quote_name)
        if condition:
            return Statement(
                self.sql_create_unique_index,
                table=table,
                name=name,
                columns=columns,
                condition=' WHERE ' + condition,
            ) if self.connection.features.supports_partial_indexes else None
        else:
            return Statement(
                self.sql_create_unique,
                table=table,
                name=name,
                columns=columns,
            )
Beispiel #3
0
    def _create_index_sql(self, model, fields, *, name=None, suffix='', using='',
                          db_tablespace=None, col_suffixes=(), sql=None, opclasses=(),
                          condition=None):
        """
        Return the SQL statement to create the index for one or several fields.
        `sql` can be specified if the syntax differs from the standard (GIS
        indexes, ...).
        """
        tablespace_sql = self._get_index_tablespace_sql(model, fields, db_tablespace=db_tablespace)
        columns = [field.column for field in fields]
        sql_create_index = sql or self.sql_create_index
        table = model._meta.db_table

        def create_index_name(*args, **kwargs):
            nonlocal name
            if name is None:
                name = self._create_index_name(*args, **kwargs)
            return self.quote_name(name)

        return Statement(
            sql_create_index,
            table=Table(table, self.quote_name),
            name=IndexName(table, columns, suffix, create_index_name),
            using=using,
            columns=self._index_columns(table, columns, col_suffixes, opclasses),
            extra=tablespace_sql,
            condition=(' WHERE ' + condition) if condition else '',
        )
    def _create_unique_sql(self, model, columns, name=None, condition=None, deferrable=None):
        if (deferrable and not getattr(self.connection.features, 'supports_deferrable_unique_constraints', False)):
            return None

        def create_unique_name(*args, **kwargs):
            return self.quote_name(self._create_index_name(*args, **kwargs))

        table = Table(model._meta.db_table, self.quote_name)
        if name is None:
            name = IndexName(model._meta.db_table, columns, '_uniq', create_unique_name)
        else:
            name = self.quote_name(name)
        columns = Columns(table, columns, self.quote_name)
        statement_args = {
            "deferrable": self._deferrable_constraint_sql(deferrable)
        } if django.VERSION >= (3, 1) else {}

        if condition:
            return Statement(
                self.sql_create_unique_index,
                table=table,
                name=name,
                columns=columns,
                condition=' WHERE ' + condition,
                **statement_args
            ) if self.connection.features.supports_partial_indexes else None
        else:
            return Statement(
                self.sql_create_unique,
                table=table,
                name=name,
                columns=columns,
                **statement_args
            )
Beispiel #5
0
    def setUp(self):
        def create_index_name(table_name, column_names, suffix):
            return ", ".join("%s_%s_%s" % (table_name, column_name, suffix)
                             for column_name in column_names)

        self.reference = IndexName("table", ["first_column", "second_column"],
                                   "suffix", create_index_name)
Beispiel #6
0
 def _create_unique_sql(self, model, columns):
     table = model._meta.db_table
     return Statement(
         self.sql_create_unique,
         table=Table(table, self.quote_name),
         name=IndexName(table, columns, '_uniq', self._create_index_name),
         columns=Columns(table, columns, self.quote_name),
     )
    def alter_field(self, model, old_field, new_field, strict=False):

        if DJANGO_VERISON >= StrictVersion('2.1'):
            from django.db.backends.ddl_references import IndexName
            if self._unique_should_be_added(old_field, new_field):
                table = model._meta.db_table
                index_name = str(IndexName(table, [new_field.column], '_uniq', self._create_index_name))
                self.execute(
                    self._create_index_sql(model, [new_field], name=index_name, sql=SQL_CREATE_UNIQUE_INDEX)
                )
                self.execute(self._create_unique_constraint_from_index_sql(table, index_name))
                self.already_added_unique = True

        return super(ZeroDownTimeMixin, self).alter_field(model, old_field, new_field, strict=strict)
Beispiel #8
0
    def _create_index_sql(self, model, fields, suffix="", sql=None):
        """
        Return the SQL statement to create the index for one or several fields.
        `sql` can be specified if the syntax differs from the standard (GIS
        indexes, ...).
        """
        tablespace_sql = self._get_index_tablespace_sql(model, fields)
        columns = [field.column for field in fields]
        sql_create_index = sql or self.sql_create_index
        table = model._meta.db_table

        def create_index_name(*args, **kwargs):
            return self.quote_name(self._create_index_name(*args, **kwargs))

        return Statement(
            sql_create_index,
            table=Table(table, self.quote_name),
            name=IndexName(table, columns, suffix, create_index_name),
            using='',
            columns=Columns(table, columns, self.quote_name),
            extra=tablespace_sql,
        )