utils.drop_unique_constraint and associated tests totally broken
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
oslo.db |
Fix Released
|
Medium
|
Mike Bayer |
Bug Description
the logic surrounding drop_unique_
the key to the failure is that both the function and the tests assume that autoload=True on a Table reflects UniqueConstraint objects. Unfortunately, this is not the case for SQLAlchemy 0.9.x and earlier, and both the function, and the tests, fail because of this. As this feature has been added in 1.0, the tests again fail on 1.0, but the feature at least seems to work.
The failure on previous versions is that *all* unique constraint objects are dropped against the SQLite backend. Therefore, this method is completely dangerous and should probably be removed entirely, as it is already marked as deprecated.
With SQLAlchemy 0.9.x or earler, there will be *no* UniqueConstraint objects under any circumstances in table.constraints with this code:
meta = MetaData(
test_table = Table(table_name, meta, autoload=True)
There will however be a PrimaryKeyConst
meta = MetaData(
test_table = Table(table_name, meta, autoload=True)
constraints = [c for c in test_table.
self.
self.
The "1" there is simply the PrimaryKeyConst
With SQLAlchemy master, the length of test_table.
The drop_unique_
t = Table(table_name, meta, autoload=True)
uc = UniqueConstrain
uc.drop()
the above Table has *no* unique constraints in it. Migrate's approach of rebuilding the whole table fails because the necessary UniqueConstraint that *isn't* dropped is non-present.
I'm assuming that on any other backend, Migrate is not recreating the entire table so this issue doesn't occur.
The correct way to test for the number of unique constraints regardless of SQLAlchemy version is to use the inspector directly (note that it caches results, so when schema changes are made, a new inspector must be built):
from sqlalchemy import inspect
insp = inspect(
constraints = insp.get_
to fix this for SQLite, we'd have to manually add UniqueConstraint objects to the reflected table after using inspector. get_unique_ constraints( ), that is, backport SQLAlchemy 1.0's feature. I'd rather whack this method entirely, so putting it out there to estimate the impact of either approach.