piotr-cz/cockpit-sql-driver
Composer 安装命令:
composer require piotr-cz/cockpit-sql-driver
包简介
SQL Driver for Cockpit CMS
README 文档
README
This addon allows to use MySQL/ MariaDB/ PostgreSQL databases instead of default MongoDB/ SQLite.
Requirements
- Cockpit CMS (next, tested up to v0.11.0 or legacy)
- MySQL 5.7.9/ MariaDB 10.2.6/ PostgreSQL 9.4
- PHP 7.1
- Enabled PHP extensions: pdo, pdo_mysql/ pdo_pgsql
Compatibility
To run Cockpit 0.9.2+ at least version 1.0.0-beta.2+ is required.
Installation
Note:
If you installed addon before ever starting Cockpit, some errors may come up once you start it.
To solve it, start Cockpit with database configuration it supports out of the box to trigger Cockpit warmup and then set configuration specific for this addon.
Manual
Download latest release and extract to COCKPIT_PATH/addons/SqlDriver directory
Using Cockpit CLI (development version)
./cp install/addon --name SqlDriver --url https://github.com/piotr-cz/cockpit-sql-driver/archive/master.zip
Using Composer
-
Make sure path to cockpit addons are defined in your projects' composer.json file
{ "name": "MY_PROJECT", "extra": { "installer-paths": { "public/cockpit/addons/{$name}": ["type:cockpit-module"] } } } -
In your project root run command
composer require piotr-cz/cockpit-sql-driver
Configuration
Example configuration for COCKPIT_PATH/config/config.php file:
<?php return [ # Cockpit configuration # … # Use SQL Driver as main data storage 'database' => [ 'server' => 'sqldriver', # Connection options 'options' => [ 'connection' => 'mysql', # One of 'mysql'|'pgsql' 'host' => 'localhost', # Optional, defaults to 'localhost' 'port' => 3306, # Optional, defaults to 3306 (MySQL) or 5432 (PostgreSQL) 'dbname' => 'DATABASE_NAME', 'username' => 'USER', 'password' => 'PASSWORD', 'charset' => 'UTF8', # Optional, defaults to 'UTF8' 'tablePrefix' => '', # Optional, database tables prefix (ie. 'cockpit_') 'bootstrapPriority' => 999, # Optional, defaults to 999 ], # Connection specific options # General: https://www.php.net/manual/en/pdo.setattribute.php # MySQL specific: https://www.php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants 'driverOptions' => [], ], ];
Rererence: Cockpit docs > Configuration
Database content migration (Cockpit v0.6.0+)
-
Export data to
COCKPIT_PATH/migrationsubdirectorymkdir migration ./cp export --target migration -
Switch database to sqldriver (see Configuration)
-
Import data from
COCKPIT_PATH/migrationsubdirectory./cp import --src migration rm -rf migration
Reference: Cockpit docs > CLI
Testing
There are integration tests included in the package. These require Cockpit CMS as a dev dependency and use it's MongoHybrid\Client API to run actions on database.
To run tests
-
Install dependencies
cd COCKPIT_PATH/addons/SqlDriver composer install -
Configure test database
copy
/phpunit.xml.distto/phpunit.xmland set up variables as in configuration -
Run tests with PHPUnit
./vendor/bin/phpunit
Drawbacks
Cockpit doesn't provide public API to register custom database drivers so this addon monkey-patches Cockpit Driver selector client (MongoHybrid Client). This means that there is no guarantee that this addon will work in future versions of Cockpit.
Collection filters
Not implemented
-
$func/$fn/$f -
$fuzzy
Work differently
-
callable
Unlike SQLite, PDO MySQL and PostgreSQL drivers don't have support for User Defined Functions in PHP language - so callable is evaluated on every result fetch. If you have lots of documents in collection and care about performance use other filters.
-
$in,$ninWhen database value is an array, evaluates to false.
-
$regexp- MySQL implemented via REGEXP + case insensitive
- PostgreSQL impemeted via POSIX Regular Expressions + case insensitive
Wrapping expression in
//or adding flags like/foobar/iwon't work, as MySQL and PosgreSQL Regexp functions don't support flags. -
$textFilter options are not supported ($minScore, $distance, $search).
Manual database optimizations
By default package creates virtual column _id with unique index on every created collection.
If you would like to speed up filters on other collection fields - add virtual column with suitable index and type.
For example to add virtual column of integer type for field FIELD_NAME in TABLE_NAME collection, use
-
MySQL:
ALTER TABLE `{TABLE_NAME}` ADD COLUMN `{FIELD_NAME}_virtual` INT AS (`document` ->> '$.{FIELD_NAME}') NOT NULL, ADD UNIQUE | KEY `idx_{TABLE_NAME}_{FIELD_NAME}` (`{FIELD_NAME}_virtual`);
Reference: MySQL 5.7 > CREATE INDEX
-
PosgreSQL:
CREATE [UNIQUE] INDEX "idx_{TABLE_NAME}_{FIELD_NAME}" ON "{FIELD_NAME}" ((("document" ->> '{FIELD_NAME}')::int));
Reference: PostgreSQL 9.4 > CREATE INDEX
Known issues
Error: Call to a member function toArray() on null
This happens when starting cockpit for the first time and this addon is installed. The reason is in that native Cockpit modules try to accesss storage which is initialized later (during custom modules bootstrap).
Cockpit must be started for the first time without being configured to use SQL driver.
Solution 1
Start Cockpit with database configuration it supports out of the box and than switch to sqldriver as described here
Solution 2
Manually create file COCKPIT_STORAGE_FOLDER/tmp/webhooks.cache.php with content
<?php return [];
Copyright and license
Copyright since 2019 Piotr Konieczny under the MIT license.
piotr-cz/cockpit-sql-driver 适用场景与选型建议
piotr-cz/cockpit-sql-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 255 次下载、GitHub Stars 达 33, 最近一次更新时间为 2019 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「mysql」 「postgresql」 「mariadb」 「cockpit」 「database driver」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 piotr-cz/cockpit-sql-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 piotr-cz/cockpit-sql-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 piotr-cz/cockpit-sql-driver 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Query filtering in your frontend
Sql dumps containing databases for the linna apps.
Database/ORM-agnostic query construction helper
Manage PostgreSQL schemas in Laravel applications
统计信息
- 总下载量: 255
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 34
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-21