Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

The dialect for postgres get a wrong col name, it case the schema sync fail to work #1292

@yuanfan

Description

@yuanfan
func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
	args := []interface{}{tableName}
	s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1")
	if len(db.Schema) != 0 {
		args = append(args, db.Schema)
		s = s + " AND schemaname=$2"
	}
	db.LogSQL(s, args)

	rows, err := db.DB().Query(s, args...)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	indexes := make(map[string]*core.Index, 0)
	for rows.Next() {
		var indexType int
		var indexName, indexdef string
		var colNames []string
		err = rows.Scan(&indexName, &indexdef)
		if err != nil {
			return nil, err
		}
		indexName = strings.Trim(indexName, `" `)
		if strings.HasSuffix(indexName, "_pkey") {
			continue
		}
		if strings.HasPrefix(indexdef, "CREATE UNIQUE INDEX") {
			indexType = core.UniqueType
		} else {
			indexType = core.IndexType
		}
		cs := strings.Split(indexdef, "(")
		colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
		var isRegular bool
		if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
			newIdxName := indexName[5+len(tableName):]
			isRegular = true
			if newIdxName != "" {
				indexName = newIdxName
			}
		}

		index := &core.Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
		for _, colName := range colNames {
			index.Cols = append(index.Cols, strings.Trim(colName, `" `))
		}
		index.IsRegular = isRegular
		indexes[index.Name] = index
	}
	return indexes, nil
}

I found 2 problem atleast.

The first :

	if strings.HasSuffix(indexName, "_pkey") {
		continue
	}

the primary key indexName mybe named "primary", not always has stuffix "_pKey". I suggest add "|| strings.Contains(indexName", "primary" )" condition to handle this satuation.

The Second:

parse indexdef string to get col name, it just the string like this

CREATE UNIQUE INDEX users_pkey ON public.users USING btree (id)

but indexdef string maybe like this

CREATE UNIQUE INDEX "primary" ON xx.public.account (id ASC)
CREATE UNIQUE INDEX "UQE_account_acc_name" ON xx.public.account (acc_name ASC)
CREATE INDEX "IDX_account_acc_type" ON xx.public.account (acc_type ASC)

in this situation, the col name will be parse to "id ASC", not "id". it case schema sync get the wrong col name, then sync will be failed. I suggest to alter the code to compatible with this situation.

Normally, the dialect for postgres work good. these problems beacuse of I used the CockroachDB. they said compatible with Postgres protocol, but a little different. I have test the xorm work with CockroachDB. It work well, except schema sync. the cockroach have a lot of users, I hope the postgres dialect compatible with CockroachDB. Please handle this, like the GORM,provide the compatibility support. thinks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions