Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db option to fix bug where tables with default values always trigger column migrations #91

Merged
merged 6 commits into from
Apr 11, 2023

Conversation

maggie-lou
Copy link

@maggie-lou maggie-lou commented Mar 9, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

There is a bug where if you use Clickhouse-Gorm to create a table with no default value, auto-migrate will always trigger a column migration even if there are no schema changes.

In order to stop doing these unnecessary column migrations, this code adds an option to consider empty string as an invalid default value.

User Case Description

If you create a Clickhouse table with Gorm auto-migrate and do not set a default value on a column, it will initialize the default value to the empty string. This is because Clickhouse columns are not nullable by default.

If you then run auto-migrate again, you would not expect a migration to take place, because you have not changed the schema. There is a bug because it will always run ALTER TABLE X MODIFY COLUMN

The problem is that the clickhouse operator reads the empty string as a valid default value, and sets DefaultValueValue.Valid = true. In gorm, this means that dvNotNull = true.

However because there's no default value set on the struct, gorm thinks there is not a default value and sets currentDefaultNotNull = false.

This causes us to always hit this case where Gorm thinks we are migrating from having no default value to empty string default value. It will then always set alterColumn=true and trigger unnecessary alter column calls.

@maggie-lou
Copy link
Author

@jinzhu Could you please take a look at this small change? Thank you!

@maggie-lou maggie-lou changed the title Add option to not support empty string as default value Add db option to fix bug where tables with default values always trigger column migrations Mar 14, 2023
@maggie-lou
Copy link
Author

Hey @jinzhu wanted to make sure you saw this. Do you have any feedback on this small change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants