Example #1
0
    def test_utils_drop_unique_constraint(self):
        table_name = "__test_tmp_table__"
        uc_name = "uniq_foo"
        values = [{"id": 1, "a": 3, "foo": 10}, {"id": 2, "a": 2, "foo": 20}, {"id": 3, "a": 1, "foo": 30}]
        test_table = Table(
            table_name,
            self.meta,
            Column("id", Integer, primary_key=True, nullable=False),
            Column("a", Integer),
            Column("foo", Integer),
            UniqueConstraint("a", name="uniq_a"),
            UniqueConstraint("foo", name=uc_name),
        )
        test_table.create()

        self.engine.execute(test_table.insert(), values)
        # NOTE(boris-42): This method is generic UC dropper.
        utils.drop_unique_constraint(self.engine, table_name, uc_name, "foo")

        s = test_table.select().order_by(test_table.c.id)
        rows = self.engine.execute(s).fetchall()

        for i in moves.range(len(values)):
            v = values[i]
            self.assertEqual((v["id"], v["a"], v["foo"]), rows[i])

        # NOTE(boris-42): Update data about Table from DB.
        meta = MetaData(bind=self.engine)
        test_table = Table(table_name, meta, autoload=True)
        constraints = [c for c in test_table.constraints if c.name == uc_name]
        self.assertEqual(len(constraints), 0)
        self.assertEqual(len(test_table.constraints), 1)

        test_table.drop()
Example #2
0
    def test_drop_unique_constraint_in_sqlite_fk_recreate(self):
        engine = self.engines['sqlite']
        meta = MetaData()
        meta.bind = engine
        parent_table = Table(
            'table0',
            meta,
            Column('id', Integer, primary_key=True),
            Column('foo', Integer),
        )
        parent_table.create()
        table_name = 'table1'
        table = Table(table_name, meta, Column('id', Integer,
                                               primary_key=True),
                      Column('baz', Integer),
                      Column('bar', Integer, ForeignKey("table0.id")),
                      UniqueConstraint('baz', name='constr1'))
        table.create()
        utils.drop_unique_constraint(engine, table_name, 'constr1', 'baz')

        insp = reflection.Inspector.from_engine(engine)
        f_keys = insp.get_foreign_keys(table_name)
        self.assertEqual(len(f_keys), 1)
        f_key = f_keys[0]
        self.assertEqual(f_key['referred_table'], 'table0')
        self.assertEqual(f_key['referred_columns'], ['id'])
        self.assertEqual(f_key['constrained_columns'], ['bar'])
Example #3
0
    def test_drop_unique_constraint_in_sqlite_fk_recreate(self):
        engine = self.engines['sqlite']
        meta = MetaData()
        meta.bind = engine
        parent_table = Table(
            'table0', meta,
            Column('id', Integer, primary_key=True),
            Column('foo', Integer),
        )
        parent_table.create()
        table_name = 'table1'
        table = Table(
            table_name, meta,
            Column('id', Integer, primary_key=True),
            Column('baz', Integer),
            Column('bar', Integer, ForeignKey("table0.id")),
            UniqueConstraint('baz', name='constr1')
        )
        table.create()
        utils.drop_unique_constraint(engine, table_name, 'constr1', 'baz')

        insp = reflection.Inspector.from_engine(engine)
        f_keys = insp.get_foreign_keys(table_name)
        self.assertEqual(len(f_keys), 1)
        f_key = f_keys[0]
        self.assertEqual(f_key['referred_table'], 'table0')
        self.assertEqual(f_key['referred_columns'], ['id'])
        self.assertEqual(f_key['constrained_columns'], ['bar'])
