Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ type Table struct {
// Column represents a database column.
// TODO: add support for foreign keys.
type Column struct {
Name string
Type Type
NotNull bool
Ignored Ignored
Id string
AutoGen ddl.AutoGenCol
Name string
Type Type
NotNull bool
Ignored Ignored
Id string
AutoGen ddl.AutoGenCol
DefaultValue DefaultValue
}

// ForeignKey represents a foreign key.
Expand Down Expand Up @@ -122,6 +123,12 @@ type Ignored struct {
AutoIncrement bool
}

// DefaultValue represents a Default value.
type DefaultValue struct {
IsPresent bool
Value string
}

// Print converts ty to a string suitable for printing.
func (ty Type) Print() string {
s := ty.Name
Expand Down
42 changes: 35 additions & 7 deletions sources/mysql/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (isi InfoSchemaImpl) GetColumns(conv *internal.Conv, table common.SchemaAnd
// Nothing to do here -- these are all handled elsewhere.
}
}
ignored.Default = colDefault.Valid
ignored.Default = false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This appears incorrect. ignored.Default should reflect whether default values should be ignored, not always be false. Consider using !colDefault.Valid instead.

colId := internal.GenerateColumnId()
if colExtra.String == "auto_increment" {
sequence := createSequence(conv)
Expand All @@ -219,20 +219,48 @@ func (isi InfoSchemaImpl) GetColumns(conv *internal.Conv, table common.SchemaAnd
} else {
colAutoGen = ddl.AutoGenCol{}
}
defaultVal := schema.DefaultValue{
IsPresent: colDefault.Valid,
Value: "",
}
if colDefault.Valid {
defaultVal.Value = sanitizeDefaultValue(colDefault.String)
}

c := schema.Column{
Id: colId,
Name: colName,
Type: toType(dataType, columnType, charMaxLen, numericPrecision, numericScale),
NotNull: common.ToNotNull(conv, isNullable),
Ignored: ignored,
AutoGen: colAutoGen,
Id: colId,
Name: colName,
Type: toType(dataType, columnType, charMaxLen, numericPrecision, numericScale),
NotNull: common.ToNotNull(conv, isNullable),
Ignored: ignored,
AutoGen: colAutoGen,
DefaultValue: defaultVal,
}
colDefs[colId] = c
colIds = append(colIds, colId)
}
return colDefs, colIds, nil
}

// sanitizeDefaultValue removes extra characters added to Default Value in information schema in MySQL.
// Example_1:
// Default Value: "concat('John', 'swamp', 'span')"
// Default Value in MySQL infoschema with extra characters: "concat(_utf8mb4\\'John\\',_utf8mb4\\'swamp\\',_utf8mb4\\'span\\')"
// Default Value after removing "_utf8mb4": "concat( \\'John\\', \\'swamp\\', \\'span\\')"
// Default Value after removing all backslashes: "concat('John','swamp','span')"
// Example_2:
// Default Value: "This is a message \nwith a newline\rand a carriage return."
// Default Value in MySQL infoschema with extra characters: "_utf8mb4\\'This is a \\tmessage \\\\nwith a newline\\\\\\'s and \\\\ra carriage return.\\'"
// Default Value after removing "_utf8mb4": " \\'This is a \\tmessage \\\\nwith a newline\\\\\\'s and \\\\ra carriage return.\\'"
// Default Value after removing all "\\\\": " \\'This is a \\tmessage \\nwith a newline\\\\'s and \\ra carriage return.\\'
// Default Value after removing all "\\'": "'This is a message \\nwith a newline\\'s \\rand a carriage return.'
func sanitizeDefaultValue(defaultValue string) string {
defaultValue = strings.ReplaceAll(defaultValue, "_utf8mb4", " ")
defaultValue = strings.ReplaceAll(defaultValue, "\\\\", "\\")
defaultValue = strings.ReplaceAll(defaultValue, "\\'", "'")
return defaultValue
}

// GetConstraints returns a list of primary keys and by-column map of
// other constraints. Note: we need to preserve ordinal order of
// columns in primary key constraints.
Expand Down
Loading