Skip to content

Fix field default value quoting and add column comment support#90

Merged
hidu merged 1 commit intohidu:masterfrom
metowolf:fix/field-default-value-quoting
Oct 29, 2025
Merged

Fix field default value quoting and add column comment support#90
hidu merged 1 commit intohidu:masterfrom
metowolf:fix/field-default-value-quoting

Conversation

@metowolf
Copy link
Copy Markdown
Contributor

Summary

This PR fixes two critical issues in schema synchronization:

  1. Default value quoting: String type fields (varchar, char, text, etc.) now have their default values properly quoted in DDL statements
  2. Column comment support: Column comments are now included in schema comparison and synchronization

Changes

Core Improvements

  • Add needsQuotedDefault() method to FieldInfo to determine if a field type requires quoted default values
  • Fix default value formatting logic in FieldInfo.String() to handle:
    • String types (char, varchar, text, blob, enum, set, json) with quoted defaults
    • Numeric types (int, tinyint, bigint, decimal, etc.) with unquoted defaults
    • Special keywords (CURRENT_TIMESTAMP, NULL) without quotes
  • Add ColumnComment field to FieldInfo struct
  • Update FieldInfo.Equals() to include column comment comparison
  • Query COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS
  • Escape single quotes in comments properly

Test Coverage

  • Add comprehensive test cases for various field types with default values:
    • varchar/char with string defaults
    • text with string defaults
    • int/tinyint with numeric defaults
    • timestamp with CURRENT_TIMESTAMP
  • All existing tests pass

Problem Statement

Before this fix:

-- Incorrect: string default value without quotes
ALTER TABLE `users` MODIFY COLUMN `status` varchar(32) NOT NULL DEFAULT queue;
-- This would cause SQL syntax error

After this fix:

-- Correct: string default value properly quoted
ALTER TABLE `users` MODIFY COLUMN `status` varchar(32) NOT NULL DEFAULT 'queue';

Additionally, column comments were completely ignored during schema comparison, causing synchronization to miss comment differences between source and target databases.

Testing

Tested with:

  • MySQL 5.7, 8.0
  • Various field types with different default value scenarios
  • Fields with and without column comments

Related Issues

This fixes the issue where schema sync would generate invalid SQL statements for fields with string default values, and addresses the missing column comment synchronization feature.

🤖 Generated with Claude Code

- Add needsQuotedDefault() method to determine if field type requires quoted default values
- Fix default value formatting for string types (char, varchar, text, etc.)
- Add ColumnComment field to FieldInfo struct and include in schema comparison
- Update field comparison to check column comments
- Add comprehensive test cases for various field types with default values
- Import slices package for type checking

This fixes issues where:
1. String type default values were not properly quoted in DDL statements
2. Column comments were not synced between source and target databases
3. Field comparison could miss comment differences

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@hidu hidu merged commit b570a31 into hidu:master Oct 29, 2025
1 check passed
@metowolf metowolf deleted the fix/field-default-value-quoting branch October 29, 2025 00:27
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.

2 participants