replaced exists bool db scans with int for broader drivers compatibility

This commit is contained in:
Gani Georgiev
2025-01-20 14:16:00 +02:00
parent 0ebe9c4faa
commit a4a228b368
11 changed files with 25 additions and 23 deletions
+2 -2
View File
@@ -762,9 +762,9 @@ func (c *Collection) updateGeneratedIdIfExists(app App) {
// add a number to the current id (if already exists)
for i := 2; i < 1000; i++ {
var exists bool
var exists int
_ = app.CollectionQuery().Select("(1)").AndWhere(dbx.HashExp{"id": newId}).Limit(1).Row(&exists)
if !exists {
if exists == 0 {
break
}
newId = c.idChecksum() + strconv.Itoa(i)
+2 -2
View File
@@ -483,7 +483,7 @@ func validateRecordId(app App, collectionNameOrId string) validation.RuleFunc {
return validation.NewError("validation_invalid_collection", "Missing or invalid collection.")
}
var exists bool
var exists int
rowErr := app.DB().Select("(1)").
From(collection.Name).
@@ -491,7 +491,7 @@ func validateRecordId(app App, collectionNameOrId string) validation.RuleFunc {
Limit(1).
Row(&exists)
if rowErr != nil || !exists {
if rowErr != nil || exists == 0 {
return validation.NewError("validation_invalid_record", "Missing or invalid record.")
}
+2 -2
View File
@@ -108,7 +108,7 @@ func (app *BaseApp) AuxHasTable(tableName string) bool {
}
func (app *BaseApp) hasTable(db dbx.Builder, tableName string) bool {
var exists bool
var exists int
err := db.Select("(1)").
From("sqlite_schema").
@@ -117,7 +117,7 @@ func (app *BaseApp) hasTable(db dbx.Builder, tableName string) bool {
Limit(1).
Row(&exists)
return err == nil && exists
return err == nil && exists > 0
}
// Vacuum executes VACUUM on the current app.DB() instance
+2 -2
View File
@@ -193,14 +193,14 @@ func (f *TextField) ValidateValue(ctx context.Context, app App, record *Record)
//
// (@todo eventually may get replaced in the future with a system unique constraint to avoid races or wrapping the request in a transaction)
if f.Pattern != defaultLowercaseRecordIdPattern {
var exists bool
var exists int
err := app.DB().
Select("(1)").
From(record.TableName()).
Where(dbx.NewExp("id = {:id} COLLATE NOCASE", dbx.Params{"id": strings.ToLower(newVal)})).
Limit(1).
Row(&exists)
if exists || (err != nil && !errors.Is(err, sql.ErrNoRows)) {
if exists > 0 || (err != nil && !errors.Is(err, sql.ErrNoRows)) {
return validation.NewError("validation_pk_invalid", "The record primary key is invalid or already exists.")
}
}
+2 -2
View File
@@ -267,7 +267,7 @@ func (r *MigrationsRunner) initMigrationsTable() error {
}
func (r *MigrationsRunner) isMigrationApplied(txApp App, file string) bool {
var exists bool
var exists int
err := txApp.DB().Select("(1)").
From(r.tableName).
@@ -275,7 +275,7 @@ func (r *MigrationsRunner) isMigrationApplied(txApp App, file string) bool {
Limit(1).
Row(&exists)
return err == nil && exists
return err == nil && exists > 0
}
func (r *MigrationsRunner) saveAppliedMigration(txApp App, file string) error {
+2 -2
View File
@@ -200,7 +200,7 @@ func TestMigrationsRunnerRemoveMissingAppliedMigrations(t *testing.T) {
}
func isMigrationApplied(app core.App, file string) bool {
var exists bool
var exists int
err := app.DB().Select("(1)").
From(core.DefaultMigrationsTable).
@@ -208,5 +208,5 @@ func isMigrationApplied(app core.App, file string) bool {
Limit(1).
Row(&exists)
return err == nil && exists
return err == nil && exists > 0
}
+2 -2
View File
@@ -584,7 +584,7 @@ func (app *BaseApp) CanAccessRecord(record *Record, requestInfo *RequestInfo, ac
return true, nil
}
var exists bool
var exists int
query := app.RecordQuery(record.Collection()).
Select("(1)").
@@ -603,5 +603,5 @@ func (app *BaseApp) CanAccessRecord(record *Record, requestInfo *RequestInfo, ac
return false, err
}
return exists, nil
return exists > 0, nil
}