From e08b11ee2624696455c64155766bb6a83c1d9068 Mon Sep 17 00:00:00 2001 From: YetAnotherMinion Date: Wed, 8 Feb 2017 12:38:03 -0600 Subject: [PATCH 1/2] Support for custom schemas per database and custom extensions per schema Resolves #146 and #140 Removes the top level configuration of schemas and extensions in the pillar and moved schemas under the scope of each individual db configuration. Now it is possible to use the same name for a schema in different databases. Extensions are configured as a list under each schema configuration. Now it is possible to install the same extension under multiple schemas in the same database and different databases. I updated the pillar.example to reflect the new structure. --- pillar.example | 26 +++++--------- postgres/macros.jinja | 1 - postgres/manage.sls | 82 +++++++++++++++++++++++++++++++------------ 3 files changed, 68 insertions(+), 41 deletions(-) diff --git a/pillar.example b/pillar.example index f6e03570..389a87d4 100644 --- a/pillar.example +++ b/pillar.example @@ -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 diff --git a/postgres/macros.jinja b/postgres/macros.jinja index 3732df6e..63ffb307 100644 --- a/postgres/macros.jinja +++ b/postgres/macros.jinja @@ -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 %} diff --git a/postgres/manage.sls b/postgres/manage.sls index 66f5865e..188e0d16 100644 --- a/postgres/manage.sls +++ b/postgres/manage.sls @@ -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') %} @@ -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 @@ -34,6 +36,7 @@ postgres-reload-modules: # Tablespace states {%- for name, tblspace in postgres.tablespaces|dictsort() %} + {%- do tblspace.update({'name': name}) %} {{ format_state(name, 'postgres_tablespace', tblspace) }} - require: @@ -46,45 +49,78 @@ 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 }} + - 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 %} From ae44fc73f8227f87f649d822c1352313635966f1 Mon Sep 17 00:00:00 2001 From: YetAnotherMinion Date: Wed, 8 Feb 2017 20:41:32 -0600 Subject: [PATCH 2/2] actually apply the extension to a specific database --- postgres/manage.sls | 1 + 1 file changed, 1 insertion(+) diff --git a/postgres/manage.sls b/postgres/manage.sls index 188e0d16..f98a9aaf 100644 --- a/postgres/manage.sls +++ b/postgres/manage.sls @@ -107,6 +107,7 @@ postgres_database-{{ db_name }}: postgres_extension-{{ db_name }}-{{ extension }}: postgres_extension.present: - schema: {{ schema_name }} + - maintenance_db: {{ db_name }} - name: {{ extension }} - require: - test: postgres-reload-modules