(t *testing.T)
| 1873 | } |
| 1874 | |
| 1875 | func TestMigrateWithUniqueIndexAndUnique(t *testing.T) { |
| 1876 | const table = "unique_struct" |
| 1877 | |
| 1878 | checkField := func(model interface{}, fieldName string, unique bool, uniqueIndex string) { |
| 1879 | stmt := &gorm.Statement{DB: DB} |
| 1880 | err := stmt.Parse(model) |
| 1881 | if err != nil { |
| 1882 | t.Fatalf("%v: failed to parse schema, got error: %v", utils.FileWithLineNum(), err) |
| 1883 | } |
| 1884 | _ = stmt.Schema.ParseIndexes() |
| 1885 | field := stmt.Schema.LookUpField(fieldName) |
| 1886 | if field == nil { |
| 1887 | t.Fatalf("%v: failed to find column %q", utils.FileWithLineNum(), fieldName) |
| 1888 | } |
| 1889 | if field.Unique != unique { |
| 1890 | t.Fatalf("%v: %q column %q unique should be %v but got %v", utils.FileWithLineNum(), stmt.Schema.Table, fieldName, unique, field.Unique) |
| 1891 | } |
| 1892 | if field.UniqueIndex != uniqueIndex { |
| 1893 | t.Fatalf("%v: %q column %q uniqueIndex should be %v but got %v", utils.FileWithLineNum(), stmt.Schema, fieldName, uniqueIndex, field.UniqueIndex) |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | type ( // not unique |
| 1898 | UniqueStruct1 struct { |
| 1899 | Name string `gorm:"size:10"` |
| 1900 | } |
| 1901 | UniqueStruct2 struct { |
| 1902 | Name string `gorm:"size:20"` |
| 1903 | } |
| 1904 | ) |
| 1905 | checkField(&UniqueStruct1{}, "name", false, "") |
| 1906 | checkField(&UniqueStruct2{}, "name", false, "") |
| 1907 | |
| 1908 | type ( // unique |
| 1909 | UniqueStruct3 struct { |
| 1910 | Name string `gorm:"size:30;unique"` |
| 1911 | } |
| 1912 | UniqueStruct4 struct { |
| 1913 | Name string `gorm:"size:40;unique"` |
| 1914 | } |
| 1915 | ) |
| 1916 | checkField(&UniqueStruct3{}, "name", true, "") |
| 1917 | checkField(&UniqueStruct4{}, "name", true, "") |
| 1918 | |
| 1919 | type ( // uniqueIndex |
| 1920 | UniqueStruct5 struct { |
| 1921 | Name string `gorm:"size:50;uniqueIndex"` |
| 1922 | } |
| 1923 | UniqueStruct6 struct { |
| 1924 | Name string `gorm:"size:60;uniqueIndex"` |
| 1925 | } |
| 1926 | UniqueStruct7 struct { |
| 1927 | Name string `gorm:"size:70;uniqueIndex:idx_us6_all_names"` |
| 1928 | NickName string `gorm:"size:70;uniqueIndex:idx_us6_all_names"` |
| 1929 | } |
| 1930 | ) |
| 1931 | checkField(&UniqueStruct5{}, "name", false, "idx_unique_struct5_name") |
| 1932 | checkField(&UniqueStruct6{}, "name", false, "idx_unique_struct6_name") |
nothing calls this directly
no test coverage detected