fix pg GetColumns missing comment #1949
|
@ -1044,12 +1044,13 @@ func (db *postgres) IsColumnExist(queryer core.Queryer, ctx context.Context, tab
|
||||||
|
|
||||||
func (db *postgres) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) {
|
func (db *postgres) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) {
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length,
|
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, description,
|
||||||
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
||||||
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
||||||
FROM pg_attribute f
|
FROM pg_attribute f
|
||||||
JOIN pg_class c ON c.oid = f.attrelid JOIN pg_type t ON t.oid = f.atttypid
|
JOIN pg_class c ON c.oid = f.attrelid JOIN pg_type t ON t.oid = f.atttypid
|
||||||
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
||||||
|
LEFT JOIN pg_description de ON f.attrelid=de.objoid AND f.attnum=de.objsubid
|
||||||
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||||
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
||||||
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
||||||
|
@ -1078,9 +1079,9 @@ WHERE n.nspname= s.table_schema AND c.relkind = 'r'::char AND c.relname = $1%s A
|
||||||
col.Indexes = make(map[string]int)
|
col.Indexes = make(map[string]int)
|
||||||
|
|
||||||
var colName, isNullable, dataType string
|
var colName, isNullable, dataType string
|
||||||
var maxLenStr, colDefault *string
|
var maxLenStr, colDefault, description *string
|
||||||
var isPK, isUnique bool
|
var isPK, isUnique bool
|
||||||
err = rows.Scan(&colName, &colDefault, &isNullable, &dataType, &maxLenStr, &isPK, &isUnique)
|
err = rows.Scan(&colName, &colDefault, &isNullable, &dataType, &maxLenStr, &description, &isPK, &isUnique)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -1126,6 +1127,10 @@ WHERE n.nspname= s.table_schema AND c.relkind = 'r'::char AND c.relname = $1%s A
|
||||||
col.DefaultIsEmpty = true
|
col.DefaultIsEmpty = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if description != nil {
|
||||||
|
col.Comment = *description
|
||||||
|
}
|
||||||
|
|
||||||
if isPK {
|
if isPK {
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -209,3 +209,39 @@ func TestDBVersion(t *testing.T) {
|
||||||
|
|
||||||
fmt.Println(testEngine.Dialect().URI().DBType, "version is", version)
|
fmt.Println(testEngine.Dialect().URI().DBType, "version is", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetColumns(t *testing.T) {
|
||||||
|
if testEngine.Dialect().URI().DBType != schemas.POSTGRES {
|
||||||
|
t.Skip()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type TestCommentStruct struct {
|
||||||
|
HasComment int
|
||||||
|
NoComment int
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(TestCommentStruct))
|
||||||
|
|
||||||
|
comment := "this is a comment"
|
||||||
|
sql := fmt.Sprintf("comment on column %s.%s is '%s'", testEngine.TableName(new(TestCommentStruct), true), "has_comment", comment)
|
||||||
|
_, err := testEngine.Exec(sql)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
tables, err := testEngine.DBMetas()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tableName := testEngine.GetColumnMapper().Obj2Table("TestCommentStruct")
|
||||||
|
var hasComment, noComment string
|
||||||
|
for _, table := range tables {
|
||||||
|
if table.Name == tableName {
|
||||||
|
col := table.GetColumn("has_comment")
|
||||||
|
assert.NotNil(t, col)
|
||||||
|
hasComment = col.Comment
|
||||||
|
col2 := table.GetColumn("no_comment")
|
||||||
|
assert.NotNil(t, col2)
|
||||||
|
noComment = col2.Comment
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(t, comment, hasComment)
|
||||||
|
assert.Zero(t, noComment)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user