-
Notifications
You must be signed in to change notification settings - Fork 6
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
Create views fixes #149
Conversation
in Unit Tests: |
@cymed |
datamodel/scripts/setup.sh
Outdated
@@ -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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 = {}):'
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
The only thing that is not working here is the shebang. Can someone with linux experience set up the shell script please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add shebang
for more information, see https://pre-commit.ci
There was a problem hiding this 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)
There was a problem hiding this 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) |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
# ${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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# ${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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cursor.execute(sql) | |
cursor.execute(sql, {'viewer_role': f'tww_viewer_{db_identifier}', 'user_role': f'tww_user_{db_identifier}'}) |
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 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. |
@3nids is it possible to upgrade pirogue to psycopg3 as well? |
@ponceta yes pirogue is an easy one to upgrade. |
superseded by #197 |
This PR adds the following functionalities: