Skip to content

Commit eb4bbcc

Browse files
committed
feat: Add postgres support
Postgres is more picky about submitting a string to the id column in a table. Postgres requires the use of only integers for the array of values in a select statement containing: where id IN (...array...) This patch fixes all the following class of problems: SELECT * FROM "ssh_keys" WHERE "ssh_keys"."deleted_at" IS NULL AND ((id IN ('host')) OR (name IN ('host'))) ORDER BY "ssh_keys"."id" ASC LIMIT 1 [0 rows affected or returned ] error: pq: invalid input syntax for type integer: "host" Signed-off-by: Jason Wessel <[email protected]>
1 parent a7a42f5 commit eb4bbcc

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

pkg/dbmodels/dbmodels.go

+39-6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ const (
167167
BastionSchemeTelnet BastionScheme = "telnet"
168168
)
169169

170+
// Generic Helper
171+
func GenericNameOrID(db *gorm.DB, identifiers []string) *gorm.DB {
172+
var ids []string;
173+
var names []string;
174+
for _, s := range identifiers {
175+
if _, err := strconv.Atoi(s); err == nil {
176+
ids = append(ids, s)
177+
} else {
178+
names = append(names, s)
179+
}
180+
}
181+
if len(ids) > 0 && len(names) > 0 {
182+
return db.Where("id IN (?)", ids).Or("name IN (?)", names)
183+
} else if len(ids) > 0 {
184+
return db.Where("id IN (?)", ids)
185+
}
186+
return db.Where("name IN (?)", names)
187+
}
188+
170189
// Host helpers
171190

172191
func (host *Host) DialAddr() string {
@@ -268,7 +287,7 @@ func HostsPreload(db *gorm.DB) *gorm.DB {
268287
return db.Preload("Groups").Preload("SSHKey")
269288
}
270289
func HostsByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
271-
return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers)
290+
return GenericNameOrID(db, identifiers)
272291
}
273292
func HostByName(db *gorm.DB, name string) (*Host, error) {
274293
var host Host
@@ -308,7 +327,7 @@ func SSHKeysPreload(db *gorm.DB) *gorm.DB {
308327
return db.Preload("Hosts")
309328
}
310329
func SSHKeysByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
311-
return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers)
330+
return GenericNameOrID(db, identifiers)
312331
}
313332

314333
// HostGroup helpers
@@ -317,7 +336,7 @@ func HostGroupsPreload(db *gorm.DB) *gorm.DB {
317336
return db.Preload("ACLs").Preload("Hosts")
318337
}
319338
func HostGroupsByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
320-
return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers)
339+
return GenericNameOrID(db, identifiers)
321340
}
322341

323342
// UserGroup helpers
@@ -326,7 +345,7 @@ func UserGroupsPreload(db *gorm.DB) *gorm.DB {
326345
return db.Preload("ACLs").Preload("Users")
327346
}
328347
func UserGroupsByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
329-
return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers)
348+
return GenericNameOrID(db, identifiers)
330349
}
331350

332351
// User helpers
@@ -335,7 +354,21 @@ func UsersPreload(db *gorm.DB) *gorm.DB {
335354
return db.Preload("Groups").Preload("Keys").Preload("Roles")
336355
}
337356
func UsersByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
338-
return db.Where("id IN (?)", identifiers).Or("email IN (?)", identifiers).Or("name IN (?)", identifiers)
357+
var ids []string;
358+
var names []string;
359+
for _, s := range identifiers {
360+
if _, err := strconv.Atoi(s); err == nil {
361+
ids = append(ids, s)
362+
} else {
363+
names = append(names, s)
364+
}
365+
}
366+
if len(ids) > 0 && len(names) > 0 {
367+
db.Where("id IN (?)", identifiers).Or("email IN (?)", identifiers).Or("name IN (?)", identifiers)
368+
} else if len(ids) > 0 {
369+
return db.Where("id IN (?)", ids)
370+
}
371+
return db.Where("email IN (?)", identifiers).Or("name IN (?)", identifiers)
339372
}
340373
func (u *User) HasRole(name string) bool {
341374
for _, role := range u.Roles {
@@ -381,7 +414,7 @@ func UserKeysByUserID(db *gorm.DB, identifiers []string) *gorm.DB {
381414
// return db.Preload("Users")
382415
//}
383416
func UserRolesByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
384-
return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers)
417+
return GenericNameOrID(db, identifiers)
385418
}
386419

387420
// Session helpers

0 commit comments

Comments
 (0)