Skip to content

Support for custom schemas per database and custom extensions per schema #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,14 @@ postgres:
schemas:
public:
owner: 'localUser'
# enable per-db extension
extensions:
uuid-ossp:
schema: 'public'

# optional schemas to enable on database
schemas:
uuid_ossp:
dbname: db1
owner: localUser

# optional extensions to install in schema
extensions:
uuid-ossp:
schema: uuid_ossp
maintenance_db: db1
#postgis: {}
# enable per-schema extension
extensions:
- uuid-ossp
secret_business_logic:
owner: 'remoteUser'
# enable per-schema extension
extensions:
- uuid-ossp
- pg_trgm

# vim: ft=yaml ts=2 sts=2 sw=2 et
1 change: 0 additions & 1 deletion postgres/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

{%- macro format_state(name, state, kwarg) %}

{%- do kwarg.update({'name': name}) %}
{%- if 'ensure' in kwarg %}
{%- set ensure = kwarg.pop('ensure') %}
{%- endif %}
Expand Down
83 changes: 60 additions & 23 deletions postgres/manage.sls
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{%- from "postgres/map.jinja" import postgres with context -%}
{%- from "postgres/macros.jinja" import format_state with context -%}
{%- from "postgres/macros.jinja" import format_kwargs with context -%}

{%- if not salt.get('postgres.user_create') %}

Expand All @@ -25,6 +26,7 @@ postgres-reload-modules:

{%- for name, user in postgres.users|dictsort() %}

{%- do user.update({'name': name}) %}
{{ format_state(name, 'postgres_user', user) }}
- require:
- test: postgres-reload-modules
Expand All @@ -34,6 +36,7 @@ postgres-reload-modules:
# Tablespace states

{%- for name, tblspace in postgres.tablespaces|dictsort() %}
{%- do tblspace.update({'name': name}) %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required because I removed the automatic generation of name parameter from format_state macro


{{ format_state(name, 'postgres_tablespace', tblspace) }}
- require:
Expand All @@ -46,45 +49,79 @@ postgres-reload-modules:

# Database states

{%- for name, db in postgres.databases|dictsort() %}
{%- macro format_database(db_name, kwarg) %}

{{ format_state(name, 'postgres_database', db) }}
- require:
- test: postgres-reload-modules
{%- if 'owner' in db %}
- postgres_user: postgres_user-{{ db.owner }}
{%- do kwarg.update({'name': db_name}) %}
{%- if 'ensure' in kwarg %}
{%- set ensure = kwarg.pop('ensure') %}
{%- endif %}
{%- if 'tablespace' in db %}
- postgres_tablespace: postgres_tablespace-{{ db.tablespace }}
{%- if 'user' not in kwarg %}
{%- do kwarg.update({'user': postgres.user}) %}
{%- endif %}

{%- endfor %}
{# extract any extensions or schemas #}
{%- if 'schemas' in kwarg %}
{%- set schemas = kwarg.pop('schemas') %}
{%- else %}
{%- set schemas = {} %}
{%- endif %}

# Schema states

{%- for name, schema in postgres.schemas|dictsort() %}
postgres_database-{{ db_name }}:
postgres_database.{{ ensure|default('present') }}:
{{- format_kwargs(kwarg) }}
- require:
- test: postgres-reload-modules
{%- if 'owner' in kwarg %}
- postgres_user: postgres_user-{{ kwarg.owner }}
{%- endif %}
{%- if 'tablespace' in kwarg %}
- postgres_tablespace: postgres_tablespace-{{ kwarg.tablespace }}
{% endif %}

{# per db schemas #}
{%- for schema_name, schema in schemas|dictsort() %}

{%- do schema.update({'dbname': db_name, 'name': schema_name}) %}
{# multiple databases can have a schema with the same name #}
{%- set schema_salt_state_suffix = db_name ~ '-' ~ schema_name %}
{%- if 'extensions' in schema %}
{%- set extensions = schema.pop('extensions') %}
{%- else %}
{%- set extensions = {} %}
{%- endif %}

{{ format_state(name, 'postgres_schema', schema) }}
{{ format_state(schema_salt_state_suffix, 'postgres_schema', schema) }}
- require:
- test: postgres-reload-modules
- postgres_database: postgres_database-{{ db_name }}
{%- if 'owner' in schema %}
- postgres_user: postgres_user-{{ schema.owner }}
{%- endif %}

{%- endfor %}

# Extension states
{# per schema extensions #}
{%- for extension in extensions %}

{%- for name, extension in postgres.extensions|dictsort() %}

{{ format_state(name, 'postgres_extension', extension) }}
{# multiple databases can use the same extension #}
postgres_extension-{{ db_name }}-{{ extension }}:
postgres_extension.present:
- schema: {{ schema_name }}
- maintenance_db: {{ db_name }}
- name: {{ extension }}
- require:
- test: postgres-reload-modules
{%- if 'maintenance_db' in extension %}
- postgres_database: postgres_database-{{ extension.maintenance_db }}
{%- endif %}
{%- if 'schema' in extension %}
- postgres_schema: postgres_schema-{{ extension.schema }}
{%- endif %}
- postgres_database: postgres_database-{{ db_name }}
- postgres_schema: postgres_schema-{{ schema_salt_state_suffix }}

{%- endfor %}

{%- endfor %}

{%- endmacro %}

{%- for name, db in postgres.databases|dictsort() %}

{{ format_database(name, db) }}

{%- endfor %}