davedevelopment / phpmig

Simple migrations system for php
Other
569 stars 92 forks source link

Callback during migration #67

Open clphillips opened 9 years ago

clphillips commented 9 years ago

I'm thinking of adding callback support to the migration process. This would be especially useful in the API migration.

Why? There's no feedback during migration processing. This can leave users feeling as if the app has stalled when processing a significant number of migrations.

A callback that is executed after each migration is processed would allow feedback to be presented to the user on the progress.

Thoughts?

davedevelopment commented 9 years ago

Doesn't it print out that it's starting a migration, and then again when it's finished? Or do you mean some sort of ticker to show something is still happening as far as we know?

clphillips commented 9 years ago

Yes, it does print output, but that's not at all flexible. Think progress bar.

If we have a callback that is executed after each individual migration then we can completely customize the display to the user. Displaying progress bars, spinning icons, or whatever we want via CLI or GUI:


$api = new Phpmig\Api($container, $output);
$total = count($api->getMigrations($api->getVersion()));
$count = 0;

$callback = function($version) use (&$count, $total) {
    // Elaborate progress bar handler code here
    $count++;
}

$api->up($this->getVersion(), $callback);

Edit This would actually be a better interface, since up() is well aware of the current count and total number of migrations.

$callback = function($version, $total, $count) {
    // Elaborate progress bar handler code here
}
davedevelopment commented 9 years ago

I see what you mean. My preference would be for the observer, pub sub or mediator pattern.

clphillips commented 9 years ago

Right, observer would be much more flexible. How do you foresee attaching the observer? I think it's necessary to attach it to Phpmig\Api, given that it's really the only piece that is aware of the total number of migrations to process (although #66 is set to change that). Maybe #66 blocks this?

Some ideas:

$api = new Phpmig\Api($container, $output);
$progress_observer = new ObjectThatImplementsSplObserver();

$api->attachObserver($progress_observer);
$api->up($this->getVersion());

Or attach via container? I personally don't like the idea of passing a global container into a constructor, but that's a different discussion altogether.

$container['phpmig.observers'] = new \ArrayObject(new ObjectThatImplementsSplObserver());
$api = new Phpmig\Api($container, $output);

$api->up($this->getVersion());