openclerk / db
Simple PHP database management
Requires
- monolog/monolog: ~1.11
- openclerk/events: ~0.1
Requires (Dev)
- phpunit/phpunit: ~4.4
- soundasleep/component-tests: ~0.1
This package is auto-updated.
Last update: 2024-12-14 09:51:10 UTC
README
A library for MySQL database management in Openclerk, supporting migrations, connection abstraction and automated replication switching.
Installing
Include openclerk/db
as a requirement in your project composer.json
,
and run composer update
to install it into your project:
{ "require": { "openclerk/db": "^0.1" } }
Using
Use component-discovery to enable
discovery of migrations across all of your dependencies. Update your discovery.json
:
{ "components": { "migrations": "migrations.json" } }
You can then define your own migrations using migrations.json
in each component:
{ "my_migration_1": "\\My\\Migration" }
Configure your database connection, optionally through a helper function db()
(see also openclerk/config project):
use \Openclerk\Config; function db() { return new \Db\SoloConnection( Config::get("database_name"), Config::get("database_username"), Config::get("database_password") ); }
Load them up and optionally install them at runtime:
$logger = new \Monolog\Logger('name'); class AllMigrations extends \Db\Migration { function getParents() { return array(new Db\BaseMigration()) + DiscoveredComponents\Migrations::getAllInstances(); } } $migrations = new AllMigrations(db()); if ($migrations->hasPending(db())) { $migrations->install(db(), $logger); }
Migrations can be discovered and loaded at runtime with a migrations.json
.
You can also generate migrations at runtime, for example generating a table for each Currency discovered at runtime.
Replication
You can also define replication connections which are selected based on the type of query, and whether that table has recently been updated in the current $_SESSION:
function db() { return new \Db\ReplicatedConnection( Config::get("database_host_master"), Config::get("database_host_slave"), Config::get("database_name"), Config::get("database_username"), Config::get("database_password") ); }
Events
A number of events are triggered by the library, and can be captured for metrics:
db_prepare_start
,db_prepare_end
,db_prepare_master
,db_prepare_slave
db_execute_start
,db_execute_end
db_fetch_start
,db_fetch_end
db_fetch_all_start
,db_fetch_all_end