migrations icon indicating copy to clipboard operation
migrations copied to clipboard

Raise exception when there are migrations yet to be run

Open markstory opened this issue 7 years ago • 16 comments

From @aaronstillwell on June 6, 2017 17:46

This is a (multiple allowed):

  • [ ] bug

  • [x] enhancement

  • [ ] feature-discussion (RFC)

  • CakePHP Version: 3.x

  • Platform and Target: -

What you did

Developing with co-worker, co-worker creates a migration, commits, I pull changes, forget to migrate / don't realise a migration has been added.

What happened

Hitting errors as the development schema is not up to date. A small but noteworthy productivity hit.

What you expected to happen

If CakePHP were to raise an exception when running in development mode, this situation would be mostly avoided.

I'll be happy to personally file a PR for this feature. Happy to hear thoughts on whether this would be a wanted / desirable feature by other Cake developers.

Copied from original issue: cakephp/cakephp#10727

markstory avatar Jun 06 '17 19:06 markstory

This could be done if the migrations plugin included middleware during debug mode.

markstory avatar Jun 06 '17 19:06 markstory

@markstory That could be in the realm of possibility.

HavokInspiration avatar Jun 07 '17 06:06 HavokInspiration

@markstory interesting idea, I like that.

ravage84 avatar Jun 09 '17 12:06 ravage84

@markstory if you'd like to assign this issue to me I'll follow up with a PR in due course?

aaronstillwell avatar Jun 13 '17 13:06 aaronstillwell

Wow.. this would be a cool feature... Can you direct me to the right area when you say "included middleware", @markstory ? I would like to send a PR this weekend.

karmicdice avatar Aug 24 '19 05:08 karmicdice

@karmicdice when an application is in debug mode, the migrations plugin could add a middleware that checks for pending migrations and throws a helpful error if there are migrations that need to be run. This is similar to how rails works.

markstory avatar Aug 24 '19 21:08 markstory

Thanks, on it!

karmicdice avatar Aug 25 '19 08:08 karmicdice

Created a folder - config under migrations plugin....

tl;dr:

Do we support middleware in plugins? I hope we do. How do we invoke it? Tried calling it via bootstrap and changed the plugin load with a config 'bootstrap' => true, didn't work. Also, cannot find the docs. Traced the working of RoR and Laravel middleware via plugins. Can someone please suggest how we are doing it differently?

I really want this feature :)

karmicdice avatar Oct 30 '19 17:10 karmicdice

Do we support middleware in plugins? I really want this feature :)

don't worry , you have it :)

only add your middleware

namespace YourPluginName;

use Cake\Core\BasePlugin;
/**
 * Plugin class for CakePHP plugin collection.
 */
class Plugin extends BasePlugin
{
    public function middleware($middleware)
    {
       // add your middleware 
        return $middleware;
    }

https://github.com/cakephp/debug_kit/blob/325a2a653f72af24489026300e4dc3169f2e15f1/src/Plugin.php#L63-L70

saeideng avatar Oct 30 '19 23:10 saeideng

Yes, I did.

Sorry, actually I had a typo. It is working. The return must be MiddlewareQueue the Queue part

karmicdice avatar Oct 31 '19 08:10 karmicdice

@karmicdice middleware hook added to bake to generate it when create new plugin with bake

return types added to branch 4 only , yes if you are using cake/plugin/4 you have to add return types according to cake core

saeideng avatar Oct 31 '19 09:10 saeideng

@karmicdice Do you want to follow up on this still?

dereuromark avatar Mar 08 '22 17:03 dereuromark

@dereuromark Thanks for the bump. Yea. I had fix logic in my head. Will get this happening.

karmicdice avatar Apr 07 '22 20:04 karmicdice

Hi. What should be the canonical exception status? 500 doesn't seem to be helpful.

karmicdice avatar Jun 27 '22 17:06 karmicdice

@karmicdice In this case there is no fitting HTTP status code to reflect that issue, as it is a very internal one. So 500 Internal error isn't totally wrong.

But if we want to go with another one, 503 Service Unavailable could be one.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503

ravage84 avatar Jun 27 '22 18:06 ravage84

Now someone just has to implement the middleware to ship with the plugin, and people can use it as opt-in for their apps.

dereuromark avatar Feb 21 '24 06:02 dereuromark