@@ -2,8 +2,10 @@ package tests_test
2
2
3
3
import (
4
4
"regexp"
5
+ "sync"
5
6
"testing"
6
7
8
+ "gorm.io/driver/postgres"
7
9
"gorm.io/gorm"
8
10
"gorm.io/gorm/schema"
9
11
"gorm.io/gorm/utils/tests"
@@ -172,3 +174,88 @@ func TestTableWithNamer(t *testing.T) {
172
174
t .Errorf ("Table with namer, got %v" , sql )
173
175
}
174
176
}
177
+
178
+ func TestPostgresTableWithIdentifierLength (t * testing.T ) {
179
+ if DB .Dialector .Name () != "postgres" {
180
+ return
181
+ }
182
+
183
+ type LongString struct {
184
+ ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"`
185
+ }
186
+
187
+ t .Run ("default" , func (t * testing.T ) {
188
+ db , _ := gorm .Open (postgres .Open (postgresDSN ), & gorm.Config {})
189
+ user , err := schema .Parse (& LongString {}, & sync.Map {}, db .Config .NamingStrategy )
190
+ if err != nil {
191
+ t .Fatalf ("failed to parse user unique, got error %v" , err )
192
+ }
193
+
194
+ constraints := user .ParseUniqueConstraints ()
195
+ if len (constraints ) != 1 {
196
+ t .Fatalf ("failed to find unique constraint, got %v" , constraints )
197
+ }
198
+
199
+ for key := range constraints {
200
+ if len (key ) != 63 {
201
+ t .Errorf ("failed to find unique constraint, got %v" , constraints )
202
+ }
203
+ }
204
+ })
205
+
206
+ t .Run ("naming strategy" , func (t * testing.T ) {
207
+ db , _ := gorm .Open (postgres .Open (postgresDSN ), & gorm.Config {
208
+ NamingStrategy : schema.NamingStrategy {},
209
+ })
210
+
211
+ user , err := schema .Parse (& LongString {}, & sync.Map {}, db .Config .NamingStrategy )
212
+ if err != nil {
213
+ t .Fatalf ("failed to parse user unique, got error %v" , err )
214
+ }
215
+
216
+ constraints := user .ParseUniqueConstraints ()
217
+ if len (constraints ) != 1 {
218
+ t .Fatalf ("failed to find unique constraint, got %v" , constraints )
219
+ }
220
+
221
+ for key := range constraints {
222
+ if len (key ) != 63 {
223
+ t .Errorf ("failed to find unique constraint, got %v" , constraints )
224
+ }
225
+ }
226
+ })
227
+
228
+ t .Run ("namer" , func (t * testing.T ) {
229
+ uname := "custom_unique_name"
230
+ db , _ := gorm .Open (postgres .Open (postgresDSN ), & gorm.Config {
231
+ NamingStrategy : mockUniqueNamingStrategy {
232
+ UName : uname ,
233
+ },
234
+ })
235
+
236
+ user , err := schema .Parse (& LongString {}, & sync.Map {}, db .Config .NamingStrategy )
237
+ if err != nil {
238
+ t .Fatalf ("failed to parse user unique, got error %v" , err )
239
+ }
240
+
241
+ constraints := user .ParseUniqueConstraints ()
242
+ if len (constraints ) != 1 {
243
+ t .Fatalf ("failed to find unique constraint, got %v" , constraints )
244
+ }
245
+
246
+ for key := range constraints {
247
+ if key != uname {
248
+ t .Errorf ("failed to find unique constraint, got %v" , constraints )
249
+ }
250
+ }
251
+ })
252
+ }
253
+
254
+ type mockUniqueNamingStrategy struct {
255
+ UName string
256
+ schema.NamingStrategy
257
+ }
258
+
259
+ func (a mockUniqueNamingStrategy ) UniqueName (table , column string ) string {
260
+ return a .UName
261
+ }
0 commit comments