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.
This commit is contained in:
Eduardo Nunes 2018-11-01 14:33:11 +01:00
parent 32f2b063d8
commit dfc8aa28f4

View File

@ -552,44 +552,35 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error { err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
columnName := m.GetString("COLUMN_NAME") columnName := m.GetString("COLUMN_NAME")
columnType := m.GetString("COLUMN_TYPE") columnType := m.GetString("COLUMN_TYPE")
if strings.Contains(columnType, "unsigned") { for _, columnsList := range columnsLists {
for _, columnsList := range columnsLists { column := columnsList.GetColumn(columnName)
columnsList.SetUnsigned(columnName) if column == nil {
continue
} }
}
if strings.Contains(columnType, "mediumint") { if strings.Contains(columnType, "unsigned") {
for _, columnsList := range columnsLists { column.IsUnsigned = true
columnsList.GetColumn(columnName).Type = sql.MediumIntColumnType
} }
} if strings.Contains(columnType, "mediumint") {
if strings.Contains(columnType, "timestamp") { column.Type = sql.MediumIntColumnType
for _, columnsList := range columnsLists {
columnsList.GetColumn(columnName).Type = sql.TimestampColumnType
} }
} if strings.Contains(columnType, "timestamp") {
if strings.Contains(columnType, "datetime") { column.Type = sql.TimestampColumnType
for _, columnsList := range columnsLists {
columnsList.GetColumn(columnName).Type = sql.DateTimeColumnType
} }
} if strings.Contains(columnType, "datetime") {
if strings.Contains(columnType, "json") { column.Type = sql.DateTimeColumnType
for _, columnsList := range columnsLists {
columnsList.GetColumn(columnName).Type = sql.JSONColumnType
} }
} if strings.Contains(columnType, "json") {
if strings.Contains(columnType, "float") { column.Type = sql.JSONColumnType
for _, columnsList := range columnsLists {
columnsList.GetColumn(columnName).Type = sql.FloatColumnType
} }
} if strings.Contains(columnType, "float") {
if strings.HasPrefix(columnType, "enum") { column.Type = sql.FloatColumnType
for _, columnsList := range columnsLists {
columnsList.GetColumn(columnName).Type = sql.EnumColumnType
} }
} if strings.HasPrefix(columnType, "enum") {
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" { column.Type = sql.EnumColumnType
for _, columnsList := range columnsLists { }
columnsList.SetCharset(columnName, charset) if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
column.Charset = charset
} }
} }
return nil return nil