# File lib/rubyrep/replication_extenders/mysql_replication.rb, line 60
      def create_replication_trigger(params)
        create_or_replace_replication_trigger_function params

        %w(insert update delete).each do |action|
          execute("DROP TRIGGER IF EXISTS `\#{params[:trigger_name]}_\#{action}`;\n")

          # The created triggers can handle the case where the trigger procedure
          # is updated (that is: temporarily deleted and recreated) while the
          # trigger is running.
          # For that an MySQL internal exception is raised if the trigger
          # procedure cannot be found. The exception is caught by an trigger
          # internal handler. 
          # The handler causes the trigger to retry calling the
          # trigger procedure several times with short breaks in between.

          trigger_var = action == 'delete' ? 'OLD' : 'NEW'
          if action == 'update'
            call_statement = "CALL `#{params[:trigger_name]}`(#{key_clause('OLD', params)}, #{key_clause('NEW', params)}, '#{action[0,1].upcase}');"
          else
            call_statement = "CALL `#{params[:trigger_name]}`(#{key_clause(trigger_var, params)}, null, '#{action[0,1].upcase}');"
          end
          execute("CREATE TRIGGER `\#{params[:trigger_name]}_\#{action}`\nAFTER \#{action} ON `\#{params[:table]}` FOR EACH ROW BEGIN\nDECLARE number_attempts INT DEFAULT 0;\nDECLARE failed INT;\nDECLARE CONTINUE HANDLER FOR 1305 BEGIN\nDO SLEEP(0.05);\nSET failed = 1;\nSET number_attempts = number_attempts + 1;\nEND;\nREPEAT\nSET failed = 0;\n\#{call_statement}\nUNTIL failed = 0 OR number_attempts >= 40 END REPEAT;\nEND;\n")
        end

      end