pycram.orm.utils

Module Contents

Functions

write_database_to_file(in_sessionmaker, filename[, ...])

Writes all Tables stored within the given session into a local file. File will be written in JSON Format

print_database(in_sessionmaker)

Prints all ORM Class data within the given Session.

update_primary_key(source_session_maker, ...)

Updates all the primary keys of the database associated with the destination engine, so that there will be no

copy_database(source_session_maker, ...)

Iterates through all tables within tht source database and merges them into the destination database. Careful

update_primary_key_constrains(session_maker)

Iterates through all tables related to any ORM Class and sets in their corresponding foreign keys in the given

migrate_neems(source_session_maker, ...)

Merges the database connected to the source session maker into the database connected to the destination session

pycram.orm.utils.write_database_to_file(in_sessionmaker: sqlalchemy.orm.sessionmaker, filename: str, b_write_to_console: bool = False)

Writes all Tables stored within the given session into a local file. File will be written in JSON Format

Parameters:
  • in_sessionmaker – sessionmaker that allows us to access the Database

  • filename – Filename of the logfile

  • b_write_to_console – enables writing to the console. Default false

pycram.orm.utils.print_database(in_sessionmaker: sqlalchemy.orm.sessionmaker)

Prints all ORM Class data within the given Session.

Parameters:

in_sessionmaker – Database Session which should be printed

pycram.orm.utils.update_primary_key(source_session_maker: sqlalchemy.orm.sessionmaker, destination_session_maker: sqlalchemy.orm.sessionmaker)

Updates all the primary keys of the database associated with the destination engine, so that there will be no problems when merging it into the source database. In order to achieve this the highest id value of the source engine is searched and the primary keys of the destination database will get all the values following that. Cascading triggers in the database will take care of the rest. Careful 2023 this will not work in memory databases as there are no triggers.

Parameters:
  • source_session_maker – Session maker of the source data_base

  • destination_session_maker – Session maker of the destination data_base

pycram.orm.utils.copy_database(source_session_maker: sqlalchemy.orm.sessionmaker, destination_session_maker: sqlalchemy.orm.sessionmaker)

Iterates through all tables within tht source database and merges them into the destination database. Careful this function does not check if there are any primary key collisions or updates any data.

Note

Ignores all previously detached data, could result in loss of information. During testing database objects sometimes had a detached twin. As a possible feature in the future it maybe useful to give the user an opportunity to decide what happens with the detached objects. Careful this could lead to duplicated data in the destination database.

Parameters:
  • source_session_maker – Sessionmaker of the source database

  • destination_session_maker – Sessionmaker of the destination database

pycram.orm.utils.update_primary_key_constrains(session_maker: sqlalchemy.orm.sessionmaker)

Iterates through all tables related to any ORM Class and sets in their corresponding foreign keys in the given endpoint to “ON UPDATE CASCADING”.

Note

Careful currently only works on postgres databases.

Parameters:

session_maker

Returns:

empty

pycram.orm.utils.migrate_neems(source_session_maker: sqlalchemy.orm.sessionmaker, destination_session_maker: sqlalchemy.orm.sessionmaker)

Merges the database connected to the source session maker into the database connected to the destination session maker. Will first update the primary constrains inside the destination database (if needed). Afterwards updates the primary keys within the destination database (as there are cascading updates now) and then merges the source database into the destination.

Note

Assumes the destination database is a postgres database

Parameters:
  • source_session_maker – Sessionmaker of the source database

  • destination_session_maker – Sessionmaker of the destination database