Skip to content

Fix/mysql compatibility - #2769

Open
Mooka-Mooka wants to merge 2 commits into
evolution-foundation:mainfrom
Mooka-Mooka:fix/mysql-compatibility
Open

Mooka-Mooka wants to merge 2 commits into
evolution-foundation:mainfrom
Mooka-Mooka:fix/mysql-compatibility

Conversation

@Mooka-Mooka

@Mooka-Mooka Mooka-Mooka commented Oct 26, 2026 •

Copy link
Copy Markdown

📋 Description

🔗 Related Issue

Closes #(issue_number)

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

📝 Additional Notes

Summary by Sourcery

Enable reliable MySQL/MariaDB deployments by adding provider-compatible Prisma behavior, schema migrations, service queries, build setup, and compatibility coverage.

New Features:

  • Add MySQL/MariaDB deployment support, including provider-specific migrations and chat label operations.
  • Add disposable local MySQL compatibility coverage for JSON filtering, message handling, labels, Chatwoot updates, and schema behavior.

Bug Fixes:

  • Fix database queries and JSON handling so message, chat, unread-count, and Chatwoot operations work correctly with MySQL while preserving instance isolation.
  • Restore the WhatsApp LID cache field and enforce compound label uniqueness per instance.

Enhancements:

  • Make Prisma client generation provider-aware during builds and centralize provider-specific JSON path handling.
  • Replace PostgreSQL-specific message queries with provider-compatible service operations and MySQL chat-list retrieval.

Build:

  • Update the build process to generate the configured Prisma client before type-checking and bundling.

Deployment:

  • Document MySQL setup, migration requirements, duplicate-label handling, and compatibility testing procedures.

Documentation:

  • Document MySQL configuration, migration steps, upgrade considerations, and compatibility test execution.

Tests:

  • Add integration-style compatibility tests against a disposable local MariaDB/MySQL database without starting WhatsApp or external services.

@sourcery-ai

sourcery-ai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR adds end-to-end MySQL/MariaDB compatibility by generating the correct Prisma client, adapting JSON paths and database-specific SQL, restoring required schema elements, and validating provider behavior through disposable-database integration tests.

Sequence diagram for MySQL message and label operations

sequenceDiagram
    participant Service as ChannelService
    participant Repository as PrismaRepository
    participant Database as MySQL

    Service->>Repository: jsonPath
    Repository-->>Service: MySQL JSON path
    Service->>Repository: message.findFirst or message.updateMany
    Repository->>Database: Provider-compatible JSON query
    Database-->>Repository: Message result
    Repository-->>Service: Message result
    Service->>Repository: $executeRaw for addLabel or removeLabel
    Repository->>Database: JSON_ARRAY or JSON_SEARCH update
    Database-->>Repository: Write result
Loading

Flow diagram for MySQL build and migration validation

flowchart TD
    Start[Configure MySQL provider and connection] --> Build[npm run build]
    Build --> Generate[npm run db:generate]
    Generate --> TypeCheck[tsc --noEmit]
    TypeCheck --> Bundle[tsup]
    Bundle --> Deploy[npm run db:deploy]
    Deploy --> Migration[Restore lid and add Label compound unique index]
    Migration --> Test[Run mysql-compatibility.test.cjs]
    Test --> Disposable[Disposable MariaDB or MySQL database]
    Disposable --> Verify[Execute service methods without WhatsApp]
Loading

File-Level Changes

Change Details Files
Added provider-aware Prisma generation, schema/migration updates, and documented MySQL/MariaDB deployment and compatibility-test procedures.
  • Generate the database-specific Prisma client during builds.
  • Restore the WhatsApp LID column and enforce compound label uniqueness in a MySQL migration.
  • Document required provider configuration, migration precautions, and disposable-database testing.
README.md
package.json
prisma/mysql-migrations/20261026000000_restore_mysql_compatibility/migration.sql
prisma/mysql-schema.prisma
Centralized JSON filtering differences and replaced PostgreSQL-specific message queries with provider-neutral Prisma operations.
  • Generate MySQL JSON paths while retaining PostgreSQL paths.
  • Migrate message lookup, update, count, and filtering operations to Prisma APIs.
  • Preserve instance scoping and await unread-chat updates.
src/api/repository/repository.service.ts
src/api/integrations/channel/meta/whatsapp.business.service.ts
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
src/api/services/channel.service.ts
Implemented MySQL-specific SQL for chat listing and atomic label mutations while preserving the existing PostgreSQL behavior.
  • Add a MySQL window-function query for latest-message chat results, pagination, JSON conversion, and boolean normalization.
  • Use MySQL JSON functions and duplicate-key updates for label add/remove operations.
  • Retain PostgreSQL SQL paths for non-MySQL deployments.
src/api/services/channel.service.ts
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Added integration coverage against a disposable local MariaDB/MySQL database.
  • Exercise provider JSON paths, message retrieval/filtering, read updates, chat pagination, labels, compound label identity, LID persistence, and Chatwoot updates.
  • Verify account isolation, special-character handling, and SQL-injection-resistant filters without starting external services.
test/mysql-compatibility.test.cjs

Possibly linked issues

  • #N/A: The PR replaces PostgreSQL-specific message queries with provider-compatible Prisma operations and MySQL SQL, directly addressing the reported errors.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="package.json" line_range="8" />
<code_context>
   "type": "commonjs",
   "scripts": {
-    "build": "tsc --noEmit && tsup",
+"build": "npm run db:generate && tsc --noEmit && tsup",
     "start": "tsx ./src/main.ts",
     "start:prod": "node dist/main",
</code_context>
<issue_to_address>
**Build provider enables shell commands**

When an attacker can control `DATABASE_PROVIDER` in the build environment, `runWithProvider.js` substitutes `DATABASE_PROVIDER` into the command and passes it to `execSync`; shell metacharacters in a provider value execute attacker-chosen commands during the build, exposing or altering build-worker data.

Validate `DATABASE_PROVIDER` against the supported provider names and invoke Prisma without constructing a shell command from the value.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if a MySQL JSON filter selects the wrong messages, the read-status update can persist incorrect statuses, and label upserts can alter stored chat labels; reverting the code will not restore those values. The migration itself fails on duplicate labels rather than deleting them, but the runtime data changes may need manual repair.

Blocking findings: package.json:8


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread package.json
"type": "commonjs",
"scripts": {
"build": "tsc --noEmit && tsup",
"build": "npm run db:generate && tsc --noEmit && tsup",

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.

🟠 High · Build provider enables shell commands

When an attacker can control DATABASE_PROVIDER in the build environment, runWithProvider.js substitutes DATABASE_PROVIDER into the command and passes it to execSync; shell metacharacters in a provider value execute attacker-chosen commands during the build, exposing or altering build-worker data.

Validate DATABASE_PROVIDER against the supported provider names and invoke Prisma without constructing a shell command from the value.

Prompt for AI agents
In `package.json` at line 8:

**Build provider enables shell commands**

When an attacker can control `DATABASE_PROVIDER` in the build environment, `runWithProvider.js` substitutes `DATABASE_PROVIDER` into the command and passes it to `execSync`; shell metacharacters in a provider value execute attacker-chosen commands during the build, exposing or altering build-worker data.

Validate `DATABASE_PROVIDER` against the supported provider names and invoke Prisma without constructing a shell command from the value.

This branch has not been deployed

No deployments
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.

1 participant