Skip to content

Commit dfc8aa2

Browse files
committed
Ignore not available columns when applying column types
The given `columnLists` may not contain all columns available in the given table. This patch prevents the code to fail. Before this patch the code was panicking whenever it was trying to set a value into a `nil` object, e.g. `columnList.GetColumn('non-existant').Type = SomeType`. For some reason the application was not completely failing but as a side-effect any column after the non-existant column would never get its Type properly set.
1 parent 32f2b06 commit dfc8aa2

1 file changed

Lines changed: 22 additions & 31 deletions

File tree

go/logic/inspect.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -552,44 +552,35 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
552552
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
553553
columnName := m.GetString("COLUMN_NAME")
554554
columnType := m.GetString("COLUMN_TYPE")
555-
if strings.Contains(columnType, "unsigned") {
556-
for _, columnsList := range columnsLists {
557-
columnsList.SetUnsigned(columnName)
555+
for _, columnsList := range columnsLists {
556+
column := columnsList.GetColumn(columnName)
557+
if column == nil {
558+
continue
558559
}
559-
}
560-
if strings.Contains(columnType, "mediumint") {
561-
for _, columnsList := range columnsLists {
562-
columnsList.GetColumn(columnName).Type = sql.MediumIntColumnType
560+
561+
if strings.Contains(columnType, "unsigned") {
562+
column.IsUnsigned = true
563563
}
564-
}
565-
if strings.Contains(columnType, "timestamp") {
566-
for _, columnsList := range columnsLists {
567-
columnsList.GetColumn(columnName).Type = sql.TimestampColumnType
564+
if strings.Contains(columnType, "mediumint") {
565+
column.Type = sql.MediumIntColumnType
568566
}
569-
}
570-
if strings.Contains(columnType, "datetime") {
571-
for _, columnsList := range columnsLists {
572-
columnsList.GetColumn(columnName).Type = sql.DateTimeColumnType
567+
if strings.Contains(columnType, "timestamp") {
568+
column.Type = sql.TimestampColumnType
573569
}
574-
}
575-
if strings.Contains(columnType, "json") {
576-
for _, columnsList := range columnsLists {
577-
columnsList.GetColumn(columnName).Type = sql.JSONColumnType
570+
if strings.Contains(columnType, "datetime") {
571+
column.Type = sql.DateTimeColumnType
578572
}
579-
}
580-
if strings.Contains(columnType, "float") {
581-
for _, columnsList := range columnsLists {
582-
columnsList.GetColumn(columnName).Type = sql.FloatColumnType
573+
if strings.Contains(columnType, "json") {
574+
column.Type = sql.JSONColumnType
583575
}
584-
}
585-
if strings.HasPrefix(columnType, "enum") {
586-
for _, columnsList := range columnsLists {
587-
columnsList.GetColumn(columnName).Type = sql.EnumColumnType
576+
if strings.Contains(columnType, "float") {
577+
column.Type = sql.FloatColumnType
588578
}
589-
}
590-
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
591-
for _, columnsList := range columnsLists {
592-
columnsList.SetCharset(columnName, charset)
579+
if strings.HasPrefix(columnType, "enum") {
580+
column.Type = sql.EnumColumnType
581+
}
582+
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
583+
column.Charset = charset
593584
}
594585
}
595586
return nil

0 commit comments

Comments
 (0)