Library to make migrations easy.
Requires PHP 8.0 or later. It is installable and autoloadable via Composer as genkgo/migrations.
To run the unit tests at the command line, issue vendor/bin/phpunit -c phpunit.xml. PHPUnit is required.
This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.
Create a migration file, define how to update and undo the changes in your application.
<?phpuseGenkgo\Migrations\AbstractMigration;
class migration_2014_11_13_11_55 extends AbstractMigration
{
publicfunctionup()
{
// your changes here
}
publicfunctiondown()
{
// undo changes here
}
}The adapter keeps the history of the already executed migrations. The current library comes with a MySQL and Sqlite adapter. To add a new adapter, create one yourself and issue a pull request. Set the adapter, create a new list (from a list of files in a directory) and execute.
<?phpusePDO;
useGenkgo\Migrations\Factory;
useGenkgo\Migrations\Adapters\PdoSqliteAdapter;
$adapter = newPdoSqliteAdapter(newPDO('sqlite::memory:'));
$factory = newFactory($adapter);
$directory = __DIR__.'/migrations';
$list = $this->factory->newListFromDirectory($directory);
$result = $list->migrate();You can inject your dependencies, e.g. some database abstraction layer like in the sample below.
<?phpusePDO;
useGenkgo\Migrations\Factory;
useGenkgo\Migrations\Adapters\PdoSqliteAdapter;
useVendor\DatabaseAbstractLayer\SchemaGenerator;
$adapter = newPdoSqliteAdapter(newPDO('sqlite::memory:'));
$factory = newFactory($adapter, function ($classname) {
returnnew$classname(newSchemaGenerator());
});And then use it the migration file.
<?phpuseGenkgo\Migrations\AbstractMigration;
useVendor\DatabaseAbstractLayer\SchemaGenerator;
class migration_2014_11_13_11_55 extends AbstractMigration
{
private$schema;
publicfunction__construct(SchemaGenerator$schema) {
$this->schema = $schema;
}
publicfunctionup()
{
// your changes here
}
publicfunctiondown()
{
// undo changes here
}
}The example below uses auto resolution of Aura.Di.
<?phpusePDO;
useGenkgo\Migrations\Factory;
useGenkgo\Migrations\Adapters\PdoSqliteAdapter;
$adapter = newPdoSqliteAdapter(newPDO('sqlite::memory:'));
$factory = newFactory($adapter, function ($classname) use ($di) {
return$di->newInstance($classname);
});You can have multiple migration lists, e.g. for each plugin one list. Just put the migration files in the right namespace. See example below.
<?phpnamespaceVendor\MyPlugin;
useGenkgo\Migrations\AbstractMigration;
class migration_2014_11_13_11_55 extends AbstractMigration
{
publicfunctionup () { }
publicfunctiondown () { }
}And then migrate.
$directory = __DIR__. '/migrations';
$list = $this->factory->newListFromDirectory($directory,'Vendor\\Plugin\\');
$list->migrate();- Found a bug? Please try to solve it yourself first and issue a pull request. If you are not able to fix it, at least give a clear description what goes wrong. We will have a look when there is time.
- Want to see a feature added, issue a pull request and see what happens. You could also file a bug of the missing feature and we can discuss how to implement it.