go-gorm / postgres

GORM PostgreSQL driver
MIT License
234 stars 123 forks source link

feat: add postgres index , impl Migrator GetIndexes func #113

Closed jimmyduang closed 2 years ago

jimmyduang commented 2 years ago

What did this pull request do?

Implement postgres Migrator GetIndexes func

User Case Description

jimmyduang commented 2 years ago

@jinzhu @qqxhb issue: https://github.com/go-gorm/gen/pull/484 https://github.com/go-gorm/gen/pull/482 https://github.com/go-gorm/gen/issues/470

jimmyduang commented 2 years ago

Can we use pg_class/pg_attribute tables to get index information instead of regexp matching? probably like

select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name,
    ix.indisunique as indisunique
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
    and t.relname like 'user%'

https://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql

OK,i will try

jimmyduang commented 2 years ago

@a631807682 done

jimmyduang commented 2 years ago

@a631807682 hi,bro. I just modify base of this pr https://github.com/go-gorm/mysql/pull/77/commits/b5cef9cfeba8a4d713b416c6a3d1bb2d513db0a3 . In order to keep the codes on both sides consistent, if it is necessary to strict, I will improve it

a631807682 commented 2 years ago

@a631807682 hi,bro. I just modify base of this pr go-gorm/mysql@b5cef9c . In order to keep the codes on both sides consistent, if it is necessary to strict, I will improve it

  1. Each driver only needs to be consistent through the interface
  2. https://github.com/go-gorm/mysql/commit/b5cef9cfeba8a4d713b416c6a3d1bb2d513db0a3 not work for me, it throw sql: converting argument $2 type: unsupported type mysql.Migrator, a struct
  3. We will usually be strict because every PR may be referenced, resulting in a cumulative impact of unnecessary code that affects efficiency
  4. We'd be happy if you'd like to create a PR to fix the mysql problem
jimmyduang commented 2 years ago

@a631807682 hi,bro. I just modify base of this pr go-gorm/mysql@b5cef9c . In order to keep the codes on both sides consistent, if it is necessary to strict, I will improve it

  1. Each driver only needs to be consistent through the interface
  2. go-gorm/mysql@b5cef9c not work for me, it throw sql: converting argument $2 type: unsupported type mysql.Migrator, a struct
  3. We will usually be strict because every PR may be referenced, resulting in a cumulative impact of unnecessary code that affects efficiency
  4. We'd be happy if you'd like to create a PR to fix the mysql problem

i see ,i will change my code,thx bro.

jimmyduang commented 2 years ago

@a631807682 done

a631807682 commented 2 years ago

I suggest removing the judgment here, because we will not have empty records, just like we do not need to judge whether there are empty values in the list after using list, err:=db.Find().Error

func groupByIndexName(indexList []*Index) map[string][]*Index {
    columnIndexMap := make(map[string][]*Index, len(indexList))
    for _, idx := range indexList {
        if idx == nil { // I suggest removing the judgment here
            continue
        }
        columnIndexMap[idx.IndexName] = append(columnIndexMap[idx.IndexName], idx)
    }
    return columnIndexMap
}

Others are fine for me.

Edit

Sorry for changing the review multiple times

jimmyduang commented 2 years ago

it‘s ok

jimmyduang commented 2 years ago

@a631807682 thx bro,please cr agin.

jimmyduang commented 2 years ago

sorry mistake request...

jinzhu commented 2 years ago

LGTM, thank you @jimmyduang @a631807682 for your awesome work.