Skip to content

DbalExecutor uses executeQuery for write operations, causing writes to replicas with PrimaryReadReplicaConnection #1551

@christiaan

Description

@christiaan

Bug Report

Q A
BC Break no
Version latest

Summary

DbalExecutor in doctrine/migrations uses Connection::executeQuery for executing SQL statements, including write operations. However, PrimaryReadReplicaConnection in doctrine/dbal expects executeQuery to be used only for read operations. This can result in write queries being sent to replica nodes, causing data inconsistency.

Current behavior

When running migrations, DbalExecutor calls executeQuery for all SQL statements, including writes (e.g., INSERT, UPDATE, DELETE, DDL). If the connection is configured as PrimaryReadReplicaConnection, these write operations may be executed on a replica instead of the primary.

How to reproduce

  1. Configure Doctrine DBAL with PrimaryReadReplicaConnection and at least one replica.
  2. Run a migration that performs a write operation (e.g., creates a table or inserts data).
  3. Observe that the write query is executed using executeQuery, which may target a replica.

Expected behavior

Write operations in migrations should use executeStatement to ensure they are always executed on the primary node, as per the contract of PrimaryReadReplicaConnection. Only read operations should use executeQuery.

See https://github.com/doctrine/dbal/blob/05f3985a266bb75c44e621e6a647027016061265/src/Connections/PrimaryReadReplicaConnection.php#L43

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions