Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value of string like field can be empty string "" or "null" #626

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sql/mysql/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (i *inspect) addColumn(s *schema.Schema, rows *sql.Rows) error {
if err := i.extraAttr(t, c, extra.String); err != nil {
return err
}
if sqlx.ValidString(defaults) {
if defaults.Valid {
if i.mariadb() {
c.Default = i.marDefaultExpr(c, defaults.String)
} else {
Expand Down Expand Up @@ -573,6 +573,10 @@ func isHex(x string) bool { return len(x) > 2 && strings.ToLower(x[:2]) == "0x"

// marDefaultExpr returns the correct schema.Expr based on the column attributes for MariaDB.
func (i *inspect) marDefaultExpr(c *schema.Column, x string) schema.Expr {
// Unlike MySQL, NULL means default to NULL or no default.
if x == "NULL" {
return nil
}
// From MariaDB 10.2.7, string-based literals are quoted to distinguish them from expressions.
if i.gteV("10.2.7") && sqlx.IsQuoted(x, '\'') {
return &schema.Literal{V: x}
Expand Down
2 changes: 1 addition & 1 deletion sql/postgres/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (i *inspect) addColumn(s *schema.Schema, rows *sql.Rows) error {
typtype: typtype.String,
typid: typid.Int64,
})
if sqlx.ValidString(defaults) {
if defaults.Valid {
c.Default = defaultExpr(c, defaults.String)
}
if identity.String == "YES" {
Expand Down
2 changes: 1 addition & 1 deletion sql/sqlite/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (i *inspect) addColumn(t *schema.Table, rows *sql.Rows) error {
if err != nil {
return err
}
if sqlx.ValidString(defaults) {
if defaults.Valid {
c.Default = defaultExpr(defaults.String)
}
// TODO(a8m): extract collation from 'CREATE TABLE' statement.
Expand Down