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

Added support for different database provider #584

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zpratikpathak
Copy link

@zpratikpathak zpratikpathak commented Jan 30, 2025

What kind of change does this PR introduce?

Database support for different providers

eg: Bug fix, feature, docs update, ...

Why was this change needed?

Application was not able to automatically adjust itself to different provider based on the "DATABASE" string, it was hardcoded, if someone wants to use mariadb they may need to pull the image, get inside the image and change the provider. It would have been a lot better if it can automatically set the provider in prisma

Please link to related issues when possible, and explain WHY you changed things, not WHAT you changed.

Other information:

eg: Did you discuss this change with anybody before working on it (not required, but can be a good idea for bigger changes).
I discussed with the fellow Develop relations, since they already had mysql/mariadb and they didn't want to spin up a new PostgreSQL they thought it was a much better idea, they were already doing image modification in order to support mariadb as provider

Any plans for the future, etc?
I also want to add the guideline in docs how to choose different database and add some sample docker-compose file with different database

Checklist:

Put a "X" in the boxes below to indicate you have followed the checklist;

  • I have read the CONTRIBUTING guide.
  • I checked that there were not similar issues or PRs already open for this.
  • This PR fixes just ONE issue (do not include multiple issues or types of change in the same PR) For example, don't try and fix a UI issue and include new dependencies in the same PR.

Summary by CodeRabbit

  • Chores
    • Updated Dockerfile development configuration
    • Modified entrypoint script to dynamically set database provider based on environment variable
    • Improved script consistency and error handling for database configuration

Copy link

vercel bot commented Jan 30, 2025

@zpratikpathak is attempting to deploy a commit to the Listinai Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

coderabbitai bot commented Jan 30, 2025

Walkthrough

The pull request introduces modifications to the development Dockerfile and the entrypoint script. The changes focus on improving the script's database provider detection mechanism by dynamically setting the provider based on the DATABASE_URL environment variable. The entrypoint script now supports MySQL, PostgreSQL, and MariaDB providers, with enhanced error handling for unsupported database configurations. The Dockerfile has been updated to relocate the entrypoint script to a standard system binary location.

Changes

File Change Summary
Dockerfile.dev - Moved entrypoint script from /app/entrypoint.sh to /usr/local/bin/entrypoint.sh
- Updated ENTRYPOINT directive to reflect new script location
- Minor formatting adjustments
var/docker/entrypoint.sh - Added conditional logic to detect database provider from DATABASE_URL
- Implemented dynamic provider selection for MySQL, PostgreSQL, and MariaDB
- Added error handling for unsupported database configurations
- Maintained existing configuration and application startup logic

Sequence Diagram

sequenceDiagram
    participant Entrypoint as Entrypoint Script
    participant Prisma as Prisma Schema
    participant Database as Database Provider

    Entrypoint->>Entrypoint: Parse DATABASE_URL
    alt MySQL URL
        Entrypoint->>Prisma: Set provider to "mysql"
    else PostgreSQL URL
        Entrypoint->>Prisma: Set provider to "postgresql"
    else MariaDB URL
        Entrypoint->>Prisma: Set provider to "mariadb"
    else Invalid URL
        Entrypoint->>Entrypoint: Print error and exit
    end
    Entrypoint->>Database: Prepare database connection
Loading

Poem

🐰 In Docker's realm, a script so bright,
Detecting databases with all its might
MySQL, Postgres, MariaDB in view
Configuring providers, swift and true
A rabbit's magic, code refined! 🔧


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
var/docker/entrypoint.sh (1)

Line range hint 29-61: Add error handling for critical operations.

While the startup logic is correct, it would benefit from additional error handling for critical operations:

Apply this diff to improve error handling:

 echo "Entrypoint: Running database migrations"
-npm run prisma-db-push
+if ! npm run prisma-db-push; then
+  echo "ERROR: Database migration failed"
+  exit 1
+fi

-mkdir -p /etc/supervisor.d/
+if ! mkdir -p /etc/supervisor.d/; then
+  echo "ERROR: Failed to create supervisor directory"
+  exit 1
+fi

 if [[ "$INTERNAL_PROXY_ENABLED" != "false" ]]; then
   echo "Entrypoint: Starting internal proxy"
-  cp -vf /app/supervisord_available_configs/caddy.conf /etc/supervisor.d/
+  if ! cp -vf /app/supervisord_available_configs/caddy.conf /etc/supervisor.d/; then
+    echo "ERROR: Failed to copy caddy configuration"
+    exit 1
+  fi
 fi
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92d09b5 and 76c97b8.

📒 Files selected for processing (2)
  • Dockerfile.dev (3 hunks)
  • var/docker/entrypoint.sh (2 hunks)
🔇 Additional comments (4)
var/docker/entrypoint.sh (1)

19-27: LGTM!

The configuration handling logic is well-implemented with appropriate error handling and user feedback.

Dockerfile.dev (3)

27-27: LGTM!

The entrypoint script setup follows best practices:

  1. Uses standard system binary location
  2. Sets proper permissions
  3. Uses absolute path in ENTRYPOINT

Also applies to: 38-42


Line range hint 48-82: LGTM!

The production image setup follows Docker best practices with proper layering, volume management, and labeling.


17-19: Consider version handling strategy for Alpine packages.

While pinning package versions is good practice, it could cause build failures if specific versions are removed from Alpine repositories. Consider:

  1. Using version ranges instead of exact versions
  2. Adding fallback versions
  3. Documenting version update process

