Skip to content

Commit

Permalink
Allow empty string for default
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Podgornyy committed Aug 6, 2020
1 parent c6e4278 commit a389afa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var migrations = []migrator.Migration{
posts := migrator.Table{Name: "posts"}

posts.UniqueID("id")
posts.Column("title", migrator.String{Precision: 64})
posts.Column("content", migrator.Text{})
posts.Varchar("title", 64)
posts.Text("content", false)
posts.Timestamps()

s.CreateTable(posts)
Expand All @@ -65,8 +65,9 @@ var migrations = []migrator.Migration{

comments.UniqueID("id")
comments.UUID("post_id", "", false)
comments.Column("title", migrator.String{Precision: 64})
comments.Column("content", migrator.Text{})
comments.Varchar("name", 64)
comments.Column("email", migrator.String{Default: "<nil>"})
comments.Text("content", false)
comments.Timestamps()

comments.Foreign("post_id", "id", "posts", "RESTRICT", "RESTRICT")
Expand Down
4 changes: 4 additions & 0 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,5 +549,9 @@ func buildDefaultForString(v string) string {
return fmt.Sprintf(" DEFAULT %s", v)
}

if v == "<empty>" || v == "<nil>" {
v = ""
}

return fmt.Sprintf(" DEFAULT '%s'", v)
}
14 changes: 14 additions & 0 deletions column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ func TestBuildDefaultForString(t *testing.T) {
assert.Equal(t, want, got)
})

t.Run("it builds default with empty string for <empty> value", func(t *testing.T) {
got := buildDefaultForString("<empty>")
want := " DEFAULT ''"

assert.Equal(t, want, got)
})

t.Run("it builds default with empty string for <nil> value", func(t *testing.T) {
got := buildDefaultForString("<nil>")
want := " DEFAULT ''"

assert.Equal(t, want, got)
})

t.Run("it builds normal default", func(t *testing.T) {
got := buildDefaultForString("value")
want := " DEFAULT 'value'"
Expand Down

0 comments on commit a389afa

Please sign in to comment.