Skip to content

[WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files [ Team Review]#3054

Open
pravingv wants to merge 5 commits intowevote:developfrom
pravingv:WV_2626_Trigramview

Conversation

@pravingv
Copy link
Copy Markdown
Contributor

@pravingv pravingv commented Mar 19, 2026

**What github.com/wevote server/issues does this fix?
WV-2626 Create button in candidate, politician page to call trigram index creation function
WV-2619 Create utility script to add trigram index to database without creating migration files

Changes included this pull request?**
WV-2626 Creates link in candidate, politician page to call trigram index creation function
Created a link in candidate, politician page to call trigram index creation function with drop down list of fields for which trigram index to be added. After running utility script the webpage shows status of index with table and index name.

WV-2619 utility script to add trigram index
wevote_functions/create_trigram_index.py
Created a new utility script to add a GiST-based trigram index to a specified column in the WeVote server database table. The script runs independently and can also be imported for use within other application views.

@pravingv pravingv changed the title [WV-2626] Create button in candidate, politician page to call trigram index creation function [[WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files Mar 19, 2026
@pravingv pravingv changed the title [[WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files [[WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files [ Team Review] Mar 19, 2026
@pravingv pravingv changed the title [[WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files [ Team Review] [WV-2626] Create button in candidate, politician page to call trigram index creation function [WV-2619] Create utility script to add trigram index to database without creating migration files [ Team Review] Mar 19, 2026
</p>

{% csrf_token %}
% {csrf_token %}
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.

revert back to {% csrf_token %}

indexes_already_existed.append(index_name)
continue

create_index_sql = f"""CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} USING gist ({field.column} gist_trgm_ops);"""
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.

While I see you added safeguards to sanitize the variables, executing raw SQL strings is still very risky. Execute this as sub-process commands like this to prevent sql injection:

result = subprocess.run(command_args, capture_output=True) # , shell=False

@mjacquot1
Copy link
Copy Markdown
Contributor

Address my comments and I'd say this is good for now.

@pravingv
Copy link
Copy Markdown
Contributor Author

pravingv commented Apr 7, 2026

Updated utility function to handle dynamic table and column names in Django/PostgreSQL is to use the psycopg2.sql . This securely quotes and escapes identifiers (table names, column names, index names) directly within the database driver, completely neutralizing injection risks without needing to spawn an external subprocess

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.

2 participants