Skip to content
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

Fixed problems with per database extensions and schemas #140 #223

Merged

Conversation

t0fik
Copy link
Contributor

@t0fik t0fik commented Jun 14, 2018

Fixes issue #140

Copy link
Contributor

@vutny vutny left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, @t0fik !

This is going to supersede #147 with much cleaner implementation, I hope.

We just need to address few issues, please take a look at in-line comments below.


{%- if 'extensions' in db %}
{%- for ext_name, extension in db.pop('extensions')|dictsort() %}
{%- do extension.update({'maintenance_db': name }) %}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we'd look at CREATE EXTENSION PG SQL syntax, we see that there is no direct relation between an extension and a database.
So an extension is intended to be installed into a schema, and in turn a schema should be associated with a database.

The maintenance_db parameter for all postges_*.present state function means PG cluser-wide database containing PostgreSQL internal entities, this is not the database you could install extension to.
So, no need to for the line above (49).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Following salt docs for postgresql_extension.present:
maintenance_db
Database to act on
I've checked and state behave as docs describe. Schema is optional for 'CREATE EXTENSION', default is to create extension for current database.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yes. You're absolutely right. An extension state is little bit different from the others in this case. I have checked the code of postgres.psql_query function (which is the workhorse of all those states) and it simply substitutes maintenance_db to psql --dbname {{maintenance_db}} raw command.
That's quite confusing, because other functions use dbname parameter instead of maintenance_db, so I misunderstood them. Thanks for pointing me to it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yes. You're absolutely right. An extension state is little bit different from the others in this case. I have checked the code of postgres.psql_query function (which is the workhorse of all those states) and it simply substitutes maintenance_db to psql --dbname {{maintenance_db}} raw command.
That's quite confusing, because other functions use dbname parameter instead of maintenance_db, so I misunderstood them. Thanks for pointing me to it.

{%- if 'extensions' in db %}
{%- for ext_name, extension in db.pop('extensions')|dictsort() %}
{%- do extension.update({'maintenance_db': name }) %}
{{ format_state( name + '-' + ext_name, 'postgres_extension', extension) }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind to separate format_state macro call and related parameters from other Jinja flow control operators?
This would much improve the code readability.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you consider to put newlines between state generation code and Jinja loops?

- require:
- postgres_database: postgres_database-{{ name }}
{%- endfor %}
{%- endif %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the logic of raw SQL command, we should first create a database, then schema and finally install an existing extension. You could pop up per-DB schemas and extensions parameters from Pillar dictionary for a single database ahead, and then construct the states afterwards in a logical order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Logic is OK. States are executed in correct order. Arrangement in file is irrelevant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep. Requisite parameters simply put them in right order. That's OK.

- require:
- postgres_database-{{ dbname }}
Copy link
Contributor

Choose a reason for hiding this comment

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

This addition would not work properly, because there are no dbname Jinja variable was ever assigned.
You wanted to put schema.dbname, right?

@aboe76
Copy link
Member

aboe76 commented Jun 19, 2018

@vutny can you take a second look...

Copy link
Contributor

@vutny vutny left a comment

Choose a reason for hiding this comment

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

Thanks a lot @t0fik ! Your PR looks great.
I have just one small request to make it.

{%- do kwarg.update({'name': name}) %}
{%- if 'name' not in kwarg %}
{%- do kwarg.update({'name': name}) %}
{%- endif %}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is just awesome. Would mind to update pillar.example file for the formula users to see they now able to provide unique IDs for PG entities?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can't set state id via pillar. So no need for pillar.example update. This feature is for formula developers only.
IMHO Allowing user setting state ids is asking for trouble.

Copy link
Contributor

Choose a reason for hiding this comment

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

@t0fik No-no, it is not about setting state IDs. There was PR #155 that intended to add capability to override PG entity attributes (including names) depending on some external data. But it wasn't merged. With your changes, it is also achievable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vutny Nice to hear that.

Copy link
Contributor

@vutny vutny left a comment

Choose a reason for hiding this comment

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

Thank you for your work @t0fik . This PR is great. It also adds some additional capabilities, and we could document them later.

{%- do kwarg.update({'name': name}) %}
{%- if 'name' not in kwarg %}
{%- do kwarg.update({'name': name}) %}
{%- endif %}
Copy link
Contributor

Choose a reason for hiding this comment

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

@t0fik No-no, it is not about setting state IDs. There was PR #155 that intended to add capability to override PG entity attributes (including names) depending on some external data. But it wasn't merged. With your changes, it is also achievable.

@vutny
Copy link
Contributor

vutny commented Jul 9, 2018

@aboe76 This PR is ready to merge, thanks.

@aboe76 aboe76 merged commit 8dfba2a into saltstack-formulas:master Jul 29, 2018
@aboe76
Copy link
Member

aboe76 commented Jul 29, 2018

@t0fik and @vutny thanks for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants