Add db option to fix bug where tables with default values always trigger column migrations #91
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.