phinx icon indicating copy to clipboard operation
phinx copied to clipboard

add fix for mariadb virtual column creation

Open oskarkregar opened this issue 2 years ago • 6 comments

When using virtual column definition with Literal class, migration fails with SQL syntax error on MariaDB as NOT NULL or NULL are not allowed at the end of column definition. Below is the link to an issue.

Fixes https://github.com/cakephp/phinx/issues/2157

oskarkregar avatar Dec 20 '22 13:12 oskarkregar

I'm not sure if it's overly reasonable to completely break the null option for all literal type usages with MariaDB, just because of this one quirks.

Maybe the check should rather look for SQL that is incompatible with [NOT] NULL, or maybe it should be possible to explicitly suppress generating [NOT] NULL?

In any case, this should have tests. Since existing tests didn't break, there seems to already be missing coverage.

ndm2 avatar Dec 20 '22 15:12 ndm2

We don't currently test against mariadb, so we'd need to wire that up before we could have a good test for this. MySQL does happily allow null and not null for virtual columns so I wouldn't expect our existing tests to be able to cover this.

MasterOdin avatar Dec 20 '22 18:12 MasterOdin

It could then be fixed with option to explicitly suppress null and not null as @ndm2 suggested. It is not needed on most MySQL and MariaDB column creation (except on TIMESTAMP type).

oskarkregar avatar Dec 20 '22 21:12 oskarkregar

Is there an argument to say that adding options to a Literal column type is unnecessary? By definition we are manually specifying the column and allowing options just adds complexity.

Then we might do;

        if (!$column->getType() instanceof Literal) {
            $def .= $column->getEncoding() ? ' CHARACTER SET ' . $column->getEncoding() : '';
            $def .= $column->getCollation() ? ' COLLATE ' . $column->getCollation() : '';
            $def .= !$column->isSigned() && isset($this->signedColumnTypes[$column->getType()]) ? ' unsigned' : '';
            $def .= $column->isNull() ? ' NULL' : ' NOT NULL';
        }

kyojin007 avatar Jul 18 '23 10:07 kyojin007

@ndm2 So you think we shouldn't merge this?

dereuromark avatar Sep 07 '23 14:09 dereuromark

@dereuromark Not in this state, no. IMHO there should either be an option to exclude [not] null generation, or there should be a clear way to define the whole column creation SQL via a Literal object. I would also second @MasterOdin that this should have an actual test on MariaDB.

ndm2 avatar Sep 07 '23 14:09 ndm2