Merge "Fix usage of sqlalchemy.engine.Connection.execute"

This commit is contained in:
Zuul 2024-03-07 16:39:31 +00:00 committed by Gerrit Code Review
commit 6a2219a4eb
3 changed files with 12 additions and 11 deletions

View File

@ -48,9 +48,9 @@ def upgrade():
if op.get_bind().engine.name != 'sqlite':
connection = op.get_bind()
host_query = connection.execute("""
host_query = connection.execute(sa.text("""
SELECT DISTINCT "physical:host", capability_name
FROM computehost_extra_capabilities;""")
FROM computehost_extra_capabilities;"""))
capability_values = [
(str(uuid.uuid4()), resource_type, capability_name)
@ -62,18 +62,18 @@ def upgrade():
INSERT INTO resource_properties
(id, resource_type, property_name)
VALUES {};"""
connection.execute(
insert.format(', '.join(map(str, capability_values))))
connection.execute(sa.text(
insert.format(', '.join(map(str, capability_values)))))
op.add_column('computehost_extra_capabilities',
sa.Column('property_id', sa.String(length=255),
nullable=False))
connection.execute("""
connection.execute(sa.text("""
UPDATE computehost_extra_capabilities c
LEFT JOIN resource_properties e
ON e.property_name = c.capability_name
SET c.property_id = e.id;""")
SET c.property_id = e.id;"""))
op.create_foreign_key('computehost_resource_property_id_fk',
'computehost_extra_capabilities',
@ -89,11 +89,11 @@ def downgrade():
if op.get_bind().engine.name != 'sqlite':
connection = op.get_bind()
connection.execute("""
connection.execute(sa.text("""
UPDATE computehost_extra_capabilities c
LEFT JOIN resource_properties e
ON e.id=c.property_id
SET c.capability_name = e.property_name;""")
SET c.capability_name = e.property_name;"""))
op.drop_constraint('computehost_resource_property_id_fk',
'computehost_extra_capabilities',
type_='foreignkey')

View File

@ -39,7 +39,7 @@ def upgrade():
# I need to do it in this way because Postgress fails
# if I use SQLAlchemy
connection = op.get_bind()
connection.execute("UPDATE computehosts SET trust_id = ''")
connection.execute(sa.text("UPDATE computehosts SET trust_id = ''"))
op.alter_column('computehosts', 'trust_id',
existing_type=sa.String(length=36), nullable=False)

View File

@ -427,10 +427,11 @@ class BaseWalkMigrationTestCase(BaseMigrationTestCase):
connection = engine.connect()
# sanity check
total = connection.execute("SELECT count(*) "
total = connection.execute(sqlalchemy.text(
"SELECT count(*) "
"from information_schema.TABLES "
"where TABLE_SCHEMA='%(database)s'" %
{'database': database})
{'database': database}))
self.assertTrue(total.scalar() > 0, "No tables found. Wrong schema?")
connection.close()