~olly/yoyo#92: 
post apply hook not called on rollback

On rollback the 'post-apply.py' is not called, so rollback can't rely on it to recreate VIEWs for example.

May be the documentation should explain that, or a config or something to enable it.

Workaround, add it manually a the end of rollback step:

WARNING: rollback steps are played in reverse order, so the 'end of the rollback' is the first defined step.

A sample of code (See #91 for the whole generated multi-step code)

def recreate_all_views_step_07(conn):
    cursor = conn.cursor()
    sql_queries = ["""SQL CREATE VIEW goes HERE""" ]
    for q in sql_queries:
        cursor.execute(q)

# steps SQL code is ommited apply_steps{} and rollback_steps{} dict of SQL
# forward step is relying on post-apply.py so no step for recreating all views
steps = [
	step(apply_steps['drop_all_views_00_sql0'],              recreate_all_views_step_07),
	step(apply_steps['drop_all_views_00_sql1'],              rollback_steps['drop_table_06_sql0']),
	step(apply_steps['drop_all_views_00_sql2'],              rollback_steps['rename_table_05_sql0']),
	step(apply_steps['create_backup_table_01_sql0'],         rollback_steps['drop_table_04_sql0']),
	step(apply_steps['ensure_drop_table_if_exists_02_sql0'], rollback_steps['copy_table_03_sql0']),
	step(apply_steps['create_table_03_sql0'],                rollback_steps['create_table_02_sql0']),
	step(apply_steps['remove_duplicate_04_sql0'],            rollback_steps['ensure_drop_table_if_exists_01_sql0']),
	step(apply_steps['copy_table_05_sql0'],                  rollback_steps['drop_all_views_00_sql2']),
	step(apply_steps['drop_table_06_sql0'],                  rollback_steps['drop_all_views_00_sql1']),
	step(apply_steps['rename_table_07_sql0'],                rollback_steps['drop_all_views_00_sql0']),
]

my sample post-apply.py hook for completeness and understanding

"""
recreate all views from sql/views/*,sql folder
"""

import os
import sys
sys.path.append('.')

from dump_sql_views import get_sql_views_from_file

def create_all_views(conn):
    cursor = conn.cursor()
    for create_view in get_sql_views_from_file(if_not_exists=True).values():
       cursor.execute(create_view)

from yoyo import step

__depends__ = {}

steps = [
    step(create_all_views),
]
Status
REPORTED
Submitter
~sylvain
Assigned to
No-one
Submitted
1 year, 6 months ago
Updated
1 year, 6 months ago
Labels
No labels applied.