start implementing column parity
This commit is contained in:
parent
941ebc4a24
commit
bb19158aa0
17
database.py
17
database.py
@ -51,6 +51,23 @@ class DBConnector:
|
|||||||
def __exit__(self):
|
def __exit__(self):
|
||||||
self._db_stop()
|
self._db_stop()
|
||||||
|
|
||||||
|
def _get_cols(self) -> set[str]:
|
||||||
|
"""Get the list of columns in the database.
|
||||||
|
|
||||||
|
:return: A list of column names."""
|
||||||
|
query = f"select COLUMN_NAME from information_schema.columns where table_name={DB_TABLE}"
|
||||||
|
rows = {x["COLUMN_NAME"] for x in self._query(query)}
|
||||||
|
return rows
|
||||||
|
|
||||||
|
def _column_parity(self, columns: list[str] | set[str]) -> set[str]:
|
||||||
|
"""If the listed columns are not in the database, add them."""
|
||||||
|
cols = set(columns)
|
||||||
|
existing = self._get_cols()
|
||||||
|
needs = cols.difference(existing.intersection(cols))
|
||||||
|
query = f"ALTER TABLE {DB_TABLE} {', '.join([f'ADD COLUMN {c}' for c in needs])}"
|
||||||
|
self._query(query)
|
||||||
|
return self._get_cols()
|
||||||
|
|
||||||
def _query(self, sql) -> list[dict]:
|
def _query(self, sql) -> list[dict]:
|
||||||
"""Basic function for running queries.
|
"""Basic function for running queries.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user