Newer
Older
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Breyta is a **library** for database migrations. There are a lot of applications for database migrations but no
actual library (without any user interface).
## Trivia
I created this library after trying ruckusing-migrations. It's the only one that does not require any additional library
to provide a user interface and is mentioned in awesome-php. Unfortunately it seems to be not actively developed and
maintained. Also I'm missing some functionality and it is very old, not using PSR-2/4 and namespaces.
The name for this library comes again from the Icelandic language and means "to change".
## Concept
* A migration is a set of database statements
* A migration has to run in a transaction
* Logging is important and goes to the migration table
* No code generating from this library (pure SQL)
* No user interface (API only)
* Generate fancy output and support progress bars
## Installation
We only support and suggest using composer - everything else on your own risk.
```console
$ composer require tflori/breyta
```
## Usage
> This might change in the next days till version 1.
Migration Script:
```php
<?php
namespace Breyta\Migrations;
use Breyta\AbstractMigration;
class CreateAnimalsTable extends AbstractMigration
{
$this->exec('CREATE TABLE "users" (
"id" bigserial,
"name" character varying (255) NOT NULL,
PRIMARY KEY ("id")
)');
}
{
$this->exec('DROP TABLE "users"');
}
}
```
Control structure:
```php
namespace App\Cli\Commands;
class MigrateCommand extends AbstractCommand
{
/**
* Your database connection
* @var PDO
*/
protected $db;
public function handle()
{
$breyta = new Breyta\Migrations($this->db, '/path/to/migrations');
// register handler (optional)
$breyta->onStart([$this, 'start']);
$breyta->onBeginMigration([$this, 'beginMigration']);
$breyta->onBeforeExecution([$this, 'beforeExecution']);
$breyta->onAfterExecution([$this, 'afterExecution']);
$breyta->onFinishMigration([$this, 'finishMigration']);
$breyta->onFinish([$this, 'finish']);
// alternative: implement Breyta\ProgressInterface and register
$breyta->useProgress($this);
$breyta->migrate();