Skip to content

Commit 397ec6f

Browse files
authored
Fix columnType.Unique() returns true for non-unique index DDL. (#154)
Signed-off-by: Jeff Ortel <[email protected]>
1 parent cf6cd11 commit 397ec6f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ddlmod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func parseDDL(strs ...string) (*ddl, error) {
162162
for _, column := range getAllColumns(matches[1]) {
163163
for idx, c := range result.columns {
164164
if c.NameValue.String == column {
165-
c.UniqueValue = sql.NullBool{Bool: true, Valid: true}
165+
c.UniqueValue = sql.NullBool{Bool: strings.ToUpper(strings.Fields(str)[1]) == "UNIQUE", Valid: true}
166166
result.columns[idx] = c
167167
}
168168
}

ddlmod_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ func TestParseDDL(t *testing.T) {
7979
},
8080
},
8181
},
82+
{
83+
"non-unique index",
84+
[]string{
85+
"CREATE TABLE `test-c` (`field` integer NOT NULL)",
86+
"CREATE INDEX `idx_uq` ON `test-b`(`field`) WHERE field = 0",
87+
},
88+
1,
89+
[]migrator.ColumnType{
90+
{
91+
NameValue: sql.NullString{String: "field", Valid: true},
92+
DataTypeValue: sql.NullString{String: "integer", Valid: true},
93+
ColumnTypeValue: sql.NullString{String: "integer", Valid: true},
94+
PrimaryKeyValue: sql.NullBool{Bool: false, Valid: true},
95+
UniqueValue: sql.NullBool{Bool: false, Valid: true},
96+
NullableValue: sql.NullBool{Bool: false, Valid: true},
97+
},
98+
},
99+
},
82100
}
83101

84102
for _, p := range params {

0 commit comments

Comments
 (0)