[database] Improve protocol template lookup for prototype

Rather than blindingly selecting the first protocol template
found, look for the first valid one.
4 jobs for improve_database_prototype_generation in 22 minutes and 14 seconds (queued for 6 seconds)