Maestro is a PHP library designed to facilitate the creation and management of PostgreSQL database schemas and tables. It allows developers to define schemas and tables using PHP classes, providing a clear and structured way to manage database definitions and relationships.
- Introduction
- Installation
- Schema and Table Classes
- Defining Schemas and Tables
- DatabaseManager
- Query Builders
- Examples
To install Maestro, use Composer:
composer require ilias/maestroMaestro uses abstract classes for schemas and tables. Developers extend these classes to define their own schemas and tables.
A schema class extends the Schema abstract class. It contains table attributes that are typed with the table classes.
A table class extends the Table abstract class. It can define columns as class properties, specifying their types and optional default values.
You can specify columns that should be unique by overriding the tableUniqueColumns method in your table class.
You can also define a column as unique by adding the @unique clause to the documentation of the attribute you want to make unique. This format will not work if the tableUniqueColumns method is overridden.
Columns can have default values. If a default value is a PostgreSQL function, it should be defined as a PostgresFunction type to ensure it is not quoted in the final SQL query.
The DatabaseManager class provides methods to create schemas, tables, and manage foreign key constraints.
The createSchema and createTable methods generate SQL queries to create schemas and tables. The createTablesForSchema method handles the creation of all tables within a schema and their foreign key constraints.
Foreign key constraints are added using ALTER TABLE statements after the tables are created.
The executeQuery method executes the generated SQL queries using a PDO instance.
Maestro provides query builder classes for common SQL operations: Select, Insert, Update, and Delete.
The Select class allows you to build and execute SELECT queries.
use Ilias\Maestro\Database\Select;
use Ilias\Maestro\Database\Connection;
$select = new Select(Connection::get());
$select->from(['u' => 'users'], ['u.id', 'u.name'])
->where(['u.active' => true])
->order('u.name', 'ASC')
->limit(10);
$sql = $select->getSql();
$params = $select->getParameters();The Insert class allows you to build and execute INSERT queries.
use Ilias\Maestro\Database\Insert;
use Ilias\Maestro\Database\Connection;
use Maestro\Example\User;
$user = new User('John Doe', '[email protected]', md5('password'), true, new Timestamp('now'));
$insert = new Insert(Connection::get());
$insert->into(User::class)
->values($user)
->returning(['id']);
$sql = $insert->getSql();
$params = $insert->getParameters();The Update class allows you to build and execute UPDATE queries.
use Ilias\Maestro\Database\Update;
use Ilias\Maestro\Database\Connection;
$update = new Update(Connection::get());
$update->table('users')
->set('name', 'Jane Doe')
->where(['id' => 1]);
$sql = $update->getSql();
$params = $update->getParameters();The Delete class allows you to build and execute DELETE queries.
use Ilias\Maestro\Database\Delete;
use Ilias\Maestro\Database\Connection;
$delete = new Delete(Connection::get());
$delete->from('users')
->where(['id' => 1]);
$sql = $delete->getSql();
$params = $delete->getParameters();<?php
namespace Maestro\Example;
use Ilias\Maestro\Abstract\Schema;
use Ilias\Maestro\Abstract\Table;
use Ilias\Maestro\Abstract\PostgresFunction;
use Ilias\Maestro\Types\Timestamp;
final class Hr extends Schema
{
public User $user;
}
final class User extends Table
{
public Hr $schema;
public string $username;
public string $email;
public string $password;
public Timestamp | PostgresFunction | string $createdIn = "CURRENT_TIMESTAMP";
public function compose(
string $username,
string $email,
string $password,
Timestamp $createdIn
) {
$this->username = $username;
$this->email = $email;
$this->password = $password;
$this->createdIn = $createdIn;
}
public static function tableUniqueColumns(): array
{
return ["username", "email"];
}
}Explanations:
final: Use the final directive declaring yourTable,SchemaandDatabaseclasses. This is the application's way of keeping track of the created entities.type: Declare all types of class attributes so that the application can better choose the equivalent data type from the database column.compose: The compose method is used to define the non-nullability of a database column. Add to the constructor arguments the columns that must not be null.default: To declare the default value, simply add an initial value to the class attribute.custom function: To use a postgres function as the default value, follow the previous step and add the following two typings:<current type> | PostgresFunction | stringto the attribute type, then the text added as a value will be used as a function.unique: Override the statictableUniqueColumnsmethod to return the names of unique columns. Alternatively, use the@uniqueclause in the attribute's documentation, but note this won't work iftableUniqueColumnsis overridden.
Your file should typically include the following variables:
- DB_SQL: The PHP data source driver name.
- DB_HOST: The hostname of your database server.
- DB_PORT: The port number on which your database server is running.
- DB_NAME: The name of the database you want to connect to.
- DB_USER: The username used to connect to the database.
- DB_PASS: The password used to connect to the database.
Here is what your .env file need to have:
DB_SQL=pgsql
DB_HOST=localhost
DB_PORT=5432
DB_NAME=maestrodb
DB_USER=postgres
DB_PASS=dbpass
<?php
require_once 'vendor/autoload.php';
use Ilias\Maestro\Database\DatabaseManager;
use Ilias\Maestro\Database\Connection;
use Maestro\Example\Hr;
use PDO;
// Initialize PDO with environment variables
$pdo = Connection::get();
// Initialize DatabaseManager
$dbManager = new DatabaseManager($pdo);
// Create schemas and tables based on the defined classes
$queries = $dbManager->createTablesForSchema(new Hr());
foreach ($queries as $query) {
$dbManager->executeQuery($pdo, $query);
}Maestro provides several commands to help you manage and synchronize your database schema. Here are the available commands:
Synchronizes the schema for a specific schema class. This command ensures that the database schema for the specified class matches the schema defined in your PHP code.
./vendor/bin/maestro sync-schema <SchemaClass>./vendor/bin/maestro sync-schema Maestro\\Example\\HrThis command will synchronize the Hr schema, ensuring that the database tables and columns for the Hr schema match the definitions in your PHP code.
Synchronizes the entire database schema for a specified database class. This command iterates through all the schemas defined in the database class and ensures that each schema in the database matches the schema defined in your PHP code.
./vendor/bin/maestro sync-database <DatabaseClass>./vendor/bin/maestro sync-database Maestro\\Example\\MaestroDbThis command will synchronize all the schemas defined in the MaestroDb class, ensuring that the database tables and columns for each schema match the definitions in your PHP code.