Skip to content

[Bug]: Transactions fail with autocommit_before_ddl (default for v25) #49

@LegendEffects

Description

@LegendEffects

What happened?

CockroachDB have enabled autocommit_before_ddl default to true as of V25, this results in transactions getting committed before they're told and results in PDO erroring with "No active transaction".

I'm not a CockroachDB expert and we're currently assessing whether we'll continue to use it or just use trusty old Postgres, for now I've made a workaround by configuring the session variable on the connection before a transaction by registering events in the AppServiceProvider.

private function setupCockroachDBPreventAutoCommitBeforeDDL(): void
{
    $configured = [];

    Event::listen(ConnectionEstablished::class, function (ConnectionEstablished $event) use (&$configured) {
        if ($event->connection->getDriverName() !== "crdb") {
            return;
        }

        $configured[$event->connectionName] = false;
    });

    Event::listen(TransactionBeginning::class, function (TransactionBeginning $event) use (&$configured) {
        if ($event->connection->getDriverName() !== "crdb" || ($configured[$event->connectionName] ?? false)) {
            return;
        }

        $event->connection->statement("SET SESSION autocommit_before_ddl = false");
        $configured[$event->connectionName] = true;
    });
}

How to reproduce the bug

  • Run CRDB with autocommit_before_ddl set to true
  • Attempt to run migrations or anything else that starts a transaction
  • "No active transaction error"

Package Version

1.4.0

PHP Version

8.3.16

Laravel Version

10.48.22

CockroachDB Version

v25.1

Which operating systems does with happen with?

macOS

Notes

See: cockroachdb/cockroach#140156

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions