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 #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):
        """
        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=Columns(table, columns, self.quote_name, col_suffixes=col_suffixes),
            extra=tablespace_sql,
        )
Beispiel #4
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),
     )
Beispiel #5
0
    def _create_fk_sql(self, model, field, suffix):
        from_table = model._meta.db_table
        from_column = field.column
        _, to_table = split_identifier(field.target_field.model._meta.db_table)
        to_column = field.target_field.column

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

        return Statement(
            self.sql_create_fk,
            table=Table(from_table, self.quote_name),
            name=ForeignKeyName(from_table, [from_column], to_table, [to_column], suffix, create_fk_name),
            column=Columns(from_table, [from_column], self.quote_name),
            to_table=Table(field.target_field.model._meta.db_table, self.quote_name),
            to_column=Columns(field.target_field.model._meta.db_table, [to_column], self.quote_name),
            deferrable=self.connection.ops.deferrable_sql(),
        )
Beispiel #6
0
    def _create_fk_sql(self, model, field, suffix):
        def create_fk_name(*args, **kwargs):
            return self.quote_name(self._create_index_name(*args, **kwargs))

        table = Table(model._meta.db_table, self.quote_name)
        name = ForeignKeyName(
            model._meta.db_table,
            [field.column],
            split_identifier(field.target_field.model._meta.db_table)[1],
            [field.target_field.column],
            suffix,
            create_fk_name,
        )
        column = Columns(model._meta.db_table, [field.column], self.quote_name)
        to_table = Table(field.target_field.model._meta.db_table,
                         self.quote_name)
        to_column = Columns(field.target_field.model._meta.db_table,
                            [field.target_field.column], self.quote_name)
        deferrable = self.connection.ops.deferrable_sql()
        table_name = model._meta.db_table
        to_table_name = field.target_field.model._meta.db_table
        if (table_name == to_table_name):
            return Statement(
                self.sql_create_fk,
                table=table,
                name=name,
                column=column,
                to_table=to_table,
                to_column=to_column,
                on_update="",
                deferrable=deferrable,
            )
        else:
            return Statement(
                self.sql_create_fk,
                table=table,
                name=name,
                column=column,
                to_table=to_table,
                to_column=to_column,
                on_update="ON UPDATE CASCADE",
                deferrable=deferrable,
            )
Beispiel #7
0
 def setUp(self):
     self.reference = Columns('table', ['first_column', 'second_column'],
                              lambda column: column.upper())
Beispiel #8
0
class ColumnsTests(TableTests):
    def setUp(self):
        self.reference = Columns('table', ['first_column', 'second_column'],
                                 lambda column: column.upper())

    def test_references_column(self):
        self.assertIs(
            self.reference.references_column('other', 'first_column'), False)
        self.assertIs(
            self.reference.references_column('table', 'third_column'), False)
        self.assertIs(
            self.reference.references_column('table', 'first_column'), True)

    def test_rename_column_references(self):
        self.reference.rename_column_references('other', 'first_column',
                                                'third_column')
        self.assertIs(
            self.reference.references_column('table', 'first_column'), True)
        self.assertIs(
            self.reference.references_column('table', 'third_column'), False)
        self.assertIs(
            self.reference.references_column('other', 'third_column'), False)
        self.reference.rename_column_references('table', 'third_column',
                                                'first_column')
        self.assertIs(
            self.reference.references_column('table', 'first_column'), True)
        self.assertIs(
            self.reference.references_column('table', 'third_column'), False)
        self.reference.rename_column_references('table', 'first_column',
                                                'third_column')
        self.assertIs(
            self.reference.references_column('table', 'first_column'), False)
        self.assertIs(
            self.reference.references_column('table', 'third_column'), True)

    def test_repr(self):
        self.assertEqual(repr(self.reference),
                         "<Columns 'FIRST_COLUMN, SECOND_COLUMN'>")

    def test_str(self):
        self.assertEqual(str(self.reference), 'FIRST_COLUMN, SECOND_COLUMN')
Beispiel #9
0
 def _index_columns(self, table, columns, col_suffixes, opclasses):
     return Columns(table, columns, self.quote_name, col_suffixes=col_suffixes)
 def setUp(self):
     self.reference = Columns(
         'table', ['first_column', 'second_column'], lambda column: column.upper()
     )
class ColumnsTests(TableTests):
    def setUp(self):
        self.reference = Columns(
            'table', ['first_column', 'second_column'], lambda column: column.upper()
        )

    def test_references_column(self):
        self.assertIs(self.reference.references_column('other', 'first_column'), False)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.assertIs(self.reference.references_column('table', 'first_column'), True)

    def test_rename_column_references(self):
        self.reference.rename_column_references('other', 'first_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), True)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.assertIs(self.reference.references_column('other', 'third_column'), False)
        self.reference.rename_column_references('table', 'third_column', 'first_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), True)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.reference.rename_column_references('table', 'first_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), False)
        self.assertIs(self.reference.references_column('table', 'third_column'), True)

    def test_repr(self):
        self.assertEqual(repr(self.reference), "<Columns 'FIRST_COLUMN, SECOND_COLUMN'>")

    def test_str(self):
        self.assertEqual(str(self.reference), 'FIRST_COLUMN, SECOND_COLUMN')
Beispiel #12
0
 def setUp(self):
     self.reference = Columns("table", ["first_column", "second_column"],
                              lambda column: column.upper())
Beispiel #13
0
class ColumnsTests(TableTests):
    def setUp(self):
        self.reference = Columns("table", ["first_column", "second_column"],
                                 lambda column: column.upper())

    def test_references_column(self):
        self.assertIs(
            self.reference.references_column("other", "first_column"), False)
        self.assertIs(
            self.reference.references_column("table", "third_column"), False)
        self.assertIs(
            self.reference.references_column("table", "first_column"), True)

    def test_rename_column_references(self):
        self.reference.rename_column_references("other", "first_column",
                                                "third_column")
        self.assertIs(
            self.reference.references_column("table", "first_column"), True)
        self.assertIs(
            self.reference.references_column("table", "third_column"), False)
        self.assertIs(
            self.reference.references_column("other", "third_column"), False)
        self.reference.rename_column_references("table", "third_column",
                                                "first_column")
        self.assertIs(
            self.reference.references_column("table", "first_column"), True)
        self.assertIs(
            self.reference.references_column("table", "third_column"), False)
        self.reference.rename_column_references("table", "first_column",
                                                "third_column")
        self.assertIs(
            self.reference.references_column("table", "first_column"), False)
        self.assertIs(
            self.reference.references_column("table", "third_column"), True)

    def test_repr(self):
        self.assertEqual(repr(self.reference),
                         "<Columns 'FIRST_COLUMN, SECOND_COLUMN'>")

    def test_str(self):
        self.assertEqual(str(self.reference), "FIRST_COLUMN, SECOND_COLUMN")