tests: Rework BannedDBSchemaOperations fixture

Two issues here. The most pressing is that we are banning use of the
'batch_alter_table' operation. There's no reason to do this as it isn't
an operation per se, but rather a way of modifying how operations run.

Fixing this highlighted a weird error case, whereby the error message
we'd see when we called a banned operation would indicate that we had
called a different operation from the one we were *actually* calling.
This made fixing the issue far more time consuming than it should have
been (I thought it was doing something else entirely!). Turns out this
is due to late binding [1]. The fixture is rewritten to avoid the issue,
simplifying it significantly in the process.

[1] https://stackoverflow.com/a/3431699/613428

Change-Id: Ib3f9099160265c4eafea1b2e38537c58eadf9a5c
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2023-02-28 17:10:47 +00:00
parent 8c9462f6fa
commit 5e9f32469e
1 changed files with 8 additions and 28 deletions

View File

@ -60,38 +60,19 @@ class BannedDBSchemaOperations(fixtures.Fixture):
@staticmethod
def _explode(op, revision):
msg = "Operation '%s' is not allowed in migration %s"
raise DBOperationNotAllowed(msg % (op, revision))
def fail(*a, **kw):
msg = "Operation '%s' is not allowed in migration %s"
raise DBOperationNotAllowed(msg % (op, revision))
return fail
def setUp(self):
super().setUp()
explode_lambda = {
x: lambda *a, **k: self._explode(x, self._revision)
for x in [
'add_column',
'alter_column',
'batch_alter_table',
'bulk_insert',
'create_check_constraint',
'create_exclude_constraint',
'create_foreign_key',
'create_index',
'create_primary_key',
'create_table',
'create_table_comment',
'create_unique_constraint',
'drop_column',
'drop_constraint',
'drop_index',
'drop_table',
'drop_table_comment',
# 'execute',
'rename_table',
]
}
for op in self._banned_ops:
self.useFixture(
fixtures.MonkeyPatch('alembic.op.%s' % op, explode_lambda[op])
fixtures.MonkeyPatch(
'alembic.op.%s' % op, self._explode(op, self._revision),
)
)
@ -105,7 +86,6 @@ class KeystoneMigrationsWalk(
BANNED_OPS = {
'expand': [
'alter_column',
'batch_alter_table',
'drop_column',
'drop_constraint',
'drop_index',