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

Create views fixes #149

Closed
wants to merge 17 commits into from
Closed

Conversation

cymed
Copy link
Contributor

@cymed cymed commented Feb 7, 2024

This PR adds the following functionalities:

  • As mentioned in Handling of tww_app #147, we need to separate tww_app rights from all the other rights as we intend to drop and re-create the schema on update
  • New script in datamodel package to drop and re-create tww_app
  • makes create_views callable from outside the docker environment by relying on abspath
  • adds 6 singleinheritance views that for some reason remained in the dumps but were not created by create_views.py
  • fixes vw_tww_import to directly access tww_od schemas instead of tww_app.vw_tww_wastewater_structure

@cymed
Copy link
Contributor Author

cymed commented Feb 7, 2024

in Unit Tests:
/src/datamodel/scripts/setup.sh: line 36: /src/datamodel/scripts/../app/create_tww_app.py: Permission denied
@domi4484 Any Ideas why?

@3nids
Copy link
Contributor

3nids commented Feb 8, 2024

@cymed chmod +x create_tww_app.py

@@ -32,5 +32,5 @@ psql "service=${PGSERVICE}" -v ON_ERROR_STOP=1 -f ${DIR}/changelogs/0001/52_plan
psql "service=${PGSERVICE}" -v ON_ERROR_STOP=1 -f ${DIR}/12_0_roles.sql
psql "service=${PGSERVICE}" -v ON_ERROR_STOP=1 -f ${DIR}/12_1_roles.sql


chmod +x ${DIR}/app/create_tww_app.py
Copy link
Contributor

Choose a reason for hiding this comment

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

no, this needs to be done on your system (make the file executable)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which is a bit problematic when you work on windows and don't have the necessary rights to install git on your shell

Copy link
Contributor Author

@cymed cymed Feb 9, 2024

Choose a reason for hiding this comment

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

OK I managed that part. However, setup.sh does not treat create_tww_app as a python script anymore:

2024-02-09T06:54:04.6558778Z /src/datamodel/scripts/../app/create_tww_app.py: line 1: import: command not found
2024-02-09T06:54:04.6559590Z /src/datamodel/scripts/../app/create_tww_app.py: line 2: import: command not found
2024-02-09T06:54:04.6560370Z /src/datamodel/scripts/../app/create_tww_app.py: line 4: import: command not found
2024-02-09T06:54:04.6561163Z /src/datamodel/scripts/../app/create_tww_app.py: line 5: from: command not found
2024-02-09T06:54:04.6562024Z /src/datamodel/scripts/../app/create_tww_app.py: line 8: syntax error near unexpected token `('
2024-02-09T06:54:04.6563144Z /src/datamodel/scripts/../app/create_tww_app.py: line 8: `def run_sql_file(file_path: str, pg_service: str, variables: dict = {}):'

Copy link
Contributor

Choose a reason for hiding this comment

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

you need to add #!/usr/bin/env python on top

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as in 875eb1d? Or do we need python3?

@cymed cymed added help wanted Extra attention is needed datamodel Concerns the datamodel labels Feb 16, 2024
@cymed
Copy link
Contributor Author

cymed commented Mar 14, 2024

The only thing that is not working here is the shebang. Can someone with linux experience set up the shell script please?

Copy link
Member

@ponceta ponceta left a comment

Choose a reason for hiding this comment

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

add shebang

@ponceta ponceta closed this Mar 26, 2024
@ponceta ponceta reopened this Mar 26, 2024
@ponceta ponceta added the fix Fixing something not working label Mar 26, 2024
@ponceta ponceta requested review from 3nids and ponceta April 2, 2024 10:02
Copy link
Member

@ponceta ponceta left a comment

Choose a reason for hiding this comment

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

Main fixes and prepare work for release and app re-creation.

@3nids if you can give it a look so we don't introduce anything bad for the release management process (future)

@ponceta ponceta added the review Waiting for review label Apr 2, 2024
@ponceta ponceta linked an issue Apr 8, 2024 that may be closed by this pull request
3 tasks
Copy link
Contributor

