Templates

Templates provide a means to re-use grants across users and databases. To demonstrate their power, let's start with an example. Suppose you often hire contractors who must be given SELECT permissions to a schema named foo in all of your databases. For your PostgreSQL databases, the permission is

GRANT SELECT ON ALL TABLES IN SCHEMA foo TO contractor_user;

and for MySQL it is

GRANT SELECT ON foo.* TO 'contractor_user'@'%';

You could copy and paste these commands to each grant for every contractor, but that would be very repetitive. And then what happens when you need to update the permissions??

Instead, create template called contractors where the grant statement looks like this

{% if type == "postgresql" %}
GRANT SELECT ON ALL TABLES IN SCHEMA foo TO {{username}};
{% elif type == "mysql" %}
GRANT SELECT ON foo.* TO {{username}};
{% endif %}

You can then associate this template with each grant. Et voila, each contractor has the same permissions that can be edited at any time across all of your databases.

The syntax of templates is the same as any other grant. See the Grants documentation for more details.