Ejemplo n.º 1
0
 def setUp(self):
     def create_foreign_key_name(table_name, column_names, suffix):
         return ', '.join("%s_%s_%s" % (table_name, column_name, suffix) for column_name in column_names)
     self.reference = ForeignKeyName(
         'table', ['first_column', 'second_column'],
         'to_table', ['to_first_column', 'to_second_column'],
         '%(to_table)s_%(to_column)s_fk',
         create_foreign_key_name,
     )
Ejemplo n.º 2
0
    def _create_fk_sql(self, model, field, suffix):
        from_table = model._meta.db_table
        from_column = field.column
        to_table = 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(to_table, self.quote_name),
            to_column=Columns(to_table, [to_column], self.quote_name),
            deferrable=self.connection.ops.deferrable_sql(),
        )
Ejemplo n.º 3
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,
            )
Ejemplo n.º 4
0
class ForeignKeyNameTests(IndexNameTests):
    def setUp(self):
        def create_foreign_key_name(table_name, column_names, suffix):
            return ', '.join("%s_%s_%s" % (table_name, column_name, suffix)
                             for column_name in column_names)

        self.reference = ForeignKeyName(
            'table',
            ['first_column', 'second_column'],
            'to_table',
            ['to_first_column', 'to_second_column'],
            '%(to_table)s_%(to_column)s_fk',
            create_foreign_key_name,
        )

    def test_references_table(self):
        super().test_references_table()
        self.assertIs(self.reference.references_table('to_table'), True)

    def test_references_column(self):
        super().test_references_column()
        self.assertIs(
            self.reference.references_column('to_table', 'second_column'),
            False)
        self.assertIs(
            self.reference.references_column('to_table', 'to_second_column'),
            True)

    def test_rename_table_references(self):
        super().test_rename_table_references()
        self.reference.rename_table_references('to_table', 'other_to_table')
        self.assertIs(self.reference.references_table('other_to_table'), True)
        self.assertIs(self.reference.references_table('to_table'), False)

    def test_rename_column_references(self):
        super().test_rename_column_references()
        self.reference.rename_column_references('to_table', 'second_column',
                                                'third_column')
        self.assertIs(
            self.reference.references_column('table', 'second_column'), True)
        self.assertIs(
            self.reference.references_column('to_table', 'to_second_column'),
            True)
        self.reference.rename_column_references('to_table', 'to_first_column',
                                                'to_third_column')
        self.assertIs(
            self.reference.references_column('to_table', 'to_first_column'),
            False)
        self.assertIs(
            self.reference.references_column('to_table', 'to_third_column'),
            True)

    def test_repr(self):
        self.assertEqual(
            repr(self.reference),
            "<ForeignKeyName 'table_first_column_to_table_to_first_column_fk, "
            "table_second_column_to_table_to_first_column_fk'>")

    def test_str(self):
        self.assertEqual(
            str(self.reference),
            'table_first_column_to_table_to_first_column_fk, '
            'table_second_column_to_table_to_first_column_fk')
Ejemplo n.º 5
0
class ForeignKeyNameTests(IndexNameTests):
    def setUp(self):
        def create_foreign_key_name(table_name, column_names, suffix):
            return ', '.join("%s_%s_%s" % (table_name, column_name, suffix) for column_name in column_names)
        self.reference = ForeignKeyName(
            'table', ['first_column', 'second_column'],
            'to_table', ['to_first_column', 'to_second_column'],
            '%(to_table)s_%(to_column)s_fk',
            create_foreign_key_name,
        )

    def test_references_table(self):
        super().test_references_table()
        self.assertIs(self.reference.references_table('to_table'), True)

    def test_references_column(self):
        super().test_references_column()
        self.assertIs(self.reference.references_column('to_table', 'second_column'), False)
        self.assertIs(self.reference.references_column('to_table', 'to_second_column'), True)

    def test_rename_table_references(self):
        super().test_rename_table_references()
        self.reference.rename_table_references('to_table', 'other_to_table')
        self.assertIs(self.reference.references_table('other_to_table'), True)
        self.assertIs(self.reference.references_table('to_table'), False)

    def test_rename_column_references(self):
        super().test_rename_column_references()
        self.reference.rename_column_references('to_table', 'second_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'second_column'), True)
        self.assertIs(self.reference.references_column('to_table', 'to_second_column'), True)
        self.reference.rename_column_references('to_table', 'to_first_column', 'to_third_column')
        self.assertIs(self.reference.references_column('to_table', 'to_first_column'), False)
        self.assertIs(self.reference.references_column('to_table', 'to_third_column'), True)

    def test_repr(self):
        self.assertEqual(
            repr(self.reference),
            "<ForeignKeyName 'table_first_column_to_table_to_first_column_fk, "
            "table_second_column_to_table_to_first_column_fk'>"
        )

    def test_str(self):
        self.assertEqual(
            str(self.reference),
            'table_first_column_to_table_to_first_column_fk, '
            'table_second_column_to_table_to_first_column_fk'
        )
Ejemplo n.º 6
0
class ForeignKeyNameTests(IndexNameTests):
    def setUp(self):
        def create_foreign_key_name(table_name, column_names, suffix):
            return ", ".join("%s_%s_%s" % (table_name, column_name, suffix)
                             for column_name in column_names)

        self.reference = ForeignKeyName(
            "table",
            ["first_column", "second_column"],
            "to_table",
            ["to_first_column", "to_second_column"],
            "%(to_table)s_%(to_column)s_fk",
            create_foreign_key_name,
        )

    def test_references_table(self):
        super().test_references_table()
        self.assertIs(self.reference.references_table("to_table"), True)

    def test_references_column(self):
        super().test_references_column()
        self.assertIs(
            self.reference.references_column("to_table", "second_column"),
            False)
        self.assertIs(
            self.reference.references_column("to_table", "to_second_column"),
            True)

    def test_rename_table_references(self):
        super().test_rename_table_references()
        self.reference.rename_table_references("to_table", "other_to_table")
        self.assertIs(self.reference.references_table("other_to_table"), True)
        self.assertIs(self.reference.references_table("to_table"), False)

    def test_rename_column_references(self):
        super().test_rename_column_references()
        self.reference.rename_column_references("to_table", "second_column",
                                                "third_column")
        self.assertIs(
            self.reference.references_column("table", "second_column"), True)
        self.assertIs(
            self.reference.references_column("to_table", "to_second_column"),
            True)
        self.reference.rename_column_references("to_table", "to_first_column",
                                                "to_third_column")
        self.assertIs(
            self.reference.references_column("to_table", "to_first_column"),
            False)
        self.assertIs(
            self.reference.references_column("to_table", "to_third_column"),
            True)

    def test_repr(self):
        self.assertEqual(
            repr(self.reference),
            "<ForeignKeyName 'table_first_column_to_table_to_first_column_fk, "
            "table_second_column_to_table_to_first_column_fk'>",
        )

    def test_str(self):
        self.assertEqual(
            str(self.reference),
            "table_first_column_to_table_to_first_column_fk, "
            "table_second_column_to_table_to_first_column_fk",
        )