docs: def assign_moduleid_default_trigger(plpy, td):
      """Trigger to fill in legacy ``moduleid`` when publishing.
  
      This correctly assigns ``moduleid`` value to
      cnx-publishing publications. This does NOT include
      matching the ``moduleid`` to previous revision by way of ``uuid``.
  
      This correctly updates the sequence values when a legacy publication
      specifies the ``moduleid`` value. This is because legacy does not know
      about nor use the sequence values when setting legacy ``moduleid``.
  
      """
      modified_state = "OK"
      portal_type = td['new']['portal_type']
      uuid = td['new']['uuid']
      moduleid = td['new']['moduleid']
      version = td['new']['version']
  
      # Is this an insert from legacy? Legacy always supplies the version.
      is_legacy_publication = version is not None
  
      if moduleid is None:
          # If the moduleid is not supplied, it is a new publication.
          if portal_type in ("Collection", "SubCollection"):
              prefix, sequence_name = 'col', "collectionid_seq"
          else:
              prefix, sequence_name = 'm', "moduleid_seq"
          plan = plpy.prepare("SELECT $1 || nextval($2)::text AS moduleid",
                              ['text', 'text'])
          row = plpy.execute(plan, (prefix, sequence_name,), 1)
          moduleid = row[0]['moduleid']
          modified_state = "MODIFY"
          td['new']['moduleid'] = moduleid
      elif is_legacy_publication and moduleid is not None:
          # Set the sequence value based on what legacy gave us.
          plan = plpy.prepare("""\
  SELECT setval($1, max(substr(moduleid, $2)::int))
  FROM (
    SELECT moduleid from modules where portal_type in ($3,$4)
    UNION ALL
    SELECT $4) AS all_together""", ['text', 'int', 'text', 'text'])
          args = []
          if portal_type == 'Collection':
              args.append('collectionid_seq')
              args.append(4)
              args.extend(('Collection', 'SubCollection'))
          elif portal_type == 'Module':
              args.append('moduleid_seq')
              args.append(2)
              args.extend(('Module', 'CompositeModule'))
          args.append(moduleid)
          if len(args) == 4:
              plpy.execute(plan, args)
  
      plpy.log("Fixed identifier and version for publication at '{}' "
               "with the following values: {} and {}"
               .format(uuid, moduleid, version))
  
      return modified_state