Skip to main content

Installation

composer require dirthara/schema

Requirements

RequirementWhy
PHP 8.5The package uses asymmetric property visibility and interface properties.
ext-pdoStatements are executed through the connection from dirthara/database.
dirthara/database ^0.1Owns the connections, drivers and PDO handling this package compiles for.

Each database also needs its own PDO extension. Install only the ones you use:

DatabaseExtension
MySQLpdo_mysql
PostgreSQLpdo_pgsql
SQLitepdo_sqlite
SQL Serverpdo_sqlsrv

Wiring it up

A Schema needs two things: the Database it should run statements on, and a resolver that knows which grammar belongs to which driver.

use Dirthara\Schema\Schema;
use Dirthara\Schema\Grammar\SchemaGrammarResolver;
use Dirthara\Schema\Grammar\MySqlSchemaGrammar;
use Dirthara\Schema\Grammar\SQLiteSchemaGrammar;
use Dirthara\Database\Connection\Driver\DriverName;

$grammars = new SchemaGrammarResolver([
DriverName::SQLite->value => new SQLiteSchemaGrammar(),
DriverName::MySql->value => new MySqlSchemaGrammar(),
]);

$schema = new Schema($database, $grammars);

Register only the grammars you need. Asking for a driver that has none throws UnsupportedDriverException, which is a clearer failure than compiling the wrong dialect.

tip

A framework integration would build this once and hand the Schema to application code. Nothing here holds a connection, so building it early costs nothing.

See Schema and connections for what Schema exposes and how it picks a connection.