fix(proxy): new sqla interface for binding params to statements

This commit is contained in:
Iain Learmonth 2023-03-13 13:46:38 +00:00
parent b1f874eaf3
commit 358e945e34

View file

@ -144,13 +144,15 @@ class ProxyAutomation(TerraformAutomation):
@classmethod
def get_subgroups(cls) -> Dict[int, Dict[int, int]]:
conn = db.engine.connect()
result = conn.execute(text("""
stmt = text("""
SELECT origin.group_id, proxy.psg, COUNT(proxy.id) FROM proxy, origin
WHERE proxy.origin_id = origin.id
AND proxy.destroyed IS NULL
AND proxy.provider = :provider
GROUP BY origin.group_id, proxy.psg;
"""), provider=cls.provider)
""")
stmt = stmt.bindparams(provider=cls.provider)
result = conn.execute(stmt).all()
subgroups: Dict[int, Dict[int, int]] = defaultdict(lambda: defaultdict(lambda: 0))
for row in result:
subgroups[row[0]][row[1]] = row[2]
@ -160,14 +162,16 @@ class ProxyAutomation(TerraformAutomation):
def next_subgroup(cls, group_id: int) -> Optional[int]:
provider = cls() # Some attributes are set by constructor
conn = db.engine.connect()
result = conn.execute(text("""
stmt = text("""
SELECT proxy.psg, COUNT(proxy.id) FROM proxy, origin
WHERE proxy.origin_id = origin.id
AND proxy.destroyed IS NULL
AND origin.group_id = :group_id
AND proxy.provider = :provider
GROUP BY proxy.psg ORDER BY proxy.psg;
"""), provider=provider.short_name, group_id=group_id)
""")
stmt = stmt.bindparams(provider=provider.short_name, group_id=group_id)
result = conn.execute(stmt).all()
subgroups = {
row[0]: row[1] for row in result
}