Skip to content

Commit

Permalink
Merge pull request #3 from coenjacobs/static-id-method
Browse files Browse the repository at this point in the history
Static id method on migrations to save memory
  • Loading branch information
coenjacobs authored Feb 5, 2019
2 parents b442f9c + aa8283b commit cf2c5ff
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Loggers take care of registering the migrations that have been run already. This

## Migration structure

All of the migrations are required to follow a specific format, being enforced by the `CoenJacobs\Migrator\Contracts\Migration.php` interface. This interface enforces your migration class to contain at least two methods: `up()` and `down()`. These two methods are used to run and rollback your migration.
All of the migrations are required to follow a specific format, being enforced by the `CoenJacobs\Migrator\Contracts\Migration.php` interface. This interface will force you to provide a static `id()` method, which will need to provide the unique identifier of the migration. The interface also enforces your migration class to contain the following two methods: `up()` and `down()`. These two methods are used to run and rollback your migration.

There is a helper `BaseMigration` available, to help with setting up the right variables for the migration, such as the Worker.

Expand All @@ -33,13 +33,13 @@ A basic migration example looks like this:
```php
<?php

use CoenJacobs\Migrator\Migrations\BaseMigration;

namespace YourPlugin\Migrations;

use CoenJacobs\Migrator\Migrations\BaseMigration;

class CreateTestTable extends BaseMigration
{
public function getId()
public static function id()
{
return 'yourplugin-1-test-table';
}
Expand Down Expand Up @@ -80,12 +80,12 @@ $worker = new WpdbWorker();
$migrator = new Handler($worker, new DatabaseLogger());
```

After that, the Handler is ready to accept new migrations to be added, before they can be run. Each Migration needs to be provided with a Worker class, again implementing the `CoenJacobs\Migrator\Contracts\Worker` interface, which is responsible for running the queries inside your Migration. You can pass a different Worker class to your Migration, than the one you have passed to the Handler, but you can also use the same:
After that, the Handler is ready to accept new migrations to be added, before they can be run. You pass the class as a string of the class name, where the class itself again implements the `CoenJacobs\Migrator\Contracts\Migration` interface:

```php
use YourPlugin\Migrations\CreateTestTable

$migrator->add('yourplugin', new CreateTestTable($worker));
$migrator->add('yourplugin', CreateTestTable::class);
```

The first parameter in the `$migration->add()` method should be a unique identifier for your plugin. The handler runs the migrations on a per plugin basis, using this unique identifier as the key to call the right migrations to be run.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface Migration
{
public function getId();
public static function id();
public function up();
public function down();
}
29 changes: 13 additions & 16 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(Worker $worker, Logger $logger)

private function setupCoreMigrations()
{
$this->add('core', new CreateMigrationsTable($this->worker));
$this->add('core', CreateMigrationsTable::class);

// From here on, the 'core' index as $plugin_key is a reserved name and
// can't be used by anyone else.
Expand All @@ -57,16 +57,16 @@ public function isReservedName($pluginKey)

/**
* @param string $pluginKey
* @param Migration $migration
* @param $migrationClassName
* @throws ReservedNameException
*/
public function add($pluginKey, Migration $migration)
public function add($pluginKey, $migrationClassName)
{
if ($this->isReservedName($pluginKey)) {
throw new ReservedNameException($pluginKey . ' is a reserved name and can not be used by implementations.');
}

$this->migrations[ $pluginKey ][] = $migration;
$this->migrations[ $pluginKey ][] = $migrationClassName;
}

/**
Expand All @@ -91,18 +91,16 @@ public function up($pluginKey)
$migrationsToRun = [];

// Add core migrations first
foreach ($this->migrations[ 'core' ] as $migration) {
/** @var $migration Migration */
if (! in_array($migration->getId(), $runMigrations)) {
$migrationsToRun['core'][] = $migration;
foreach ($this->migrations[ 'core' ] as $migrationClass) {
if (! in_array($migrationClass::id(), $runMigrations)) {
$migrationsToRun['core'][] = new $migrationClass($this->worker);
}
}

// Add added migrations for $pluginKey second
foreach ($this->migrations[ $pluginKey ] as $migration) {
/** @var $migration Migration */
if (! in_array($migration->getId(), $runMigrations)) {
$migrationsToRun[ $pluginKey ][] = $migration;
foreach ($this->migrations[ $pluginKey ] as $migrationClass) {
if (! in_array($migrationClass::id(), $runMigrations)) {
$migrationsToRun[ $pluginKey ][] = new $migrationClass($this->worker);
}
}

Expand Down Expand Up @@ -131,10 +129,9 @@ public function down($pluginKey)
$migrationsToRun = [];

// Add added migrations for $pluginKey second
foreach ($this->migrations[ $pluginKey ] as $migration) {
/** @var $migration Migration */
if (in_array($migration->getId(), $runMigrations)) {
$migrationsToRun[ $pluginKey ][] = $migration;
foreach ($this->migrations[ $pluginKey ] as $migrationClass) {
if (in_array($migrationClass::id(), $runMigrations)) {
$migrationsToRun[ $pluginKey ][] = new $migrationClass($this->worker);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Loggers/DatabaseLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DatabaseLogger extends BaseLogger
{
public function add($plugin_key, Migration $migration, $batch)
{
$id = $migration->getId();
$id = $migration->id();
$tableName = $this->worker->getPrefix() . 'migrator_migrations';

$batch = intval($batch);
Expand All @@ -20,7 +20,7 @@ public function add($plugin_key, Migration $migration, $batch)

public function remove($plugin_key, Migration $migration)
{
$id = $migration->getId();
$id = $migration->id();
$tableName = $this->worker->getPrefix() . 'migrator_migrations';
$query = "DELETE FROM $tableName (migration, plugin_key)
VALUES ('$id', '$plugin_key')";
Expand Down
2 changes: 1 addition & 1 deletion src/Migrations/CreateMigrationsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class CreateMigrationsTable extends BaseMigration
{
public function getId()
public static function id()
{
return 'migrator-1-migrations-table';
}
Expand Down

0 comments on commit cf2c5ff

Please sign in to comment.