I was surprised to discover that add_role() modifies the database and fails if the role already exists. There are two implications here, one first more serious than the other: 1) if you're in development and update your add_role code, you must first remove_role() 2) once you have it right, you should never have to run that code again.
So typically I have been putting my add_role() inside a wp_loaded action hook. And since I'm in development, I've also added a remove_role() before my add_role so I can be sure that if I modify my list of caps, it will actually take effect.
But clearly this is now being run every time a page of the blog is being accessed. Okay, I could put it in an admin-only action, or I could create a plugin page maybe under Users or Tools where this role can be created once. I guess I'm hoping there's a simpler, more elegant solution out there.
I don't imagine there's a run_once kind of action is there?
Or is the best practice just to add the role and then use add_cap() a bunch of times? And even then I imagine add_cap is accessing the db.
Just thinking in terms of the best way to reduce unncessary db access. What are your best practices?