Example #4
0
    def test_util_drop_unique_constraint_with_not_supported_sqlite_type(self):
        table_name = "__test_tmp_table__"
        uc_name = 'uniq_foo'
        values = [
            {'id': 1, 'a': 3, 'foo': 10},
            {'id': 2, 'a': 2, 'foo': 20},
            {'id': 3, 'a': 1, 'foo': 30}
        ]

        engine = self.engines['sqlite']
        meta = MetaData(bind=engine)

        test_table = Table(
            table_name, meta,
            Column('id', Integer, primary_key=True, nullable=False),
            Column('a', Integer),
            Column('foo', CustomType, default=0),
            UniqueConstraint('a', name='uniq_a'),
            UniqueConstraint('foo', name=uc_name),
        )
        test_table.create()

        engine.execute(test_table.insert(), values)
        warnings.simplefilter("ignore", SAWarning)

        # reflection of custom types has been fixed upstream
        if SA_VERSION < (0, 9, 0):
            # NOTE(boris-42): Missing info about column `foo` that has
            #                 unsupported type CustomType.
            self.assertRaises(utils.ColumnError,
                              utils.drop_unique_constraint,
                              engine, table_name, uc_name, 'foo')

            # NOTE(boris-42): Wrong type of foo instance. it should be
            #                 instance of sqlalchemy.Column.
            self.assertRaises(utils.ColumnError,
                              utils.drop_unique_constraint,
                              engine, table_name, uc_name, 'foo',
                              foo=Integer())

        foo = Column('foo', CustomType, default=0)
        utils.drop_unique_constraint(
            engine, table_name, uc_name, 'foo', foo=foo)

        s = test_table.select().order_by(test_table.c.id)
        rows = engine.execute(s).fetchall()

        for i in moves.range(len(values)):
            v = values[i]
            self.assertEqual((v['id'], v['a'], v['foo']), rows[i])

        # NOTE(boris-42): Update data about Table from DB.
        meta = MetaData(bind=engine)
        test_table = Table(table_name, meta, autoload=True)
        constraints = [c for c in test_table.constraints if c.name == uc_name]
        self.assertEqual(len(constraints), 0)
        self.assertEqual(len(test_table.constraints), 1)
        test_table.drop()
Example #5
0
    def test_utils_drop_unique_constraint(self):
        table_name = "__test_tmp_table__"
        uc_name = 'uniq_foo'
        values = [
            {
                'id': 1,
                'a': 3,
                'foo': 10
            },
            {
                'id': 2,
                'a': 2,
                'foo': 20
            },
            {
                'id': 3,
                'a': 1,
                'foo': 30
            },
        ]
        for engine in self.engines.values():
            meta = MetaData()
            meta.bind = engine
            test_table = Table(
                table_name,
                meta,
                Column('id', Integer, primary_key=True, nullable=False),
                Column('a', Integer),
                Column('foo', Integer),
                UniqueConstraint('a', name='uniq_a'),
                UniqueConstraint('foo', name=uc_name),
            )
            test_table.create()

            engine.execute(test_table.insert(), values)
            # NOTE(boris-42): This method is generic UC dropper.
            utils.drop_unique_constraint(engine, table_name, uc_name, 'foo')

            s = test_table.select().order_by(test_table.c.id)
            rows = engine.execute(s).fetchall()

            for i in moves.range(len(values)):
                v = values[i]
                self.assertEqual((v['id'], v['a'], v['foo']), rows[i])

            # NOTE(boris-42): Update data about Table from DB.
            meta = MetaData()
            meta.bind = engine
            test_table = Table(table_name, meta, autoload=True)
            constraints = [
                c for c in test_table.constraints if c.name == uc_name
            ]
            self.assertEqual(len(constraints), 0)
            self.assertEqual(len(test_table.constraints), 1)

            test_table.drop()
