Wrap publishing in a database transaction to prevent partial templates

The whole thing will disappear if something goes wrong.