@3nids 3nids left a comment

Choose a reason for hiding this comment

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

Good move!

I would suggest to go one step further and actually move all the content of create_views.py to create_app.py
There is quite some redundancy in the code (mainly the python function, variable creation, sql execution). Merging the 2 files would make the code better organized and a bit more robust.

I haven't looked into the details of the changes of tww_import. What is the logic to avoid relying on the view? It sounds a bit more practical to me to avoid code duplication. But no strong opinion there.


def run_sql(sql: str, pg_service: str, variables: dict = {}):
if variables:
sql = sql.format(**variables)
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 quite dangerous in terms of SQL injection and in general I would suggest to use psycopg's native way of handling variables: https://www.psycopg.org/psycopg3/docs/basic/params.html

def create_tww_app(
srid: int = 2056,
pg_service: str = "pg_tww",
tww_reach_extra: str = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
tww_reach_extra: str = None,
tww_reach_extra: Optional[Path] = None,

and adding

from pathlib import Path
from typing import Optional

srid: int = 2056,
pg_service: str = "pg_tww",
tww_reach_extra: str = None,
tww_wastewater_structure_extra: str = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

pg_service: str = "pg_tww",
tww_reach_extra: str = None,
tww_wastewater_structure_extra: str = None,
db_identifier: str = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

This argument is not documented.
Can it be None, it's not really clear to me.

Comment on lines +43 to +44
# ${DIR}/app/view/create_views.py --pg_service ${PGSERVICE} --srid ${SRID}
# psql "service=${PGSERVICE}" -v ON_ERROR_STOP=1 -f ${DIR}/app/triggers/network.sql
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# ${DIR}/app/view/create_views.py --pg_service ${PGSERVICE} --srid ${SRID}
# psql "service=${PGSERVICE}" -v ON_ERROR_STOP=1 -f ${DIR}/app/triggers/network.sql

drop the lines?

DO
$$
BEGIN
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tww_viewer_{db_identifier}') THEN
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tww_viewer_{db_identifier}') THEN
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = %(viewer_role)s) THEN

ALTER DEFAULT PRIVILEGES IN SCHEMA tww_app GRANT SELECT, REFERENCES, TRIGGER ON TABLES TO tww_viewer_{db_identifier};
ELSE NULL;
END CASE;
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tww_user_{db_identifier}') THEN
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tww_user_{db_identifier}') THEN
CASE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = %(user_role)s) THEN

sql = sql.format(**variables)
conn = psycopg2.connect(f"service={pg_service}")
cursor = conn.cursor()
cursor.execute(sql)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
cursor.execute(sql)
cursor.execute(sql, {'viewer_role': f'tww_viewer_{db_identifier}', 'user_role': f'tww_user_{db_identifier}'})

@3nids
Copy link
Contributor

3nids commented Apr 8, 2024

One more thought on the way to pass variables.

There is another thing to consider: moving to psycopg3 from psycopg2. It is quite a minor change, and probably easier to do at early stage of the project (before an official release).

Another approach is recommended in the doc:https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html#multiple-statements-in-the-same-query (using sql.SQL.format)

So, I am not sure what is the most reasonable approach here, but the best would be integrate the move to psycopg3 into this PR. Not sure if @ponceta agrees and if that's possible on your end @cymed. I can help if needed.

@ponceta
Copy link
Member

ponceta commented Apr 8, 2024

@3nids is it possible to upgrade pirogue to psycopg3 as well?

@3nids
Copy link
Contributor

3nids commented Apr 8, 2024

@ponceta yes pirogue is an easy one to upgrade.

@3nids
Copy link
Contributor

3nids commented Apr 9, 2024

superseded by #197

@3nids 3nids closed this Apr 9, 2024
@cymed cymed deleted the create_views_relative_path branch July 18, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datamodel Concerns the datamodel fix Fixing something not working help wanted Extra attention is needed review Waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handling of tww_app
3 participants