Skip to content

Commit a1b7b09

Browse files
author
Sergey Podgornyy
committed
Introduce binary ID
1 parent dd13cf9 commit a1b7b09

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

table.go

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func (t *Table) UniqueID(name string) {
4242
t.Primary(name)
4343
}
4444

45+
// BinaryID adds unique binary id column (represented as UUID) that is primary key
46+
func (t *Table) BinaryID(name string) {
47+
t.Column(name, Binary{Fixed: true, Precision: 16, Default: "(UUID_TO_BIN(UUID()))"})
48+
t.Primary(name)
49+
}
50+
4551
// Boolean represented in DB as tinyint
4652
func (t *Table) Boolean(name string, def string) {
4753
// tinyint(1)

table_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ func TestUniqueIDColumn(t *testing.T) {
5252
assert.Equal(key{typ: "primary", columns: []string{"id"}}, table.indexes[0])
5353
}
5454

55+
func TestBinaryID(t *testing.T) {
56+
assert := assert.New(t)
57+
table := Table{}
58+
59+
assert.Nil(table.columns)
60+
assert.Len(table.indexes, 0)
61+
62+
table.BinaryID("id")
63+
64+
assert.Len(table.columns, 1)
65+
assert.Equal("id", table.columns[0].field)
66+
assert.Equal(Binary{Default: "(UUID_TO_BIN(UUID()))", Fixed: true, Precision: 16}, table.columns[0].definition)
67+
assert.Len(table.indexes, 1)
68+
assert.Equal(key{typ: "primary", columns: []string{"id"}}, table.indexes[0])
69+
}
70+
5571
func TestBooleanColumn(t *testing.T) {
5672
assert := assert.New(t)
5773
table := Table{}

0 commit comments

Comments
 (0)