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

View File

@ -14,12 +14,14 @@
- Added JSVM `new Timezone(name)` binding for constructing `time.Location` value ([#6219](https://github.com/pocketbase/pocketbase/discussions/6219)).
- Soft-deprecated `Record.GetUploadedFiles` in favour of `Record.GetUnsavedFiles` to minimize the ambiguities what the method do ([#6269](https://github.com/pocketbase/pocketbase/discussions/6269)).
- Soft-deprecated `Record.GetUploadedFiles` in favor of `Record.GetUnsavedFiles` to minimize the ambiguities what the method do ([#6269](https://github.com/pocketbase/pocketbase/discussions/6269)).
(@todo update docs to reflect the `:unsaved` getter change)
- Enforced `when_required` for the new AWS SDK request and response checksum validations to allow other non-AWS vendors to catch up with new AWS SDK changes (see [#6313](https://github.com/pocketbase/pocketbase/discussions/6313) and [aws/aws-sdk-go-v2#2960](https://github.com/aws/aws-sdk-go-v2/discussions/2960)).
_You can set the environment variables `AWS_REQUEST_CHECKSUM_CALCULATION` and `AWS_RESPONSE_CHECKSUM_VALIDATION` to `when_supported` if your S3 vendor supports the new [new default integrity protections](https://docs.aws.amazon.com/sdkref/latest/guide/feature-dataintegrity.html)._
- Other minor improvements (_replaced all `bool` exists db scans with `int` for broader drivers compatibility, use the non-transactional app instance during realtime records delete access checks to ensure that cascade deleted records with API rules relying on the parent will be resolved, updated UI dependencies, etc._)
## v0.24.4

View File

@ -734,7 +734,7 @@ func realtimeCanAccessRecord(
return false
}
var exists bool
var exists int
q := app.DB().Select("(1)").
From(record.Collection().Name).
@ -751,5 +751,5 @@ func realtimeCanAccessRecord(
err = q.Limit(1).Row(&exists)
return err == nil && exists
return err == nil && exists > 0
}

View File

@ -314,9 +314,9 @@ func recordCreate(optFinalizer func(data any) error) func(e *core.RequestEvent)
resolver.UpdateQuery(ruleQuery)
var exists bool
var exists int
err = ruleQuery.Limit(1).Row(&exists)
if err != nil || !exists {
if err != nil || exists == 0 {
return e.BadRequestError("Failed to create record", fmt.Errorf("create rule failure: %w", err))
}
}
@ -720,9 +720,9 @@ func hasAuthManageAccess(app core.App, requestInfo *core.RequestInfo, collection
resolver.UpdateQuery(query)
var exists bool
var exists int
err = query.Limit(1).Row(&exists)
return err == nil && exists
return err == nil && exists > 0
}

View File

@ -146,7 +146,7 @@ func wantsMFA(e *core.RequestEvent, record *core.Record) (bool, error) {
return true, err
}
var exists bool
var exists int
query := e.App.RecordQuery(record.Collection()).
Select("(1)").
@ -165,7 +165,7 @@ func wantsMFA(e *core.RequestEvent, record *core.Record) (bool, error) {
return true, err
}
return exists, nil
return exists > 0, nil
}
// checkMFA handles any MFA auth checks that needs to be performed for the specified request event.

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)

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.")
}

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

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.")
}
}

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 {

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
}

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
}