fleet/server/datastore/mysql/android_mysql.go
Victor Lyuboslavsky ccc36a9cb3
Finishing mysql package migration to slog (#40350)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #40054

# Checklist for submitter

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Already present in previous PR

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Chores**
* Migrated logging to a structured, context-aware backend for clearer,
richer diagnostics and consistent log formatting.
* Introduced broader context propagation and adjusted internal
interfaces to support the new logging approach (no end-user behavior
changes).
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-24 16:52:36 -06:00

53 lines
1.6 KiB
Go

// Package mysql is a MySQL implementation of the android.Datastore interface.
package mysql
import (
"context"
"log/slog"
"github.com/fleetdm/fleet/v4/server/contexts/ctxdb"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mdm/android"
common_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
"github.com/jmoiron/sqlx"
)
// AndroidDatastore is an implementation of android.Datastore interface backed by MySQL
type AndroidDatastore struct {
logger *slog.Logger
primary *sqlx.DB
replica fleet.DBReader // so it cannot be used to perform writes
}
// NewAndroidDatastore creates a new Android Datastore
func NewAndroidDatastore(logger *slog.Logger, primary *sqlx.DB, replica fleet.DBReader) android.Datastore {
return &AndroidDatastore{
logger: logger,
primary: primary,
replica: replica,
}
}
// reader returns the DB instance to use for read-only statements, which is the
// replica unless the primary has been explicitly required via
// ctxdb.RequirePrimary.
func (ds *AndroidDatastore) reader(ctx context.Context) fleet.DBReader {
if ctxdb.IsPrimaryRequired(ctx) {
return ds.primary
}
return ds.replica
}
// Writer returns the DB instance to use for write statements, which is always
// the primary.
func (ds *AndroidDatastore) Writer(_ context.Context) *sqlx.DB {
return ds.primary
}
func (ds *AndroidDatastore) WithRetryTxx(ctx context.Context, fn common_mysql.TxFn) (err error) {
return common_mysql.WithRetryTxx(ctx, ds.Writer(ctx), fn, ds.logger)
}
func (ds *AndroidDatastore) WithTxx(ctx context.Context, fn common_mysql.TxFn) (err error) {
return common_mysql.WithTxx(ctx, ds.Writer(ctx), fn, ds.logger)
}