|
| 1 | +"""Create chatbot triggers |
| 2 | +
|
| 3 | +Revision ID: e2a71c5767b1 |
| 4 | +Revises: 2e8dc6b4f10c |
| 5 | +Create Date: 2025-06-15 22:26:27.946492 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import sqlalchemy as sa |
| 10 | +from alembic_utils.pg_extension import PGExtension |
| 11 | +from alembic_utils.pg_function import PGFunction |
| 12 | +from alembic_utils.pg_trigger import PGTrigger |
| 13 | +from sqlalchemy import text as sql_text |
| 14 | +from sqlalchemy.dialects import postgresql |
| 15 | + |
| 16 | +from alembic import op |
| 17 | + |
| 18 | +# revision identifiers, used by Alembic. |
| 19 | +revision = "e2a71c5767b1" |
| 20 | +down_revision = "2e8dc6b4f10c" |
| 21 | +branch_labels = None |
| 22 | +depends_on = None |
| 23 | + |
| 24 | + |
| 25 | +def upgrade(): |
| 26 | + # ### commands auto generated by Alembic - please adjust! ### |
| 27 | + public_notify_flow_event = PGFunction( |
| 28 | + schema="public", |
| 29 | + signature="notify_flow_event()", |
| 30 | + definition="returns trigger LANGUAGE plpgsql\n AS $function$\n BEGIN\n -- Notify on session state changes with comprehensive event data\n IF TG_OP = 'INSERT' THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', 'session_started',\n 'session_id', NEW.id,\n 'flow_id', NEW.flow_id,\n 'user_id', NEW.user_id,\n 'current_node', NEW.current_node_id,\n 'status', NEW.status,\n 'revision', NEW.revision,\n 'timestamp', extract(epoch from NEW.created_at)\n )::text\n );\n RETURN NEW;\n ELSIF TG_OP = 'UPDATE' THEN\n -- Only notify on significant state changes\n IF OLD.current_node_id != NEW.current_node_id \n OR OLD.status != NEW.status \n OR OLD.revision != NEW.revision THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', CASE \n WHEN OLD.status != NEW.status THEN 'session_status_changed'\n WHEN OLD.current_node_id != NEW.current_node_id THEN 'node_changed'\n ELSE 'session_updated'\n END,\n 'session_id', NEW.id,\n 'flow_id', NEW.flow_id,\n 'user_id', NEW.user_id,\n 'current_node', NEW.current_node_id,\n 'previous_node', OLD.current_node_id,\n 'status', NEW.status,\n 'previous_status', OLD.status,\n 'revision', NEW.revision,\n 'previous_revision', OLD.revision,\n 'timestamp', extract(epoch from NEW.updated_at)\n )::text\n );\n END IF;\n RETURN NEW;\n ELSIF TG_OP = 'DELETE' THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', 'session_deleted',\n 'session_id', OLD.id,\n 'flow_id', OLD.flow_id,\n 'user_id', OLD.user_id,\n 'timestamp', extract(epoch from NOW())\n )::text\n );\n RETURN OLD;\n END IF;\n RETURN NULL;\n END;\n $function$", |
| 31 | + ) |
| 32 | + op.create_entity(public_notify_flow_event) |
| 33 | + |
| 34 | + public_conversation_sessions_conversation_sessions_notify_flow_event_trigger = PGTrigger( |
| 35 | + schema="public", |
| 36 | + signature="conversation_sessions_notify_flow_event_trigger", |
| 37 | + on_entity="public.conversation_sessions", |
| 38 | + is_constraint=False, |
| 39 | + definition="AFTER INSERT OR UPDATE OR DELETE ON public.conversation_sessions \n FOR EACH ROW EXECUTE FUNCTION notify_flow_event()", |
| 40 | + ) |
| 41 | + op.create_entity( |
| 42 | + public_conversation_sessions_conversation_sessions_notify_flow_event_trigger |
| 43 | + ) |
| 44 | + |
| 45 | + public_collection_items_update_collections_trigger = PGTrigger( |
| 46 | + schema="public", |
| 47 | + signature="update_collections_trigger", |
| 48 | + on_entity="public.collection_items", |
| 49 | + is_constraint=False, |
| 50 | + definition="AFTER INSERT OR UPDATE ON public.collection_items FOR EACH ROW EXECUTE FUNCTION update_collections_function()", |
| 51 | + ) |
| 52 | + op.drop_entity(public_collection_items_update_collections_trigger) |
| 53 | + |
| 54 | + # ### end Alembic commands ### |
| 55 | + |
| 56 | + |
| 57 | +def downgrade(): |
| 58 | + # ### commands auto generated by Alembic - please adjust! ### |
| 59 | + |
| 60 | + public_collection_items_update_collections_trigger = PGTrigger( |
| 61 | + schema="public", |
| 62 | + signature="update_collections_trigger", |
| 63 | + on_entity="public.collection_items", |
| 64 | + is_constraint=False, |
| 65 | + definition="AFTER INSERT OR UPDATE ON public.collection_items FOR EACH ROW EXECUTE FUNCTION update_collections_function()", |
| 66 | + ) |
| 67 | + op.create_entity(public_collection_items_update_collections_trigger) |
| 68 | + |
| 69 | + public_conversation_sessions_conversation_sessions_notify_flow_event_trigger = PGTrigger( |
| 70 | + schema="public", |
| 71 | + signature="conversation_sessions_notify_flow_event_trigger", |
| 72 | + on_entity="public.conversation_sessions", |
| 73 | + is_constraint=False, |
| 74 | + definition="AFTER INSERT OR UPDATE OR DELETE ON public.conversation_sessions \n FOR EACH ROW EXECUTE FUNCTION notify_flow_event()", |
| 75 | + ) |
| 76 | + op.drop_entity( |
| 77 | + public_conversation_sessions_conversation_sessions_notify_flow_event_trigger |
| 78 | + ) |
| 79 | + |
| 80 | + public_notify_flow_event = PGFunction( |
| 81 | + schema="public", |
| 82 | + signature="notify_flow_event()", |
| 83 | + definition="returns trigger LANGUAGE plpgsql\n AS $function$\n BEGIN\n -- Notify on session state changes with comprehensive event data\n IF TG_OP = 'INSERT' THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', 'session_started',\n 'session_id', NEW.id,\n 'flow_id', NEW.flow_id,\n 'user_id', NEW.user_id,\n 'current_node', NEW.current_node_id,\n 'status', NEW.status,\n 'revision', NEW.revision,\n 'timestamp', extract(epoch from NEW.created_at)\n )::text\n );\n RETURN NEW;\n ELSIF TG_OP = 'UPDATE' THEN\n -- Only notify on significant state changes\n IF OLD.current_node_id != NEW.current_node_id \n OR OLD.status != NEW.status \n OR OLD.revision != NEW.revision THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', CASE \n WHEN OLD.status != NEW.status THEN 'session_status_changed'\n WHEN OLD.current_node_id != NEW.current_node_id THEN 'node_changed'\n ELSE 'session_updated'\n END,\n 'session_id', NEW.id,\n 'flow_id', NEW.flow_id,\n 'user_id', NEW.user_id,\n 'current_node', NEW.current_node_id,\n 'previous_node', OLD.current_node_id,\n 'status', NEW.status,\n 'previous_status', OLD.status,\n 'revision', NEW.revision,\n 'previous_revision', OLD.revision,\n 'timestamp', extract(epoch from NEW.updated_at)\n )::text\n );\n END IF;\n RETURN NEW;\n ELSIF TG_OP = 'DELETE' THEN\n PERFORM pg_notify(\n 'flow_events',\n json_build_object(\n 'event_type', 'session_deleted',\n 'session_id', OLD.id,\n 'flow_id', OLD.flow_id,\n 'user_id', OLD.user_id,\n 'timestamp', extract(epoch from NOW())\n )::text\n );\n RETURN OLD;\n END IF;\n RETURN NULL;\n END;\n $function$", |
| 84 | + ) |
| 85 | + op.drop_entity(public_notify_flow_event) |
| 86 | + |
| 87 | + # ### end Alembic commands ### |
0 commit comments