db.py - Database configuration
Set up database configuration in this file
Imports
These are listed in the order prescribed by PEP 8.
Standard library
None.
Third-party imports
Use asyncio for SQLAlchemy – see SQLAlchemy Asynchronous I/O (asyncio).
Local application imports
The polling in test_runestone_components.py - Tests of the Runestone Components produces a HUGE amount of output when echo is true.
This creates the SessionLocal class. An actual session is an instance of this class.
This creates the base class we will use to create models
Never, ever drop tables in a production environment!
Look for any records that violate non-null constraints. TODO: when/where should we call this? Or should it be removed?
SQLAlchemy requires ==
to correctly create the query; it can’t overload the is
operator.
query = select(table).where(column == None) # noqa: E711.
res = (await session.execute(query)).fetchall()
if res:
not_null_count += 1
rslogger.error(
f"Column {table_name}.{column.key} has {len(res)} NULL records, such as:"
)
for row in res[0:9]:
def shorten(s):
s = str(s)
return s if len(s) < 20 else s[0:20] + "..."
The result isn’t an ORM object, so use this to display it.
If the engine isn’t disposed of, then a PostgreSQL database will remain in a pseudo-locked state, refusing to drop or truncate tables (see bookserver_session).
Dependency