-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changed recreate_db to skip non sqlite attributes #2168
Changed recreate_db to skip non sqlite attributes #2168
Conversation
📝 WalkthroughWalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Script
participant Database
User->>Script: Run recreate_db
Script->>Script: Check SPIFFWORKFLOW_BACKEND_DATABASE_TYPE
alt If not SQLite
Script->>Database: Process database URI
Database-->>Script: Connection established
Script->>Database: Perform operations (create/drop)
else If SQLite
Script->>Database: Handle SQLite operations
end
Script-->>User: Complete database setup
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
Script was crashing when using SQLite. Nested setting database variables irrelevant to SQLite so that script can continue running when using SQLite.
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.
if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" != "sqlite" ]]; then | ||
if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then | ||
database_host_and_port=$(grep -oP "^[^:]+://.*@\K(.+?)[/]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[\/]$//') | ||
database_host=$(awk -F ':' '{print $1}' <<<"$database_host_and_port") | ||
database_port=$(awk -F ':' '{print $2}' <<<"$database_host_and_port") | ||
database_username_and_password=$(grep -oP "^[^:]+://\K([^@]+)[@]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[@]$//') | ||
database_username=$(awk -F ':' '{print $1}' <<<"$database_username_and_password") | ||
database_password=$(awk -F ':' '{print $2}' <<<"$database_username_and_password") | ||
database_name_from_uri=$(grep -oP "/\K(\w+)$" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI") | ||
if ! grep "\<$database_name_from_uri\>" <<<"$databases_to_run_on"; then | ||
databases_to_run_on="$database_name_from_uri" | ||
fi | ||
fi |
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.
🛠️ Refactor suggestion
Improve Database URI Parsing for Robustness and Portability
The current implementation for parsing SPIFFWORKFLOW_BACKEND_DATABASE_URI
uses multiple grep -oP
, awk
, and sed
commands with complex regular expressions. This approach may:
- Be fragile when handling URIs with special characters, different formats, or URL-encoded components.
- Suffer from portability issues since the
-P
option (Perl-compatible regex) ingrep
is not supported on all systems by default.
Consider the following improvements:
-
Use a dedicated URI parsing tool or utility: Instead of manually parsing the URI, utilize a more robust method or tool designed for URI parsing. For instance, you can use Python's
urllib.parse
module within the script to reliably extract URI components. -
Enhance portability: Replace
grep -oP
with POSIX-compliant tools or pureawk
to ensure compatibility across different environments.
Example Refactor Using awk
:
Here's a refactored version that uses awk
for parsing without relying on non-standard options:
if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" != "sqlite" ]]; then
if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then
- database_host_and_port=$(grep -oP "^[^:]+://.*@\K(.+?)[/]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[\/]$//')
- database_host=$(awk -F ':' '{print $1}' <<<"$database_host_and_port")
- database_port=$(awk -F ':' '{print $2}' <<<"$database_host_and_port")
- database_username_and_password=$(grep -oP "^[^:]+://\K([^@]+)[@]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[@]$//')
- database_username=$(awk -F ':' '{print $1}' <<<"$database_username_and_password")
- database_password=$(awk -F ':' '{print $2}' <<<"$database_username_and_password")
- database_name_from_uri=$(grep -oP "/\K(\w+)$" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI")
+ database_username_and_password=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'[@:]+' '{print $2}')
+ database_username=$(echo "$database_username_and_password" | awk -F':' '{print $1}')
+ database_password=$(echo "$database_username_and_password" | awk -F':' '{print $2}')
+ database_host_and_port=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'[@/]' '{print $2}')
+ database_host=$(echo "$database_host_and_port" | awk -F':' '{print $1}')
+ database_port=$(echo "$database_host_and_port" | awk -F':' '{print $2}')
+ database_name_from_uri=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'/' '{print $NF}')
if ! grep -q -w "$database_name_from_uri" <<<"$databases_to_run_on"; then
databases_to_run_on="$database_name_from_uri"
fi
fi
fi
Benefits of This Refactor:
- Portability: Eliminates the use of
grep -P
, enhancing compatibility with systems that have standardgrep
implementations. - Robustness: Reduces reliance on complex regular expressions, minimizing the risk of parsing errors with different URI formats.
- Readability: Simplifies the parsing logic, making the script easier to maintain and understand.
Additional Recommendations:
- Quote Variables Properly: Ensure all variable expansions are properly quoted to prevent word splitting or globbing issues, especially when dealing with data that may contain spaces or special characters.
- Validate Parsed Components: Add validation checks after parsing to ensure that each component (username, password, host, port, database name) has been extracted correctly before proceeding.
Please test the refactored code thoroughly to confirm it handles all expected URI formats correctly.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" != "sqlite" ]]; then | |
if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then | |
database_host_and_port=$(grep -oP "^[^:]+://.*@\K(.+?)[/]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[\/]$//') | |
database_host=$(awk -F ':' '{print $1}' <<<"$database_host_and_port") | |
database_port=$(awk -F ':' '{print $2}' <<<"$database_host_and_port") | |
database_username_and_password=$(grep -oP "^[^:]+://\K([^@]+)[@]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[@]$//') | |
database_username=$(awk -F ':' '{print $1}' <<<"$database_username_and_password") | |
database_password=$(awk -F ':' '{print $2}' <<<"$database_username_and_password") | |
database_name_from_uri=$(grep -oP "/\K(\w+)$" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI") | |
if ! grep "\<$database_name_from_uri\>" <<<"$databases_to_run_on"; then | |
databases_to_run_on="$database_name_from_uri" | |
fi | |
fi | |
if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" != "sqlite" ]]; then | |
if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then | |
database_username_and_password=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'[@:]+' '{print $2}') | |
database_username=$(echo "$database_username_and_password" | awk -F':' '{print $1}') | |
database_password=$(echo "$database_username_and_password" | awk -F':' '{print $2}') | |
database_host_and_port=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'[@/]' '{print $2}') | |
database_host=$(echo "$database_host_and_port" | awk -F':' '{print $1}') | |
database_port=$(echo "$database_host_and_port" | awk -F':' '{print $2}') | |
database_name_from_uri=$(echo "$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | awk -F'/' '{print $NF}') | |
if ! grep -q -w "$database_name_from_uri" <<<"$databases_to_run_on"; then | |
databases_to_run_on="$database_name_from_uri" | |
fi | |
fi | |
fi |
thank you, @twentyninehairs ! |
Summary by CodeRabbit
New Features
Bug Fixes
Chores