Skip to content

Commit 2204b96

Browse files
committed
Gorm.
1 parent b46f480 commit 2204b96

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

gormlite/ddlmod.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ func (d *ddl) renameTable(dst, src string) error {
209209
return nil
210210
}
211211

212+
func compileConstraintRegexp(name string) *regexp.Regexp {
213+
return regexp.MustCompile("^(?i:CONSTRAINT)\\s+[\"`]?" + regexp.QuoteMeta(name) + "[\"`\\s]")
214+
}
215+
212216
func (d *ddl) addConstraint(name string, sql string) {
213-
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
217+
reg := compileConstraintRegexp(name)
214218

215219
for i := 0; i < len(d.fields); i++ {
216220
if reg.MatchString(d.fields[i]) {
@@ -223,7 +227,7 @@ func (d *ddl) addConstraint(name string, sql string) {
223227
}
224228

225229
func (d *ddl) removeConstraint(name string) bool {
226-
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
230+
reg := compileConstraintRegexp(name)
227231

228232
for i := 0; i < len(d.fields); i++ {
229233
if reg.MatchString(d.fields[i]) {
@@ -236,7 +240,7 @@ func (d *ddl) removeConstraint(name string) bool {
236240

237241
//lint:ignore U1000 ignore unused code.
238242
func (d *ddl) hasConstraint(name string) bool {
239-
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
243+
reg := compileConstraintRegexp(name)
240244

241245
for _, f := range d.fields {
242246
if reg.MatchString(f) {

gormlite/ddlmod_parse_all_columns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func parseAllColumns(in string) ([]string, error) {
9595
}
9696
return nil, fmt.Errorf("unexpected token: %s", string(s[i]))
9797
case parseAllColumnsState_State_End:
98-
break
98+
continue // avoid SA4011
9999
}
100100
}
101101
if state != parseAllColumnsState_State_End {

gormlite/ddlmod_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,41 @@ func TestRemoveConstraint(t *testing.T) {
313313
success: true,
314314
expect: []string{"`id` integer NOT NULL"},
315315
},
316+
{
317+
name: "lowercase",
318+
fields: []string{"`id` integer NOT NULL", "constraint `fk_users_notes` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"},
319+
cName: "fk_users_notes",
320+
success: true,
321+
expect: []string{"`id` integer NOT NULL"},
322+
},
323+
{
324+
name: "mixed_case",
325+
fields: []string{"`id` integer NOT NULL", "cOnsTraiNT `fk_users_notes` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"},
326+
cName: "fk_users_notes",
327+
success: true,
328+
expect: []string{"`id` integer NOT NULL"},
329+
},
330+
{
331+
name: "newline",
332+
fields: []string{"`id` integer NOT NULL", "CONSTRAINT `fk_users_notes`\nFOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"},
333+
cName: "fk_users_notes",
334+
success: true,
335+
expect: []string{"`id` integer NOT NULL"},
336+
},
337+
{
338+
name: "lots_of_newlines",
339+
fields: []string{"`id` integer NOT NULL", "constraint \n fk_users_notes \n FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"},
340+
cName: "fk_users_notes",
341+
success: true,
342+
expect: []string{"`id` integer NOT NULL"},
343+
},
344+
{
345+
name: "no_backtick",
346+
fields: []string{"`id` integer NOT NULL", "CONSTRAINT fk_users_notes FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"},
347+
cName: "fk_users_notes",
348+
success: true,
349+
expect: []string{"`id` integer NOT NULL"},
350+
},
316351
{
317352
name: "check",
318353
fields: []string{"CONSTRAINT `name_checker` CHECK (`name` <> 'thetadev')", "`id` integer NOT NULL"},

gormlite/update.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/error_translator.go"
1111
curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/migrator.go"
1212
curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite.go"
1313
curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite_test.go"
14-
curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite_test.go"
1514
curl -#L "https://github.com/glebarez/sqlite/raw/v1.11.0/sqlite_error_translator_test.go" > error_translator_test.go

0 commit comments

Comments
 (0)