Skip to content

Small refactor and Compatibility with DHIS2 2.41#55

Merged
ifoche merged 34 commits intodevelopmentfrom
feature/update_sqls_to_241
Apr 2, 2025
Merged

Small refactor and Compatibility with DHIS2 2.41#55
ifoche merged 34 commits intodevelopmentfrom
feature/update_sqls_to_241

Conversation

@idelcano
Copy link
Copy Markdown
Contributor

@idelcano idelcano commented Feb 4, 2025

https://app.clickup.com/t/8697kr4wf

This branch fixes all the sqls in 2.41 and make it multiversion.

Functions have been split into dedicated files, compatibility with DHIS2 2.41 has been added, and the handling of versioned tables has been improved.

Also some little related bugs deleting organizational units and programs and small removing user from usergroups errors have been fixed to make it work in 2.41, and also password hiding in logs has been ensured.

@idelcano idelcano requested a review from nshandra February 4, 2025 16:50
@ifoche ifoche requested a review from tokland February 5, 2025 14:39
@tokland tokland removed their request for review February 6, 2025 12:46
@idelcano
Copy link
Copy Markdown
Contributor Author

@nshandra I have changed the way SQL failures are identified so that .sql files are renamed to make them strict, calling them x_strict.sql instead of searching for errors in the log. This also requires updating the Docker setup: EyeSeeTea/d2-docker#140.

@ifoche ifoche requested a review from tokland March 19, 2025 14:23
@ifoche
Copy link
Copy Markdown
Member

ifoche commented Mar 19, 2025

when difficult to replicate the scenario, please reviewers, do only a code review, to identify any code quality or architecture issue you could identify

Copy link
Copy Markdown
Contributor

@tokland tokland left a comment

Choose a reason for hiding this comment

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

A minor in-line comments. I was about to comment that long SQL can be extracted to files (so we can format it properly) but it's some work for small value, let's keep as is.

for org_unit in orgunits:
path_query = " (path like '%{}%' and uid <> '{}') or ".format(
org_unit, org_unit)
path_query = path_query[:-3]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That [:-3] is very cryptic, I guess it's removing the trailing "or ". For that, you can use str.join, like this:

    path_query = " or ".join([
        f"(path like '%{orgunit}%' and uid <> '{orgunit}')"
        for orgunit in orgunits
    ])

(I use also f-strings, python +3.6)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Arnau, thanks. I wouldn’t change it now because it’s copy-pasted from an older file and I’m not entirely sure what it does — it’s old code. And there are already quite a few changes in this PR.

Copy link
Copy Markdown
Collaborator

@nshandra nshandra left a comment

Choose a reason for hiding this comment

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

Looks good. Minor styling and leftover issues.

Comment on lines +104 to +106
pre_api_version=args.pre_api
if args.post_api is not None:
post_api_version=args.post_api
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor nitpick, should be:
pre_api_version = args.pre_api

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call on the style issues, but I spoke with Arnau about defining and applying a standard style to all the project files once there are no more pull requests.

if sql_data_elements != "":
has_rule = True
if sql_datasets != "":
sql_query = sql_query + """
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling: Trailing whitespace after """"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just a note in case this comes up later—
maybe it’s not the case here, but the spaces inside the SQL triple quotes (""") are intentional to avoid accidentally merging words in the queries.

DELETE FROM datavalue where dataelementid in (select dataelementid from datasetelement
where datasetid in (select datasetid from dataset where uid in {datasets}));
""".format(datasets=datasets))
pass
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Leftover pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have fixed this one

Comment on lines +71 to +75
def delete_all_event_programs_from_lists(programs, f):
programs = convert_to_sql_format(programs)
delete_all_event_programs(programs, f)

def delete_all_event_programs(programs, f):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling: 2 blank lines between defs

Comment on lines +2 to +7
from src.preprocess.sql_generation.versioned_table_names import *




def generate_delete_tracker_rules(trackers, data_elements, org_units, org_unit_descendants,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling: should be 2 blank lines

Comment on lines +200 to +207
if anonimize_org_units:
write(f, """
update {programinstance} as rand set organisationunitid=(select organisationunitid
from organisationunit where length(path)=(select length(path) from organisationunit where rand.organisationunitid= organisationunitid) ORDER BY RANDOM() limit 1) where {programinstanceid}
in ( select psi.{programinstanceid} from programstageinstance psi
inner join programstage ps on ps.programstageid=psi.programstageid
inner join program p on p.programid=ps.programid where p.uid in {program});
""".format(program=sql_event_program, programstageinstance=get_event_table_name(),programinstanceid=get_enrollment_identifier_name(), programinstance=get_enrollment_table_name()))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

programstageinstance arg is unused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have fixed this one

Comment on lines +28 to +38
")".replace("(or", " ")

def fix_final_query(sql_query):
"""This method is required now to create the sql with the ; and remove possible unnecessary and;"""
sql_query = sql_query + ";"
sql_query = remove_spaces_before_semicolon(sql_query)
sql_query = sql_query.replace("and;", ";")
return sql_query

def remove_spaces_before_semicolon(s):
return re.sub(r'(\S)\s+;', r'\1;', s) No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling: 2 blank lines between defs



def write(f, text):
# print(text)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Leftover debug line?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Originally it wasn’t commented out, but Jordi asked me to comment it out back some years ago. It can actually be removed, although it’s a good spot to check the logs if debugging is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have fixed this one

Comment on lines +1 to +10

from enum import Enum

class TableKey(Enum):
USER = "userinfo"
EVENT = "event"
ENROLLMENT = "enrollment"
TRACKER = "tracker"
EVENT_COMMENT = "event_comment"
ENROLLMENT_COMMENT = "enrollment_comment" No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling

Comment on lines +20 to +22
}

def _get_tables_for_version():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Styling: 2 blank lines between defs

@ifoche ifoche merged commit 9d40b2c into development Apr 2, 2025
@ifoche ifoche deleted the feature/update_sqls_to_241 branch April 2, 2025 14:26
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.

4 participants