Repository: ariga/atlas
Pull request #492 (2022-01-25 15:57:03)

sql/mysql: fulltext indexes contain NULL collation


Patch:
diff --git a/sql/mysql/inspect.go b/sql/mysql/inspect.go
index ab7381d3..8cbe7b63 100644
--- a/sql/mysql/inspect.go
+++ b/sql/mysql/inspect.go
@@ -306,13 +306,13 @@ func (i *inspect) addIndexes(t *schema.Table, rows *sql.Rows) error {
 	)
 	for rows.Next() {
 		var (
-			nonuniq, desc                  bool
 			seqno                          int
 			name, indexType                string
+			nonuniq, desc                  sql.NullBool
 			column, subPart, expr, comment sql.NullString
 		)
 		if err := rows.Scan(&name, &column, &nonuniq, &seqno, &indexType, &desc, &comment, &subPart, &expr); err != nil {
-			return fmt.Errorf("mysql: scanning index: %w", err)
+			return fmt.Errorf("mysql: scanning indexes for table %q: %w", t.Name, err)
 		}
 		// Ignore primary keys.
 		if name == "PRIMARY" {
@@ -323,7 +323,7 @@ func (i *inspect) addIndexes(t *schema.Table, rows *sql.Rows) error {
 		if !ok {
 			idx = &schema.Index{
 				Name:   name,
-				Unique: !nonuniq,
+				Unique: !nonuniq.Bool,
 				Table:  t,
 				Attrs: []schema.Attr{
 					&IndexType{T: indexType},
@@ -339,7 +339,7 @@ func (i *inspect) addIndexes(t *schema.Table, rows *sql.Rows) error {
 		}
 		// Rows are ordered by SEQ_IN_INDEX that specifies the
 		// position of the column in the index definition.
-		part := &schema.IndexPart{SeqNo: seqno, Desc: desc}
+		part := &schema.IndexPart{SeqNo: seqno, Desc: desc.Bool}
 		switch {
 		case sqlx.ValidString(expr):
 			part.X = &schema.RawExpr{
diff --git a/sql/postgres/inspect.go b/sql/postgres/inspect.go
index aee5ec30..5bbeb3c5 100644
--- a/sql/postgres/inspect.go
+++ b/sql/postgres/inspect.go
@@ -360,7 +360,7 @@ func (i *inspect) addIndexes(t *schema.Table, rows *sql.Rows) error {
 			column, contype, pred, expr, comment sql.NullString
 		)
 		if err := rows.Scan(&name, &typ, &column, &primary, &uniq, &contype, &pred, &expr, &desc, &nullsfirst, &nullslast, &comment); err != nil {
-			return fmt.Errorf("postgres: scanning index: %w", err)
+			return fmt.Errorf("postgres: scanning indexes for table %q: %w", t.Name, err)
 		}
 		idx, ok := names[name]
 		if !ok {


Test patch:
diff --git a/sql/mysql/inspect_test.go b/sql/mysql/inspect_test.go
index 0dabbb9a..5422fb35 100644
--- a/sql/mysql/inspect_test.go
+++ b/sql/mysql/inspect_test.go
@@ -479,7 +479,7 @@ func TestDriver_InspectTable(t *testing.T) {
 +--------------+-------------+------------+--------------+--------------+---------+--------------+------------+------------------+
 | INDEX_NAME   | COLUMN_NAME | NON_UNIQUE | SEQ_IN_INDEX | INDEX_TYPE   | DESC    | COMMENT      | SUB_PART   | EXPRESSION       |
 +--------------+-------------+------------+--------------+--------------+---------+--------------+------------+------------------+
-| nickname     | nickname    |          0 |            1 | BTREE        | 0       |              |        255 |      NULL        |
+| nickname     | nickname    |          0 |            1 | BTREE        | nil     |              |        255 |      NULL        |
 | lower_nick   | NULL        |          1 |            1 | HASH         | 0       |              |       NULL | lower(nickname)  |
 | non_unique   | oid         |          1 |            1 | BTREE        | 0       |              |       NULL |      NULL        |
 | non_unique   | uid         |          1 |            2 | BTREE        | 0       |              |       NULL |      NULL        |