Example #6
0
    def test_utils_drop_unique_constraint(self):
        table_name = "__test_tmp_table__"
        uc_name = 'uniq_foo'
        values = [
            {'id': 1, 'a': 3, 'foo': 10},
            {'id': 2, 'a': 2, 'foo': 20},
            {'id': 3, 'a': 1, 'foo': 30},
        ]
        for engine in self.engines.values():
            meta = MetaData()
            meta.bind = engine
            test_table = Table(
                table_name, meta,
                Column('id', Integer, primary_key=True, nullable=False),
                Column('a', Integer),
                Column('foo', Integer),
                UniqueConstraint('a', name='uniq_a'),
                UniqueConstraint('foo', name=uc_name),
            )
            test_table.create()

            engine.execute(test_table.insert(), values)
            # NOTE(boris-42): This method is generic UC dropper.
            utils.drop_unique_constraint(engine, table_name, uc_name, 'foo')

            s = test_table.select().order_by(test_table.c.id)
            rows = engine.execute(s).fetchall()

            for i in moves.range(len(values)):
                v = values[i]
                self.assertEqual((v['id'], v['a'], v['foo']), rows[i])

            # NOTE(boris-42): Update data about Table from DB.
            meta = MetaData()
            meta.bind = engine
            test_table = Table(table_name, meta, autoload=True)
            constraints = [c for c in test_table.constraints
                           if c.name == uc_name]
            self.assertEqual(len(constraints), 0)
            self.assertEqual(len(test_table.constraints), 1)

            test_table.drop()
Example #7
0
    def test_drop_unique_constraint_in_sqlite_fk_recreate(self):
        parent_table = Table("table0", self.meta, Column("id", Integer, primary_key=True), Column("foo", Integer))
        parent_table.create()
        table_name = "table1"
        table = Table(
            table_name,
            self.meta,
            Column("id", Integer, primary_key=True),
            Column("baz", Integer),
            Column("bar", Integer, ForeignKey("table0.id")),
            UniqueConstraint("baz", name="constr1"),
        )
        table.create()
        utils.drop_unique_constraint(self.engine, table_name, "constr1", "baz")

        insp = reflection.Inspector.from_engine(self.engine)
        f_keys = insp.get_foreign_keys(table_name)
        self.assertEqual(len(f_keys), 1)
        f_key = f_keys[0]
        self.assertEqual(f_key["referred_table"], "table0")
        self.assertEqual(f_key["referred_columns"], ["id"])
        self.assertEqual(f_key["constrained_columns"], ["bar"])
Example #8
0
    def test_util_drop_unique_constraint_with_not_supported_sqlite_type(self):
        table_name = "__test_tmp_table__"
        uc_name = 'uniq_foo'
        values = [{
            'id': 1,
            'a': 3,
            'foo': 10
        }, {
            'id': 2,
            'a': 2,
            'foo': 20
        }, {
            'id': 3,
            'a': 1,
            'foo': 30
        }]

        test_table = Table(
            table_name,
            self.meta,
            Column('id', Integer, primary_key=True, nullable=False),
            Column('a', Integer),
            Column('foo', CustomType, default=0),
            UniqueConstraint('a', name='uniq_a'),
            UniqueConstraint('foo', name=uc_name),
        )
        test_table.create()

        self.engine.execute(test_table.insert(), values)
        warnings.simplefilter("ignore", SAWarning)

        # reflection of custom types has been fixed upstream
        if SA_VERSION < (0, 9, 0):
            # NOTE(boris-42): Missing info about column `foo` that has
            #                 unsupported type CustomType.
            self.assertRaises(exception.ColumnError,
                              utils.drop_unique_constraint, self.engine,
                              table_name, uc_name, 'foo')

            # NOTE(boris-42): Wrong type of foo instance. it should be
            #                 instance of sqlalchemy.Column.
            self.assertRaises(exception.ColumnError,
                              utils.drop_unique_constraint,
                              self.engine,
                              table_name,
                              uc_name,
                              'foo',
                              foo=Integer())

        foo = Column('foo', CustomType, default=0)
        utils.drop_unique_constraint(self.engine,
                                     table_name,
                                     uc_name,
                                     'foo',
                                     foo=foo)

        s = test_table.select().order_by(test_table.c.id)
        rows = self.engine.execute(s).fetchall()

        for i in moves.range(len(values)):
            v = values[i]
            self.assertEqual((v['id'], v['a'], v['foo']), rows[i])

        # NOTE(boris-42): Update data about Table from DB.
        meta = MetaData(bind=self.engine)
        test_table = Table(table_name, meta, autoload=True)
        constraints = [c for c in test_table.constraints if c.name == uc_name]
        self.assertEqual(len(constraints), 0)
        self.assertEqual(len(test_table.constraints), 1)
        test_table.drop()