mirror of
https://github.com/ent/ent.git
synced 2026-05-06 01:20:56 +03:00
dialect/sql/schema: skip default value parsing on pg functions (#951)
Closes #934
This commit is contained in:
@@ -251,7 +251,7 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
|
||||
c.Type = field.TypeUUID
|
||||
}
|
||||
switch {
|
||||
case !defaults.Valid || c.Type == field.TypeTime:
|
||||
case !defaults.Valid || c.Type == field.TypeTime || seqfunc(defaults.String):
|
||||
return nil
|
||||
case strings.Contains(defaults.String, "::"):
|
||||
parts := strings.Split(defaults.String, "::")
|
||||
@@ -447,3 +447,13 @@ func (d *Postgres) alterColumns(table string, add, modify, drop []*Column) sql.Q
|
||||
}
|
||||
return sql.Queries{b}
|
||||
}
|
||||
|
||||
// seqfunc reports if the given string is a sequence function.
|
||||
func seqfunc(defaults string) bool {
|
||||
for _, fn := range [...]string{"currval", "lastval", "setval", "nextval"} {
|
||||
if strings.HasPrefix(defaults, fn+"(") && strings.HasSuffix(defaults, ")") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -175,6 +175,32 @@ func TestPostgres_Create(t *testing.T) {
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "scan table with default set to serial",
|
||||
tables: []*Table{
|
||||
{
|
||||
Name: "users",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
PrimaryKey: []*Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
before: func(mock pgMock) {
|
||||
mock.start("120000")
|
||||
mock.tableExists("users", true)
|
||||
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM INFORMATION_SCHEMA.COLUMNS WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
|
||||
WithArgs("users").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
|
||||
AddRow("id", "bigint", "NO", "nextval('users_colname_seq'::regclass)"))
|
||||
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
|
||||
AddRow("users_pkey", "id", "t", "t", 0))
|
||||
mock.ExpectCommit()
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "add column to table",
|
||||
tables: []*Table{
|
||||
|
||||
Reference in New Issue
Block a user