Skip to content

pg_restore Callback Tasks

Writing callbacks in Shell

PG restore callbacks can also be written as shell scripts (Jinja2 bash templates), without deploying a Python addon. See Shell Restore Callbacks.

What they are

A pg_restore Callback Task hooks into the restore process. Muppy calls it at each step, so you can act between them.

Typical uses:

  • adjust a user's attributes right after it is created
  • alter the schema after the database is created and before the data goes in
  • anonymise a database immediately after restoring it

A callback is an ordinary Muppy Task with a specific signature. Its step parameter tells it which step just finished: s3_download, create_user, drop_db, create_db or pg_restore.

Template

@fabric_task()
def template_pg_restore_callback(
        cnx, host_obj,
        step:str,
        pg_cluster_obj:OdooModelType,
        pg_dump_obj:OdooModelType=None,
        pg_dump_file_path:str=None,
        db_name:str=None,
        db_comment:str=None,
        db_owner:str=None,
        _imq_logger=None
    ):
    """ Template pg_restore callback 
    :param step: The pg_restore step whose end triggered this task:
       any of 's3_download', 'create_user', 'drop_db', 'create_db', 'pg_restore'.
    :param pg_cluster_obj: Cluster on which the dump will be restored.
    :param pg_dump_obj: The Muppy pg_dump object that will be restored.
    :param pg_dump_file_path: Full path of the pg_dump that will be restored.
    :param db_name: Name of the database to restore.
    :param db_comment: Comment that will be set at restore.
    :param db_owner: User owner of the database.

    :returns: something or False
    """
    odoo_env = host_obj.env
    _task_logger = _imq_logger or _logger
    # ...

The task is called once per step. Branch on step and return early for the steps you don't care about.

Where a callback is set

A callback can be attached at four levels. Each one is a default for the next:

Set on Effect
PostgreSQL Database Default proposed in the Backup Databases wizard.
Backup Databases wizard Stored on the resulting pg_dump, and proposed as the default when that dump is restored.
Restore PostgreSQL Databases wizard Overrides the dump's callback for this restore. Leave it empty to use the dump's.
Copy Databases (using pg_dump) wizard The callback used for the restore half of the copy.