定制 fwolfsjaeger/doctrine-cockroachdb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fwolfsjaeger/doctrine-cockroachdb

Composer 安装命令:

composer require fwolfsjaeger/doctrine-cockroachdb

包简介

Doctrine DBAL CockroachDB Driver

README 文档

README

Latest Stable Version Total Downloads PHP Version Require License

CockroachDB Driver

CockroachDB Driver is a Doctrine DBAL Driver and ORM patcher to handle incompatibilities with PostgreSQL.

Big Thanks To

  • Lapay Group for their CockroachDB Doctrine driver, which this driver is based on
  • media.monks for the idea to patch FQCNs using a composer script
  • sweoggy for his contributions to use CockroachDB's built in SERIAL generator

CockroachDB Quick Setup Guide

Usage

Configuration

# doctrine.yaml
doctrine:
    dbal:
        user: <user>
        port: <port(26257)>
        host: <host>
        dbname: <dbname>
        sslmode: verify-full
        sslrootcert: <path-to-ca.crt>
        sslcert: <path-to-user.crt>
        sslkey: <path-to-user.key>
        driver: pdo_pgsql
        driver_class: DoctrineCockroachDB\Driver\CockroachDBDriver

(Optional) Use modified BasicEntityPersister and SerialGenerator

For improved compatibility and performance we recommend you to override Doctrine ORM's default BasicEntityPersister with the custom one provided with this package. When using the custom BasicEntityPersister you can use CockroachDB's built in SERIAL generator for primary keys, which performs vastly better than Doctrine's recommended SequenceGenerator.

Overriding is done by adding the composer script DoctrineCockroachDB\\Composer\\PatchDoctrine::overrideBasicEntityPersister to the composer.json script sections post-install-cmd and post-update-cmd:

{
    "scripts": {
        "post-install-cmd": [
            "DoctrineCockroachDB\\Composer\\PatchDoctrine::overrideBasicEntityPersister"
        ],
        "post-update-cmd": [
            "DoctrineCockroachDB\\Composer\\PatchDoctrine::overrideBasicEntityPersister"
        ]
    }
}

Then change your entities to use the SerialGenerator provided by this package:

<?php

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use DoctrineCockroachDB\ORM\Id\SerialGenerator;

#[Entity]
#[Table]
class Entity
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: SerialGenerator::class)]
    #[ORM\Column(name: 'id', type: Types::INTEGER, options: ['unsigned' => true])]
    private int $id;
}

Finally, you should register the DoctrineCockroachDB\ORM\Listener\AddDefaultToSerialGeneratorListener and DoctrineCockroachDB\ORM\Listener\RemoveDefaultFromForeignKeysListener (in that order) to get proper default values for the identifiers using SerialGenerator when using Doctrine ORM.

Troubleshooting

ERROR: currval(): could not determine data type of placeholder $1

This is caused by using the IdentityGenerator as GenerateValue strategy and Doctrine ORM's default BasicEntityPersister. It is solved by using our custom BasicEntityPersister and SerialGenerator, see above for instructions.

Unit testing

Start an insecure single-node instance:

cockroach start-single-node \
  --store='type=mem,size=1GB' \
  --log='sinks: {stderr: {channels: [DEV]}}' \
  --listen-addr=127.0.0.1:26257 \
  --insecure \
  --accept-sql-without-tls

Connect to CockroachDB:

cockroach sql --host=127.0.0.1:26257 --insecure

Create the user & database for the tests:

CREATE USER "doctrine_tests";
CREATE DATABASE doctrine_tests OWNER "doctrine_tests";
USE doctrine_tests;
CREATE SCHEMA doctrine_tests AUTHORIZATION "doctrine_tests";
ALTER DATABASE doctrine_tests SET search_path = doctrine_tests;
GRANT ALL PRIVILEGES ON DATABASE doctrine_tests TO "doctrine_tests";
GRANT ALL PRIVILEGES ON SCHEMA doctrine_tests TO "doctrine_tests";
CREATE TABLE doctrine_tests.TestEntity (an_identifier SERIAL8 NOT NULL, second_identifier SERIAL8 NOT NULL, a_string_column VARCHAR(255) NOT NULL);
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA doctrine_tests TO "doctrine_tests";

License

MIT

fwolfsjaeger/doctrine-cockroachdb 适用场景与选型建议

fwolfsjaeger/doctrine-cockroachdb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.13k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「doctrine」 「pdo」 「cockroach」 「cockroachdb」 「pdo_pgsql」 「doctrine-cockroachdb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fwolfsjaeger/doctrine-cockroachdb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fwolfsjaeger/doctrine-cockroachdb 我们能提供哪些服务?
定制开发 / 二次开发

基于 fwolfsjaeger/doctrine-cockroachdb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-09