Explorar o código

fix excute GetIndexes error with sepcial postgresql index

xormplus %!s(int64=6) %!d(string=hai) anos
pai
achega
720ddff194
Modificáronse 2 ficheiros con 50 adicións e 2 borrados
  1. 14 2
      dialect_postgres.go
  2. 36 0
      dialect_postgres_test.go

+ 14 - 2
dialect_postgres.go

@@ -1108,6 +1108,19 @@ func (db *postgres) GetTables() ([]*core.Table, error) {
 	return tables, nil
 	return tables, nil
 }
 }
 
 
+
+func getIndexColName(indexdef string) []string {
+	var colNames []string
+
+	cs := strings.Split(indexdef, "(")
+	for _, v := range strings.Split(strings.Split(cs[1], ")")[0], ",") {
+		colNames = append(colNames, strings.Split(strings.TrimLeft(v, " "), " ")[0])
+	}
+
+	return colNames
+}
+
+
 func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
 func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
 	args := []interface{}{tableName}
 	args := []interface{}{tableName}
 	s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1")
 	s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1")
@@ -1141,8 +1154,7 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error)
 		} else {
 		} else {
 			indexType = core.IndexType
 			indexType = core.IndexType
 		}
 		}
-		cs := strings.Split(indexdef, "(")
-		colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
+		colNames = getIndexColName(indexdef)
 		var isRegular bool
 		var isRegular bool
 		if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
 		if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
 			newIdxName := indexName[5+len(tableName):]
 			newIdxName := indexName[5+len(tableName):]

+ 36 - 0
dialect_postgres_test.go

@@ -5,6 +5,7 @@ import (
 	"testing"
 	"testing"
 
 
 	"github.com/jackc/pgx/stdlib"
 	"github.com/jackc/pgx/stdlib"
+	"github.com/stretchr/testify/assert"
 	"github.com/xormplus/core"
 	"github.com/xormplus/core"
 )
 )
 
 
@@ -31,6 +32,7 @@ func TestParsePostgres(t *testing.T) {
 
 
 	for _, test := range tests {
 	for _, test := range tests {
 		uri, err := driver.Parse("postgres", test.in)
 		uri, err := driver.Parse("postgres", test.in)
+
 		if err != nil && test.valid {
 		if err != nil && test.valid {
 			t.Errorf("%q got unexpected error: %s", test.in, err)
 			t.Errorf("%q got unexpected error: %s", test.in, err)
 		} else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
 		} else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
@@ -83,3 +85,37 @@ func TestParsePgx(t *testing.T) {
 	}
 	}
 
 
 }
 }
+
+func TestGetIndexColName(t *testing.T) {
+	t.Run("Index", func(t *testing.T) {
+		s := "CREATE INDEX test2_mm_idx ON test2 (major);"
+		colNames := getIndexColName(s)
+		assert.Equal(t, []string{"major"}, colNames)
+	})
+
+	t.Run("Multicolumn indexes", func(t *testing.T) {
+		s := "CREATE INDEX test2_mm_idx ON test2 (major, minor);"
+		colNames := getIndexColName(s)
+		assert.Equal(t, []string{"major", "minor"}, colNames)
+	})
+
+	t.Run("Indexes and ORDER BY", func(t *testing.T) {
+		s := "CREATE INDEX test2_mm_idx ON test2 (major  NULLS FIRST, minor DESC NULLS LAST);"
+		colNames := getIndexColName(s)
+		assert.Equal(t, []string{"major", "minor"}, colNames)
+	})
+
+	t.Run("Combining Multiple Indexes", func(t *testing.T) {
+		s := "CREATE INDEX test2_mm_cm_idx ON public.test2 USING btree (major, minor) WHERE ((major <> 5) AND (minor <> 6))"
+		colNames := getIndexColName(s)
+		assert.Equal(t, []string{"major", "minor"}, colNames)
+	})
+
+	t.Run("unique", func(t *testing.T) {
+		s := "CREATE UNIQUE INDEX test2_mm_uidx ON test2 (major);"
+		colNames := getIndexColName(s)
+		assert.Equal(t, []string{"major"}, colNames)
+	})
+
+	t.Run("Indexes on Expressions", func(t *testing.T) {})
+}