Skip to content

Database layers

Database layers are ordered migration directories. Each directory contains a config.yml file and one or more scripts.

Directory structure

database-layers/
└── main/
    ├── 001-create-users/
    │   ├── config.yml
    │   └── 001.sql
    └── 002-create-projects/
        ├── config.yml
        └── 001.sql

The directory name is the layer name. Files beginning with . are ignored. Other scripts are executed in directory order returned by the filesystem.

Layer configuration

connection: default
depends:
  - 001-create-users
required-parameter:
  environment: production
no-execute: false

Supported keys:

  • connection: required connection name;
  • depends: list of dependencies;
  • required-parameter: expected entries in the parameter bag;
  • no-execute: disables the layer when true.

A dependency in another selector uses layer@selector.

Load and execute layers

use Small\SwooleEntityManager\Layers\LayerCollection;

$layers = (new LayerCollection())
    ->setConnectionFactory($connectionFactory)
    ->setParameterBag(['environment' => 'production'])
    ->loadPath('main', __DIR__ . '/database-layers/main');

$layers->execute();

Each driver stores executed layer names through its own layer manager. Execution stops with ExecutionFailedException when dependencies cannot be resolved.

Small Swoole DB layers execute PHP fixture scripts rather than SQL. Empty or missing scripts are rejected.

Next chapter: Transactions and persistence threads