From 2204b96ff62042c2a76c144537ca4ab8cd83c5b1 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 27 May 2025 10:51:42 +0100 Subject: [PATCH] Gorm. --- gormlite/ddlmod.go | 10 +++++--- gormlite/ddlmod_parse_all_columns.go | 2 +- gormlite/ddlmod_test.go | 35 ++++++++++++++++++++++++++++ gormlite/update.sh | 1 - 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/gormlite/ddlmod.go b/gormlite/ddlmod.go index 471fd70..c56a2cf 100644 --- a/gormlite/ddlmod.go +++ b/gormlite/ddlmod.go @@ -209,8 +209,12 @@ func (d *ddl) renameTable(dst, src string) error { return nil } +func compileConstraintRegexp(name string) *regexp.Regexp { + return regexp.MustCompile("^(?i:CONSTRAINT)\\s+[\"`]?" + regexp.QuoteMeta(name) + "[\"`\\s]") +} + func (d *ddl) addConstraint(name string, sql string) { - reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]") + reg := compileConstraintRegexp(name) for i := 0; i < len(d.fields); i++ { if reg.MatchString(d.fields[i]) { @@ -223,7 +227,7 @@ func (d *ddl) addConstraint(name string, sql string) { } func (d *ddl) removeConstraint(name string) bool { - reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]") + reg := compileConstraintRegexp(name) for i := 0; i < len(d.fields); i++ { if reg.MatchString(d.fields[i]) { @@ -236,7 +240,7 @@ func (d *ddl) removeConstraint(name string) bool { //lint:ignore U1000 ignore unused code. func (d *ddl) hasConstraint(name string) bool { - reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]") + reg := compileConstraintRegexp(name) for _, f := range d.fields { if reg.MatchString(f) { diff --git a/gormlite/ddlmod_parse_all_columns.go b/gormlite/ddlmod_parse_all_columns.go index bf398ee..dba736d 100644 --- a/gormlite/ddlmod_parse_all_columns.go +++ b/gormlite/ddlmod_parse_all_columns.go @@ -95,7 +95,7 @@ func parseAllColumns(in string) ([]string, error) { } return nil, fmt.Errorf("unexpected token: %s", string(s[i])) case parseAllColumnsState_State_End: - break + continue // avoid SA4011 } } if state != parseAllColumnsState_State_End { diff --git a/gormlite/ddlmod_test.go b/gormlite/ddlmod_test.go index 94898e7..1040415 100644 --- a/gormlite/ddlmod_test.go +++ b/gormlite/ddlmod_test.go @@ -313,6 +313,41 @@ func TestRemoveConstraint(t *testing.T) { success: true, expect: []string{"`id` integer NOT NULL"}, }, + { + name: "lowercase", + fields: []string{"`id` integer NOT NULL", "constraint `fk_users_notes` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"}, + cName: "fk_users_notes", + success: true, + expect: []string{"`id` integer NOT NULL"}, + }, + { + name: "mixed_case", + fields: []string{"`id` integer NOT NULL", "cOnsTraiNT `fk_users_notes` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"}, + cName: "fk_users_notes", + success: true, + expect: []string{"`id` integer NOT NULL"}, + }, + { + name: "newline", + fields: []string{"`id` integer NOT NULL", "CONSTRAINT `fk_users_notes`\nFOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"}, + cName: "fk_users_notes", + success: true, + expect: []string{"`id` integer NOT NULL"}, + }, + { + name: "lots_of_newlines", + fields: []string{"`id` integer NOT NULL", "constraint \n fk_users_notes \n FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"}, + cName: "fk_users_notes", + success: true, + expect: []string{"`id` integer NOT NULL"}, + }, + { + name: "no_backtick", + fields: []string{"`id` integer NOT NULL", "CONSTRAINT fk_users_notes FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))"}, + cName: "fk_users_notes", + success: true, + expect: []string{"`id` integer NOT NULL"}, + }, { name: "check", fields: []string{"CONSTRAINT `name_checker` CHECK (`name` <> 'thetadev')", "`id` integer NOT NULL"}, diff --git a/gormlite/update.sh b/gormlite/update.sh index e47d9fc..eacc779 100755 --- a/gormlite/update.sh +++ b/gormlite/update.sh @@ -11,5 +11,4 @@ curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/error_translator.go" curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/migrator.go" curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite.go" curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite_test.go" -curl -#OL "https://github.com/go-gorm/sqlite/raw/v1.5.7/sqlite_test.go" curl -#L "https://github.com/glebarez/sqlite/raw/v1.11.0/sqlite_error_translator_test.go" > error_translator_test.go \ No newline at end of file