fluentmigrator / fluentmigrator

Fluent migrations framework for .NET
https://fluentmigrator.github.io
Apache License 2.0
3.27k stars 658 forks source link

Incorrect syntax for renaming a column with a default method in MySQL #1911

Open isaaclyman opened 1 month ago

isaaclyman commented 1 month ago

Describe the bug When attempting to rename a column in MySQL 8, if that column has an expression default value (like CURRENT_TIMESTAMP), Fluent Migrator incorrectly includes the cosmetic information DEFAULT_GENERATED in the query, which causes a MySql exception.

To Reproduce

Migration:

Create.Table("my_table").WithColumn("my_column").AsDateTime().NotNullable()
  .WithDefault(SystemMethods.CurrentDateTime);

Rename.Column("my_column").OnTable("my_table").To("anything");

Generated SQL:

ALTER TABLE `my_table` CHANGE `my_column` `anything` datetime NOT NULL DEFAULT `CURRENT_TIMESTAMP` DEFAULT_GENERATED

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT_GENERATED' at line 1

Expected behavior I would expect the rename to ignore the DEFAULT_GENERATED and generate a correct query.

Information (please complete the following information):

Additional context The Phinx migration library for PHP faced a similar issue: https://github.com/cakephp/phinx/pull/2253

jzabroski commented 1 month ago

https://github.com/fluentmigrator/fluentmigrator/blob/6fe6395fd3fd9e933e5f49f648aa9bef82ddc55f/src/FluentMigrator.Runner.MySql/Generators/MySql/MySql4Generator.cs#L101-L104

You would need to copy-paste this and add it to the Mysql8Generator, and update the test for Mysql8Generator to not include this.

The interface is here: https://github.com/fluentmigrator/fluentmigrator/blob/main/test/FluentMigrator.Tests/Unit/Generators/BaseColumnTests.cs#L41-L42

You need to add a new stub tests for đź‘Ť

        public abstract void CanRenameColumnWithCustomSchemaAndDefaultValue();
        public abstract void CanRenameColumnWithDefaultSchemaAndDefaultValue();

The test should be updated here: https://github.com/fluentmigrator/fluentmigrator/blob/6fe6395fd3fd9e933e5f49f648aa9bef82ddc55f/test/FluentMigrator.Tests/Unit/Generators/MySql4/MySql4ColumnTests.cs#L215-L233

For all other generators you can have the tests call the other test case, e.g.

public override void CanRenameColumnWithCustomSchemaAndDefaultValue()
{
    CanRenameColumnWithCustomSchema();
}
isaaclyman commented 1 month ago

I've been hoping to have time to submit a fix, but I just don't think it's likely. If someone else is available, feel free to pick this one up.

jzabroski commented 1 month ago

Thank you for getting back!