You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: 20171117171433_adjust_project_transitions_rd_events_to_use_subscription.rb
    path of file: ./repos/catarse/db/migrate
    
    the code of the file until where you have to start completion: class AdjustProjectTransitionsRdEventsToUseSubscription < ActiveRecord::Migration
  def up
    execute %Q{
CREATE OR REPLACE FUNCTION public.project_rdevents_dispatcher()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
        DECLARE
            v_enabled_events text[];
            v_project public.projects;
            v_mode_concat text;
        BEGIN
            v_enabled_events =  ARRAY['in_analysis', 'approved', 'online',  'waiting_funds', 'successful', 'failed'];

            SELECT * FROM projects WHERE id = NEW.project_id
                INTO v_project;

            v_mode_concat = (CASE WHEN v_project.mode = 'flex' THEN 'flex_project_' 
                                  WHEN v_project.mode = 'sub' THEN 'sub_project_' 
                                  ELSE 'project_' END);

            IF NEW.to_state = ANY(v_enabled_events) THEN
                INSERT INTO public.rdevents (user_id, project_id, event_name, created_at)
                    VALUES (v_project.user_id, NEW.project_id, v_mode_concat||NEW.to_state, now());
     
    
  def down
    execute %Q{
CREATE OR REPLACE FUNCTION public.project_rdevents_dispatcher()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
        DECLARE
            v_enabled_events text[];
            v_project public.projects;
            v_mode_concat text;
        BEGIN
            v_enabled_events =  ARRAY['in_analysis', 'approved', 'online',  'waiting_funds', 'successful', 'failed'];

            SELECT * FROM projects WHERE id = NEW.project_id
                INTO v_project;

            v_mode_concat = (CASE WHEN v_project.mode = 'flex' THEN 'flex_project_' ELSE 'project_' END);

            IF NEW.to_state = ANY(v_enabled_events) THEN
                INSERT INTO public.rdevents (user_id, project_id, event_name, created_at)
                    VALUES (v_project.user_id, NEW.project_id, v_mode_concat||NEW.to_state, now());
            END IF;

            RETURN NULL;
        END;
    $function$;

CREATE OR REPLACE FUNCTION public.project_received_conversion()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
        BEGIN
            INSERT INTO public.rdevents (user_id, project_id, event_name)
                VALUES (NEW.user_id, NEW.id, 'project_draft');
            RETURN NULL;
        END;
    $function$
;
}
  end