Phinx: Command [Create] doesn't work on single-letter words in camel case

Created on 7 Jul 2014  路  17Comments  路  Source: cakephp/phinx

eg) t_foo

$ phinx create TFoo

  [InvalidArgumentException]
  The migration class name "TFoo" is invalid. Please use CamelCase format.
bug

Most helpful comment

:+1: Ran into this issue too.

All 17 comments

Ran into the same issue. isValidPhinxClassName would need to updated

public static function isValidPhinxClassName($className)
    {
        return (bool) preg_match('/^([A-Z][A-Za-z0-9]+)+$/', $className);
    }

The regex should be:

^([A-Z][a-z0-9]*)+[a-z0-9]$

Then isValidPhinxClassName would be:

{
    return (bool) preg_match('/^([A-Z][a-z0-9]*)+[a-z0-9]$/', $className);
}

@tuannh99 This regex will match capital letter word also.

We can use this regex but I'm not sure.

^([A-Z]{1,2}[a-z0-9]+)+$

@73sl4 Re-look my regex, I updated it a bit.
If nobody has objection on that, I will make write several unit test cases as well as pull-request for that.

@tuannh99 Same issue with this regex. I even wrote the same regex before but then realised that it won't work. You can use http://regexr.com/ to test the regex.

@73sl4 : The above regex works with:

  • TFoo
  • CreateTableArticles
    ...

What is the case you said it failed?

Some thoughts/suggestions/ideas.

One issue is capitalised words (SQL, CSV, HTML, etc.) become single letter snake case.

So many different patterns would be useful. We have recently started using Jira for our project tracking. Creating a migration that included the ticket number.

We would want to be able to have a migration named something like DT396_MigrateSomething. Ideally, leaving the class as that, rather than changing it all would be ideal.

Now considering class names are validated by a static method, MAYBE (and this is just a random idea), who about allowing the method to be injected/overwritten/defined elsewhere/somehow.

So, as standard the validation is as is. And is updated only under very specific reasons/fixes.

But allow those users that require something a little more structured/different to define the naming convention for their team.

Whilst I love Phinx (my team has several outstanding feature PRs), there are certain areas where it doesn't fit. And having a reasonable mechanism to allow us to override the default utility methods would be great.

A possible implementation would be to have a alternative_utility_class parameter that we can supply in our configs that would be examined first for an appropriate method (so look for MyPhinxUtilUtil::isValidMigrationFilename() and if not found use PhinxUtilUtil::isValidPhinxClassName()

My preference is always to allow a user to extend the functionality by configuration rather than trying to support every possible request internally and becoming bloated. If an alternative validation is required, then allow me to supply the validation I want. (--template|--class being prime examples).

Do what is possible for the majority of users, but provide a simple mechanism to extend, enhance, replace elements where appropriate. Considering these are simple validations routines, I think these are fairly simple to implement.

I agree, it would be nice to be able to configure this. DT396_MigrateSomething would be a nice pattern as we use JIRA as well and that would be a better format.

also limiting the regex to just 2 caps in a row i don't think is wise.

TBH, i'm not even sure why the script snake cases the camel case for the file name, as a PSR standard you just use the same case as the class name for the file name. capital letters in a file name is not uncommon in php.

:+1: Ran into this issue too.

The filename is sanitised to snake_case is to allow case insensitive OSs to work appropriately. At a guess.

Anyone from above interested in writing test cases and sending a PR?

Why not this:

public static function isValidPhinxClassName($className)
{
    return (bool) preg_match('/^[A-Z][a-z0-9]*+(?:_?+[A-Z][a-z0-9]*+)*+$/', $className);
}

Less restrictive, so those JIRA pattern class names would be fine. You can still snake-case everything and just leave underscores there so FooSQL -> foo_s_q_l, Foo_Bar -> foo__bar, etc.

There is a feature in development that will allow for custom naming : https://github.com/cakephp/phinx/pull/1087

It doesn't seem to have had any love for a while though.

Custom naming would be nice, not choking on simple names would be better.

Did a PR get created for the regex update?

@dereuromark I got most of the way through one that would be backwards compatible. Haven't published anything though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BardiaAfshin picture BardiaAfshin  路  14Comments

saada picture saada  路  14Comments

aimfeld picture aimfeld  路  23Comments

joshribakoff picture joshribakoff  路  15Comments

jeremylivingston picture jeremylivingston  路  20Comments