Skip to content

Commit a389afa

Browse files
author
Sergey Podgornyy
committed
Allow empty string for default
1 parent c6e4278 commit a389afa

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ var migrations = []migrator.Migration{
4141
posts := migrator.Table{Name: "posts"}
4242

4343
posts.UniqueID("id")
44-
posts.Column("title", migrator.String{Precision: 64})
45-
posts.Column("content", migrator.Text{})
44+
posts.Varchar("title", 64)
45+
posts.Text("content", false)
4646
posts.Timestamps()
4747

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

6666
comments.UniqueID("id")
6767
comments.UUID("post_id", "", false)
68-
comments.Column("title", migrator.String{Precision: 64})
69-
comments.Column("content", migrator.Text{})
68+
comments.Varchar("name", 64)
69+
comments.Column("email", migrator.String{Default: "<nil>"})
70+
comments.Text("content", false)
7071
comments.Timestamps()
7172

7273
comments.Foreign("post_id", "id", "posts", "RESTRICT", "RESTRICT")

column.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,5 +549,9 @@ func buildDefaultForString(v string) string {
549549
return fmt.Sprintf(" DEFAULT %s", v)
550550
}
551551

552+
if v == "<empty>" || v == "<nil>" {
553+
v = ""
554+
}
555+
552556
return fmt.Sprintf(" DEFAULT '%s'", v)
553557
}

column_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,20 @@ func TestBuildDefaultForString(t *testing.T) {
578578
assert.Equal(t, want, got)
579579
})
580580

581+
t.Run("it builds default with empty string for <empty> value", func(t *testing.T) {
582+
got := buildDefaultForString("<empty>")
583+
want := " DEFAULT ''"
584+
585+
assert.Equal(t, want, got)
586+
})
587+
588+
t.Run("it builds default with empty string for <nil> value", func(t *testing.T) {
589+
got := buildDefaultForString("<nil>")
590+
want := " DEFAULT ''"
591+
592+
assert.Equal(t, want, got)
593+
})
594+
581595
t.Run("it builds normal default", func(t *testing.T) {
582596
got := buildDefaultForString("value")
583597
want := " DEFAULT 'value'"

0 commit comments

Comments
 (0)