biweeklybudget.db module

biweeklybudget.db._alembic_get_current_rev(config, script)[source]

Works sorta like alembic.command.current

Parameters:config – alembic Config
Returns:current revision
Return type:str
biweeklybudget.db.cleanup_db()[source]

This must be called from all scripts, using

atexit.register(cleanup_db)
biweeklybudget.db.db_session = <sqlalchemy.orm.scoping.scoped_session object>

sqlalchemy.orm.scoping.scoped_session session

biweeklybudget.db.engine = Engine(sqlite:///:memory:)

The database engine object; return value of sqlalchemy.create_engine().

biweeklybudget.db.init_db()[source]

Initialize the database; call sqlalchemy.schema.MetaData.create_all() on the metadata object.

biweeklybudget.db.upsert_record(model_class, key_fields, **kwargs)[source]

Upsert a record in the database.

key_fields is either a string primary key field name (a key in the kwargs dict) or a list or tuple of string primary key field names, for compound keys.

If a record can be found matching these keys, it will be updated and committed. If not, a new one will be inserted. Either way, the record is returned.

sqlalchemy.orm.session.Session.commit() is NOT called.

Parameters:
  • model_class (biweeklybudget.models.base.ModelAsDict) – the class of model to insert/update
  • key_fields – The field name(s) (keys in kwargs) that make up the primary key. This can be a single string, or a list or tuple of strings for compound keys. The values for these key fields MUST be included in kwargs.
  • kwargs (dict) – arguments to provide to the model class constructor, or to update if there is an existing record matching the key.
Returns:

inserted or updated record; type is an instance of model_class