Run this script to verify package availability:

Comment on lines +17 to +18
# Update the datasource provider in the schema.prisma file
sed -i "s/provider = \".*\"/provider = \"$provider\"/" ./libraries/nestjs-libraries/src/database/prisma/schema.prisma
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add safety checks for schema.prisma modification.

The current implementation lacks validation and error handling for the schema.prisma update:

Apply this diff to add necessary safety checks:

 # Update the datasource provider in the schema.prisma file
-sed -i "s/provider = \".*\"/provider = \"$provider\"/" ./libraries/nestjs-libraries/src/database/prisma/schema.prisma
+SCHEMA_PATH="./libraries/nestjs-libraries/src/database/prisma/schema.prisma"
+
+if [ ! -f "$SCHEMA_PATH" ]; then
+  echo "ERROR: schema.prisma not found at $SCHEMA_PATH"
+  exit 1
+fi
+
+# Create backup
+cp "$SCHEMA_PATH" "${SCHEMA_PATH}.bak"
+
+# Update provider
+if ! sed -i "s/provider = \".*\"/provider = \"$provider\"/" "$SCHEMA_PATH"; then
+  echo "ERROR: Failed to update provider in schema.prisma"
+  mv "${SCHEMA_PATH}.bak" "$SCHEMA_PATH"
+  exit 1
+fi
+
+# Clean up backup on success
+rm "${SCHEMA_PATH}.bak"
📝 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.

Suggested change
# Update the datasource provider in the schema.prisma file
sed -i "s/provider = \".*\"/provider = \"$provider\"/" ./libraries/nestjs-libraries/src/database/prisma/schema.prisma
# Update the datasource provider in the schema.prisma file
SCHEMA_PATH="./libraries/nestjs-libraries/src/database/prisma/schema.prisma"
if [ ! -f "$SCHEMA_PATH" ]; then
echo "ERROR: schema.prisma not found at $SCHEMA_PATH"
exit 1
fi
# Create backup
cp "$SCHEMA_PATH" "${SCHEMA_PATH}.bak"
# Update provider
if ! sed -i "s/provider = \".*\"/provider = \"$provider\"/" "$SCHEMA_PATH"; then
echo "ERROR: Failed to update provider in schema.prisma"
mv "${SCHEMA_PATH}.bak" "$SCHEMA_PATH"
exit 1
fi
# Clean up backup on success
rm "${SCHEMA_PATH}.bak"

Comment on lines +5 to +15
# Determine the provider based on the DATABASE_URL
if [[ "$DATABASE_URL" == mysql://* ]]; then
provider="mysql"
elif [[ "$DATABASE_URL" == postgresql://* ]]; then
provider="postgresql"
elif [[ "$DATABASE_URL" == mariadb://* ]]; then
provider="mariadb"
else
echo "Unsupported database provider in DATABASE_URL"
exit 1
fi
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance database provider detection robustness.

While the provider detection logic works, it could be more robust:

  1. Add validation for empty/unset DATABASE_URL
  2. Support case-insensitive matching
  3. Provide more informative error message listing supported providers

Apply this diff to improve the implementation:

 # Determine the provider based on the DATABASE_URL
+if [ -z "$DATABASE_URL" ]; then
+  echo "ERROR: DATABASE_URL environment variable is not set"
+  exit 1
+fi
+
+database_url_lower=$(echo "$DATABASE_URL" | tr '[:upper:]' '[:lower:]')
-if [[ "$DATABASE_URL" == mysql://* ]]; then
+if [[ "$database_url_lower" == mysql://* ]]; then
   provider="mysql"
-elif [[ "$DATABASE_URL" == postgresql://* ]]; then
+elif [[ "$database_url_lower" == postgresql://* ]]; then
   provider="postgresql"
-elif [[ "$DATABASE_URL" == mariadb://* ]]; then
+elif [[ "$database_url_lower" == mariadb://* ]]; then
   provider="mariadb"
 else
-  echo "Unsupported database provider in DATABASE_URL"
+  echo "ERROR: Unsupported database provider in DATABASE_URL"
+  echo "Supported providers: mysql://, postgresql://, mariadb://"
   exit 1
 fi
📝 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.

Suggested change
# Determine the provider based on the DATABASE_URL
if [[ "$DATABASE_URL" == mysql://* ]]; then
provider="mysql"
elif [[ "$DATABASE_URL" == postgresql://* ]]; then
provider="postgresql"
elif [[ "$DATABASE_URL" == mariadb://* ]]; then
provider="mariadb"
else
echo "Unsupported database provider in DATABASE_URL"
exit 1
fi
# Determine the provider based on the DATABASE_URL
if [ -z "$DATABASE_URL" ]; then
echo "ERROR: DATABASE_URL environment variable is not set"
exit 1
fi
database_url_lower=$(echo "$DATABASE_URL" | tr '[:upper:]' '[:lower:]')
if [[ "$database_url_lower" == mysql://* ]]; then
provider="mysql"
elif [[ "$database_url_lower" == postgresql://* ]]; then
provider="postgresql"
elif [[ "$database_url_lower" == mariadb://* ]]; then
provider="mariadb"
else
echo "ERROR: Unsupported database provider in DATABASE_URL"
echo "Supported providers: mysql://, postgresql://, mariadb://"
exit 1
fi

@nevo-david
Copy link
Contributor

it's a big change to check, let see if this pull requests get more upvotes!

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