1.2.1.1. manager

This module provides the functionality to manage the server application’s database connection.

1.2.1.1.1. Functions

clear_database()[source]

Delete all data from all tables in the connected database. The database schema will remain unaffected.

Warning

This action can not be reversed and there is no confirmation before it takes place.

export_database(target_file)[source]

Export the contents of the database using SQLAlchemy’s serialization. This creates an archive file containing all of the tables and their data. The resulting export can be imported into another supported database so long as the SCHEMA_VERSION is the same.

Parameters:target_file (str) – The file to write the export to.
import_database(target_file, clear=True)[source]

Import the contents of a serialized database from an archive previously created with the export_database() function. The current SCHEMA_VERSION must be the same as the exported archive.

Warning

This will by default delete the contents of the current database in accordance with the clear parameter. If clear is not specified and objects in the database and import share an ID, they will be merged.

Parameters:
  • target_file (str) – The database archive file to import from.
  • clear (bool) – Whether or not to delete the contents of the existing database before importing the new data.
normalize_connection_url(connection_url)[source]

Normalize a connection url by performing any conversions necessary for it to be used with the database API.

Parameters:connection_url (str) – The connection url to normalize.
Returns:The normalized connection url.
Return type:str
get_metadata(key, session=None)[source]

Store a piece of metadata regarding the King Phisher database.

Parameters:
  • key (str) – The name of the data.
  • value (int, str) – The value to store.
  • session – The session to use to store the value.
get_row_by_id(session, table, row_id)[source]

Retrieve a database row from the specified table by it’s unique id.

Parameters:
  • session (.Session) – The database session to use for the query.
  • table – The table object or the name of the database table where the row resides.
  • row_id – The id of the row to retrieve.
Returns:

The object representing the specified row or None if it does not exist.

init_alembic(engine, schema_version)[source]

Creates the alembic_version table and sets the value of the table according to the specified schema version.

Parameters:
  • engine (sqlalchemy.engine.Engine) – The engine used to connect to the database.
  • schema_version (int) – The MetaData schema_version to set the alembic version to.
init_database(connection_url, extra_init=False)[source]

Create and initialize the database engine. This must be done before the session object can be used. This will also attempt to perform any updates to the database schema if the backend supports such operations.

Parameters:
  • connection_url (str) – The url for the database connection.
  • extra_init (bool) – Run optional extra dbms-specific initialization logic.
Returns:

The initialized database engine.

init_database_postgresql(connection_url)[source]

Perform additional initialization checks and operations for a PostgreSQL database. If the database is hosted locally this will ensure that the service is currently running and start it if it is not. Additionally if the specified database or user do not exist, they will be created.

Parameters:connection_url (sqlalchemy.engine.url.URL) – The url for the PostgreSQL database connection.
Returns:The initialized database engine.
set_metadata(key, value, session=None)[source]

Store a piece of metadata regarding the King Phisher database.

Parameters:
  • key (str) – The name of the data.
  • value (int, str) – The value to store.
  • session – The session to use to store the value.