diff --git a/internal/models/bmc_mac_address.go b/internal/models/bmc_mac_address.go new file mode 100644 index 00000000..1a211164 --- /dev/null +++ b/internal/models/bmc_mac_address.go @@ -0,0 +1,1062 @@ +// Code generated by SQLBoiler 4.11.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package models + +import ( + "context" + "database/sql" + "fmt" + "reflect" + "strconv" + "strings" + "sync" + "time" + + "github.com/friendsofgo/errors" + "github.com/volatiletech/sqlboiler/v4/boil" + "github.com/volatiletech/sqlboiler/v4/queries" + "github.com/volatiletech/sqlboiler/v4/queries/qm" + "github.com/volatiletech/sqlboiler/v4/queries/qmhelper" + "github.com/volatiletech/strmangle" +) + +// BMCMacAddress is an object representing the database table. +type BMCMacAddress struct { + BMCMacAddress string `boil:"bmc_mac_address" json:"bmc_mac_address" toml:"bmc_mac_address" yaml:"bmc_mac_address"` + SerialNum string `boil:"serial_num" json:"serial_num" toml:"serial_num" yaml:"serial_num"` + + R *bmcMacAddressR `boil:"-" json:"-" toml:"-" yaml:"-"` + L bmcMacAddressL `boil:"-" json:"-" toml:"-" yaml:"-"` +} + +var BMCMacAddressColumns = struct { + BMCMacAddress string + SerialNum string +}{ + BMCMacAddress: "bmc_mac_address", + SerialNum: "serial_num", +} + +var BMCMacAddressTableColumns = struct { + BMCMacAddress string + SerialNum string +}{ + BMCMacAddress: "bmc_mac_address.bmc_mac_address", + SerialNum: "bmc_mac_address.serial_num", +} + +// Generated where + +var BMCMacAddressWhere = struct { + BMCMacAddress whereHelperstring + SerialNum whereHelperstring +}{ + BMCMacAddress: whereHelperstring{field: "\"bmc_mac_address\".\"bmc_mac_address\""}, + SerialNum: whereHelperstring{field: "\"bmc_mac_address\".\"serial_num\""}, +} + +// BMCMacAddressRels is where relationship names are stored. +var BMCMacAddressRels = struct { + SerialNumBomInfo string +}{ + SerialNumBomInfo: "SerialNumBomInfo", +} + +// bmcMacAddressR is where relationships are stored. +type bmcMacAddressR struct { + SerialNumBomInfo *BomInfo `boil:"SerialNumBomInfo" json:"SerialNumBomInfo" toml:"SerialNumBomInfo" yaml:"SerialNumBomInfo"` +} + +// NewStruct creates a new relationship struct +func (*bmcMacAddressR) NewStruct() *bmcMacAddressR { + return &bmcMacAddressR{} +} + +func (r *bmcMacAddressR) GetSerialNumBomInfo() *BomInfo { + if r == nil { + return nil + } + return r.SerialNumBomInfo +} + +// bmcMacAddressL is where Load methods for each relationship are stored. +type bmcMacAddressL struct{} + +var ( + bmcMacAddressAllColumns = []string{"bmc_mac_address", "serial_num"} + bmcMacAddressColumnsWithoutDefault = []string{"bmc_mac_address", "serial_num"} + bmcMacAddressColumnsWithDefault = []string{} + bmcMacAddressPrimaryKeyColumns = []string{"bmc_mac_address"} + bmcMacAddressGeneratedColumns = []string{} +) + +type ( + // BMCMacAddressSlice is an alias for a slice of pointers to BMCMacAddress. + // This should almost always be used instead of []BMCMacAddress. + BMCMacAddressSlice []*BMCMacAddress + // BMCMacAddressHook is the signature for custom BMCMacAddress hook methods + BMCMacAddressHook func(context.Context, boil.ContextExecutor, *BMCMacAddress) error + + bmcMacAddressQuery struct { + *queries.Query + } +) + +// Cache for insert, update and upsert +var ( + bmcMacAddressType = reflect.TypeOf(&BMCMacAddress{}) + bmcMacAddressMapping = queries.MakeStructMapping(bmcMacAddressType) + bmcMacAddressPrimaryKeyMapping, _ = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, bmcMacAddressPrimaryKeyColumns) + bmcMacAddressInsertCacheMut sync.RWMutex + bmcMacAddressInsertCache = make(map[string]insertCache) + bmcMacAddressUpdateCacheMut sync.RWMutex + bmcMacAddressUpdateCache = make(map[string]updateCache) + bmcMacAddressUpsertCacheMut sync.RWMutex + bmcMacAddressUpsertCache = make(map[string]insertCache) +) + +var ( + // Force time package dependency for automated UpdatedAt/CreatedAt. + _ = time.Second + // Force qmhelper dependency for where clause generation (which doesn't + // always happen) + _ = qmhelper.Where +) + +var bmcMacAddressAfterSelectHooks []BMCMacAddressHook + +var bmcMacAddressBeforeInsertHooks []BMCMacAddressHook +var bmcMacAddressAfterInsertHooks []BMCMacAddressHook + +var bmcMacAddressBeforeUpdateHooks []BMCMacAddressHook +var bmcMacAddressAfterUpdateHooks []BMCMacAddressHook + +var bmcMacAddressBeforeDeleteHooks []BMCMacAddressHook +var bmcMacAddressAfterDeleteHooks []BMCMacAddressHook + +var bmcMacAddressBeforeUpsertHooks []BMCMacAddressHook +var bmcMacAddressAfterUpsertHooks []BMCMacAddressHook + +// doAfterSelectHooks executes all "after Select" hooks. +func (o *BMCMacAddress) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressAfterSelectHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeInsertHooks executes all "before insert" hooks. +func (o *BMCMacAddress) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressBeforeInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterInsertHooks executes all "after Insert" hooks. +func (o *BMCMacAddress) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressAfterInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpdateHooks executes all "before Update" hooks. +func (o *BMCMacAddress) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressBeforeUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpdateHooks executes all "after Update" hooks. +func (o *BMCMacAddress) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressAfterUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeDeleteHooks executes all "before Delete" hooks. +func (o *BMCMacAddress) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressBeforeDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterDeleteHooks executes all "after Delete" hooks. +func (o *BMCMacAddress) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressAfterDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpsertHooks executes all "before Upsert" hooks. +func (o *BMCMacAddress) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressBeforeUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpsertHooks executes all "after Upsert" hooks. +func (o *BMCMacAddress) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bmcMacAddressAfterUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// AddBMCMacAddressHook registers your hook function for all future operations. +func AddBMCMacAddressHook(hookPoint boil.HookPoint, bmcMacAddressHook BMCMacAddressHook) { + switch hookPoint { + case boil.AfterSelectHook: + bmcMacAddressAfterSelectHooks = append(bmcMacAddressAfterSelectHooks, bmcMacAddressHook) + case boil.BeforeInsertHook: + bmcMacAddressBeforeInsertHooks = append(bmcMacAddressBeforeInsertHooks, bmcMacAddressHook) + case boil.AfterInsertHook: + bmcMacAddressAfterInsertHooks = append(bmcMacAddressAfterInsertHooks, bmcMacAddressHook) + case boil.BeforeUpdateHook: + bmcMacAddressBeforeUpdateHooks = append(bmcMacAddressBeforeUpdateHooks, bmcMacAddressHook) + case boil.AfterUpdateHook: + bmcMacAddressAfterUpdateHooks = append(bmcMacAddressAfterUpdateHooks, bmcMacAddressHook) + case boil.BeforeDeleteHook: + bmcMacAddressBeforeDeleteHooks = append(bmcMacAddressBeforeDeleteHooks, bmcMacAddressHook) + case boil.AfterDeleteHook: + bmcMacAddressAfterDeleteHooks = append(bmcMacAddressAfterDeleteHooks, bmcMacAddressHook) + case boil.BeforeUpsertHook: + bmcMacAddressBeforeUpsertHooks = append(bmcMacAddressBeforeUpsertHooks, bmcMacAddressHook) + case boil.AfterUpsertHook: + bmcMacAddressAfterUpsertHooks = append(bmcMacAddressAfterUpsertHooks, bmcMacAddressHook) + } +} + +// One returns a single bmcMacAddress record from the query. +func (q bmcMacAddressQuery) One(ctx context.Context, exec boil.ContextExecutor) (*BMCMacAddress, error) { + o := &BMCMacAddress{} + + queries.SetLimit(q.Query, 1) + + err := q.Bind(ctx, exec, o) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: failed to execute a one query for bmc_mac_address") + } + + if err := o.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + + return o, nil +} + +// All returns all BMCMacAddress records from the query. +func (q bmcMacAddressQuery) All(ctx context.Context, exec boil.ContextExecutor) (BMCMacAddressSlice, error) { + var o []*BMCMacAddress + + err := q.Bind(ctx, exec, &o) + if err != nil { + return nil, errors.Wrap(err, "models: failed to assign all query results to BMCMacAddress slice") + } + + if len(bmcMacAddressAfterSelectHooks) != 0 { + for _, obj := range o { + if err := obj.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + } + } + + return o, nil +} + +// Count returns the count of all BMCMacAddress records in the query. +func (q bmcMacAddressQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + var count int64 + + queries.SetSelect(q.Query, nil) + queries.SetCount(q.Query) + + err := q.Query.QueryRowContext(ctx, exec).Scan(&count) + if err != nil { + return 0, errors.Wrap(err, "models: failed to count bmc_mac_address rows") + } + + return count, nil +} + +// Exists checks if the row exists in the table. +func (q bmcMacAddressQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { + var count int64 + + queries.SetSelect(q.Query, nil) + queries.SetCount(q.Query) + queries.SetLimit(q.Query, 1) + + err := q.Query.QueryRowContext(ctx, exec).Scan(&count) + if err != nil { + return false, errors.Wrap(err, "models: failed to check if bmc_mac_address exists") + } + + return count > 0, nil +} + +// SerialNumBomInfo pointed to by the foreign key. +func (o *BMCMacAddress) SerialNumBomInfo(mods ...qm.QueryMod) bomInfoQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"serial_num\" = ?", o.SerialNum), + } + + queryMods = append(queryMods, mods...) + + return BomInfos(queryMods...) +} + +// LoadSerialNumBomInfo allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (bmcMacAddressL) LoadSerialNumBomInfo(ctx context.Context, e boil.ContextExecutor, singular bool, maybeBMCMacAddress interface{}, mods queries.Applicator) error { + var slice []*BMCMacAddress + var object *BMCMacAddress + + if singular { + object = maybeBMCMacAddress.(*BMCMacAddress) + } else { + slice = *maybeBMCMacAddress.(*[]*BMCMacAddress) + } + + args := make([]interface{}, 0, 1) + if singular { + if object.R == nil { + object.R = &bmcMacAddressR{} + } + args = append(args, object.SerialNum) + + } else { + Outer: + for _, obj := range slice { + if obj.R == nil { + obj.R = &bmcMacAddressR{} + } + + for _, a := range args { + if a == obj.SerialNum { + continue Outer + } + } + + args = append(args, obj.SerialNum) + + } + } + + if len(args) == 0 { + return nil + } + + query := NewQuery( + qm.From(`bom_info`), + qm.WhereIn(`bom_info.serial_num in ?`, args...), + ) + if mods != nil { + mods.Apply(query) + } + + results, err := query.QueryContext(ctx, e) + if err != nil { + return errors.Wrap(err, "failed to eager load BomInfo") + } + + var resultSlice []*BomInfo + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice BomInfo") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results of eager load for bom_info") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for bom_info") + } + + if len(bmcMacAddressAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + + if len(resultSlice) == 0 { + return nil + } + + if singular { + foreign := resultSlice[0] + object.R.SerialNumBomInfo = foreign + if foreign.R == nil { + foreign.R = &bomInfoR{} + } + foreign.R.SerialNumBMCMacAddresses = append(foreign.R.SerialNumBMCMacAddresses, object) + return nil + } + + for _, local := range slice { + for _, foreign := range resultSlice { + if local.SerialNum == foreign.SerialNum { + local.R.SerialNumBomInfo = foreign + if foreign.R == nil { + foreign.R = &bomInfoR{} + } + foreign.R.SerialNumBMCMacAddresses = append(foreign.R.SerialNumBMCMacAddresses, local) + break + } + } + } + + return nil +} + +// SetSerialNumBomInfo of the bmcMacAddress to the related item. +// Sets o.R.SerialNumBomInfo to related. +// Adds o to related.R.SerialNumBMCMacAddresses. +func (o *BMCMacAddress) SetSerialNumBomInfo(ctx context.Context, exec boil.ContextExecutor, insert bool, related *BomInfo) error { + var err error + if insert { + if err = related.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } + + updateQuery := fmt.Sprintf( + "UPDATE \"bmc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"serial_num"}), + strmangle.WhereClause("\"", "\"", 2, bmcMacAddressPrimaryKeyColumns), + ) + values := []interface{}{related.SerialNum, o.BMCMacAddress} + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, updateQuery) + fmt.Fprintln(writer, values) + } + if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { + return errors.Wrap(err, "failed to update local table") + } + + o.SerialNum = related.SerialNum + if o.R == nil { + o.R = &bmcMacAddressR{ + SerialNumBomInfo: related, + } + } else { + o.R.SerialNumBomInfo = related + } + + if related.R == nil { + related.R = &bomInfoR{ + SerialNumBMCMacAddresses: BMCMacAddressSlice{o}, + } + } else { + related.R.SerialNumBMCMacAddresses = append(related.R.SerialNumBMCMacAddresses, o) + } + + return nil +} + +// BMCMacAddresses retrieves all the records using an executor. +func BMCMacAddresses(mods ...qm.QueryMod) bmcMacAddressQuery { + mods = append(mods, qm.From("\"bmc_mac_address\"")) + q := NewQuery(mods...) + if len(queries.GetSelect(q)) == 0 { + queries.SetSelect(q, []string{"\"bmc_mac_address\".*"}) + } + + return bmcMacAddressQuery{q} +} + +// FindBMCMacAddress retrieves a single record by ID with an executor. +// If selectCols is empty Find will return all columns. +func FindBMCMacAddress(ctx context.Context, exec boil.ContextExecutor, bMCMacAddress string, selectCols ...string) (*BMCMacAddress, error) { + bmcMacAddressObj := &BMCMacAddress{} + + sel := "*" + if len(selectCols) > 0 { + sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") + } + query := fmt.Sprintf( + "select %s from \"bmc_mac_address\" where \"bmc_mac_address\"=$1", sel, + ) + + q := queries.Raw(query, bMCMacAddress) + + err := q.Bind(ctx, exec, bmcMacAddressObj) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: unable to select from bmc_mac_address") + } + + if err = bmcMacAddressObj.doAfterSelectHooks(ctx, exec); err != nil { + return bmcMacAddressObj, err + } + + return bmcMacAddressObj, nil +} + +// Insert a single record using an executor. +// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. +func (o *BMCMacAddress) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { + if o == nil { + return errors.New("models: no bmc_mac_address provided for insertion") + } + + var err error + + if err := o.doBeforeInsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(bmcMacAddressColumnsWithDefault, o) + + key := makeCacheKey(columns, nzDefaults) + bmcMacAddressInsertCacheMut.RLock() + cache, cached := bmcMacAddressInsertCache[key] + bmcMacAddressInsertCacheMut.RUnlock() + + if !cached { + wl, returnColumns := columns.InsertColumnSet( + bmcMacAddressAllColumns, + bmcMacAddressColumnsWithDefault, + bmcMacAddressColumnsWithoutDefault, + nzDefaults, + ) + + cache.valueMapping, err = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, wl) + if err != nil { + return err + } + cache.retMapping, err = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, returnColumns) + if err != nil { + return err + } + if len(wl) != 0 { + cache.query = fmt.Sprintf("INSERT INTO \"bmc_mac_address\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) + } else { + cache.query = "INSERT INTO \"bmc_mac_address\" %sDEFAULT VALUES%s" + } + + var queryOutput, queryReturning string + + if len(cache.retMapping) != 0 { + queryReturning = fmt.Sprintf(" RETURNING \"%s\"", strings.Join(returnColumns, "\",\"")) + } + + cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning) + } + + value := reflect.Indirect(reflect.ValueOf(o)) + vals := queries.ValuesFromMapping(value, cache.valueMapping) + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, cache.query) + fmt.Fprintln(writer, vals) + } + + if len(cache.retMapping) != 0 { + err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) + } else { + _, err = exec.ExecContext(ctx, cache.query, vals...) + } + + if err != nil { + return errors.Wrap(err, "models: unable to insert into bmc_mac_address") + } + + if !cached { + bmcMacAddressInsertCacheMut.Lock() + bmcMacAddressInsertCache[key] = cache + bmcMacAddressInsertCacheMut.Unlock() + } + + return o.doAfterInsertHooks(ctx, exec) +} + +// Update uses an executor to update the BMCMacAddress. +// See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates. +// Update does not automatically update the record in case of default values. Use .Reload() to refresh the records. +func (o *BMCMacAddress) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) { + var err error + if err = o.doBeforeUpdateHooks(ctx, exec); err != nil { + return 0, err + } + key := makeCacheKey(columns, nil) + bmcMacAddressUpdateCacheMut.RLock() + cache, cached := bmcMacAddressUpdateCache[key] + bmcMacAddressUpdateCacheMut.RUnlock() + + if !cached { + wl := columns.UpdateColumnSet( + bmcMacAddressAllColumns, + bmcMacAddressPrimaryKeyColumns, + ) + + if !columns.IsWhitelist() { + wl = strmangle.SetComplement(wl, []string{"created_at"}) + } + if len(wl) == 0 { + return 0, errors.New("models: unable to update bmc_mac_address, could not build whitelist") + } + + cache.query = fmt.Sprintf("UPDATE \"bmc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, wl), + strmangle.WhereClause("\"", "\"", len(wl)+1, bmcMacAddressPrimaryKeyColumns), + ) + cache.valueMapping, err = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, append(wl, bmcMacAddressPrimaryKeyColumns...)) + if err != nil { + return 0, err + } + } + + values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, cache.query) + fmt.Fprintln(writer, values) + } + var result sql.Result + result, err = exec.ExecContext(ctx, cache.query, values...) + if err != nil { + return 0, errors.Wrap(err, "models: unable to update bmc_mac_address row") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by update for bmc_mac_address") + } + + if !cached { + bmcMacAddressUpdateCacheMut.Lock() + bmcMacAddressUpdateCache[key] = cache + bmcMacAddressUpdateCacheMut.Unlock() + } + + return rowsAff, o.doAfterUpdateHooks(ctx, exec) +} + +// UpdateAll updates all rows with the specified column values. +func (q bmcMacAddressQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { + queries.SetUpdate(q.Query, cols) + + result, err := q.Query.ExecContext(ctx, exec) + if err != nil { + return 0, errors.Wrap(err, "models: unable to update all for bmc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected for bmc_mac_address") + } + + return rowsAff, nil +} + +// UpdateAll updates all rows with the specified column values, using an executor. +func (o BMCMacAddressSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { + ln := int64(len(o)) + if ln == 0 { + return 0, nil + } + + if len(cols) == 0 { + return 0, errors.New("models: update all requires at least one column argument") + } + + colNames := make([]string, len(cols)) + args := make([]interface{}, len(cols)) + + i := 0 + for name, value := range cols { + colNames[i] = name + args[i] = value + i++ + } + + // Append all of the primary key values for each column + for _, obj := range o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), bmcMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := fmt.Sprintf("UPDATE \"bmc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, colNames), + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, bmcMacAddressPrimaryKeyColumns, len(o))) + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, args...) + } + result, err := exec.ExecContext(ctx, sql, args...) + if err != nil { + return 0, errors.Wrap(err, "models: unable to update all in bmcMacAddress slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all bmcMacAddress") + } + return rowsAff, nil +} + +// Delete deletes a single BMCMacAddress record with an executor. +// Delete will match against the primary key column to find the record to delete. +func (o *BMCMacAddress) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if o == nil { + return 0, errors.New("models: no BMCMacAddress provided for delete") + } + + if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), bmcMacAddressPrimaryKeyMapping) + sql := "DELETE FROM \"bmc_mac_address\" WHERE \"bmc_mac_address\"=$1" + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, args...) + } + result, err := exec.ExecContext(ctx, sql, args...) + if err != nil { + return 0, errors.Wrap(err, "models: unable to delete from bmc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by delete for bmc_mac_address") + } + + if err := o.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + return rowsAff, nil +} + +// DeleteAll deletes all matching rows. +func (q bmcMacAddressQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if q.Query == nil { + return 0, errors.New("models: no bmcMacAddressQuery provided for delete all") + } + + queries.SetDelete(q.Query) + + result, err := q.Query.ExecContext(ctx, exec) + if err != nil { + return 0, errors.Wrap(err, "models: unable to delete all from bmc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for bmc_mac_address") + } + + return rowsAff, nil +} + +// DeleteAll deletes all rows in the slice, using an executor. +func (o BMCMacAddressSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if len(o) == 0 { + return 0, nil + } + + if len(bmcMacAddressBeforeDeleteHooks) != 0 { + for _, obj := range o { + if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + } + } + + var args []interface{} + for _, obj := range o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), bmcMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "DELETE FROM \"bmc_mac_address\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, bmcMacAddressPrimaryKeyColumns, len(o)) + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, args) + } + result, err := exec.ExecContext(ctx, sql, args...) + if err != nil { + return 0, errors.Wrap(err, "models: unable to delete all from bmcMacAddress slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for bmc_mac_address") + } + + if len(bmcMacAddressAfterDeleteHooks) != 0 { + for _, obj := range o { + if err := obj.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + } + } + + return rowsAff, nil +} + +// Reload refetches the object from the database +// using the primary keys with an executor. +func (o *BMCMacAddress) Reload(ctx context.Context, exec boil.ContextExecutor) error { + ret, err := FindBMCMacAddress(ctx, exec, o.BMCMacAddress) + if err != nil { + return err + } + + *o = *ret + return nil +} + +// ReloadAll refetches every row with matching primary key column values +// and overwrites the original object slice with the newly updated slice. +func (o *BMCMacAddressSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { + if o == nil || len(*o) == 0 { + return nil + } + + slice := BMCMacAddressSlice{} + var args []interface{} + for _, obj := range *o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), bmcMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "SELECT \"bmc_mac_address\".* FROM \"bmc_mac_address\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, bmcMacAddressPrimaryKeyColumns, len(*o)) + + q := queries.Raw(sql, args...) + + err := q.Bind(ctx, exec, &slice) + if err != nil { + return errors.Wrap(err, "models: unable to reload all in BMCMacAddressSlice") + } + + *o = slice + + return nil +} + +// BMCMacAddressExists checks if the BMCMacAddress row exists. +func BMCMacAddressExists(ctx context.Context, exec boil.ContextExecutor, bMCMacAddress string) (bool, error) { + var exists bool + sql := "select exists(select 1 from \"bmc_mac_address\" where \"bmc_mac_address\"=$1 limit 1)" + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, bMCMacAddress) + } + row := exec.QueryRowContext(ctx, sql, bMCMacAddress) + + err := row.Scan(&exists) + if err != nil { + return false, errors.Wrap(err, "models: unable to check if bmc_mac_address exists") + } + + return exists, nil +} + +// Upsert attempts an insert using an executor, and does an update or ignore on conflict. +// See boil.Columns documentation for how to properly use updateColumns and insertColumns. +func (o *BMCMacAddress) Upsert(ctx context.Context, exec boil.ContextExecutor, updateOnConflict bool, conflictColumns []string, updateColumns, insertColumns boil.Columns) error { + if o == nil { + return errors.New("models: no bmc_mac_address provided for upsert") + } + + if err := o.doBeforeUpsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(bmcMacAddressColumnsWithDefault, o) + + // Build cache key in-line uglily - mysql vs psql problems + buf := strmangle.GetBuffer() + if updateOnConflict { + buf.WriteByte('t') + } else { + buf.WriteByte('f') + } + buf.WriteByte('.') + for _, c := range conflictColumns { + buf.WriteString(c) + } + buf.WriteByte('.') + buf.WriteString(strconv.Itoa(updateColumns.Kind)) + for _, c := range updateColumns.Cols { + buf.WriteString(c) + } + buf.WriteByte('.') + buf.WriteString(strconv.Itoa(insertColumns.Kind)) + for _, c := range insertColumns.Cols { + buf.WriteString(c) + } + buf.WriteByte('.') + for _, c := range nzDefaults { + buf.WriteString(c) + } + key := buf.String() + strmangle.PutBuffer(buf) + + bmcMacAddressUpsertCacheMut.RLock() + cache, cached := bmcMacAddressUpsertCache[key] + bmcMacAddressUpsertCacheMut.RUnlock() + + var err error + + if !cached { + insert, ret := insertColumns.InsertColumnSet( + bmcMacAddressAllColumns, + bmcMacAddressColumnsWithDefault, + bmcMacAddressColumnsWithoutDefault, + nzDefaults, + ) + update := updateColumns.UpdateColumnSet( + bmcMacAddressAllColumns, + bmcMacAddressPrimaryKeyColumns, + ) + + if updateOnConflict && len(update) == 0 { + return errors.New("models: unable to upsert bmc_mac_address, could not build update column list") + } + + conflict := conflictColumns + if len(conflict) == 0 { + conflict = make([]string, len(bmcMacAddressPrimaryKeyColumns)) + copy(conflict, bmcMacAddressPrimaryKeyColumns) + } + cache.query = buildUpsertQueryCockroachDB(dialect, "\"bmc_mac_address\"", updateOnConflict, ret, update, conflict, insert) + + cache.valueMapping, err = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, insert) + if err != nil { + return err + } + if len(ret) != 0 { + cache.retMapping, err = queries.BindMapping(bmcMacAddressType, bmcMacAddressMapping, ret) + if err != nil { + return err + } + } + } + + value := reflect.Indirect(reflect.ValueOf(o)) + vals := queries.ValuesFromMapping(value, cache.valueMapping) + var returns []interface{} + if len(cache.retMapping) != 0 { + returns = queries.PtrsFromMapping(value, cache.retMapping) + } + + if boil.DebugMode { + _, _ = fmt.Fprintln(boil.DebugWriter, cache.query) + _, _ = fmt.Fprintln(boil.DebugWriter, vals) + } + + if len(cache.retMapping) != 0 { + err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(returns...) + if err == sql.ErrNoRows { + err = nil // CockcorachDB doesn't return anything when there's no update + } + } else { + _, err = exec.ExecContext(ctx, cache.query, vals...) + } + if err != nil { + return errors.Wrap(err, "models: unable to upsert bmc_mac_address") + } + + if !cached { + bmcMacAddressUpsertCacheMut.Lock() + bmcMacAddressUpsertCache[key] = cache + bmcMacAddressUpsertCacheMut.Unlock() + } + + return o.doAfterUpsertHooks(ctx, exec) +} diff --git a/internal/models/bmc_mac_address_test.go b/internal/models/bmc_mac_address_test.go new file mode 100644 index 00000000..baaea86a --- /dev/null +++ b/internal/models/bmc_mac_address_test.go @@ -0,0 +1,841 @@ +// Code generated by SQLBoiler 4.11.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package models + +import ( + "bytes" + "context" + "reflect" + "testing" + + "github.com/volatiletech/randomize" + "github.com/volatiletech/sqlboiler/v4/boil" + "github.com/volatiletech/sqlboiler/v4/queries" + "github.com/volatiletech/strmangle" +) + +func testBMCMacAddressesUpsert(t *testing.T) { + t.Parallel() + + if len(bmcMacAddressAllColumns) == len(bmcMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + // Attempt the INSERT side of an UPSERT + o := BMCMacAddress{} + if err = randomize.Struct(seed, &o, bmcMacAddressDBTypes, true); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Upsert(ctx, tx, false, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert BMCMacAddress: %s", err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + if count != 1 { + t.Error("want one record, got:", count) + } + + // Attempt the UPDATE side of an UPSERT + if err = randomize.Struct(seed, &o, bmcMacAddressDBTypes, false, bmcMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + if err = o.Upsert(ctx, tx, true, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert BMCMacAddress: %s", err) + } + + count, err = BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + if count != 1 { + t.Error("want one record, got:", count) + } +} + +var ( + // Relationships sometimes use the reflection helper queries.Equal/queries.Assign + // so force a package dependency in case they don't. + _ = queries.Equal +) + +func testBMCMacAddresses(t *testing.T) { + t.Parallel() + + query := BMCMacAddresses() + + if query.Query == nil { + t.Error("expected a query, got nothing") + } +} + +func testBMCMacAddressesDelete(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if rowsAff, err := o.Delete(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBMCMacAddressesQueryDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if rowsAff, err := BMCMacAddresses().DeleteAll(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBMCMacAddressesSliceDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice := BMCMacAddressSlice{o} + + if rowsAff, err := slice.DeleteAll(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBMCMacAddressesExists(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + e, err := BMCMacAddressExists(ctx, tx, o.BMCMacAddress) + if err != nil { + t.Errorf("Unable to check if BMCMacAddress exists: %s", err) + } + if !e { + t.Errorf("Expected BMCMacAddressExists to return true, but got false.") + } +} + +func testBMCMacAddressesFind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + bmcMacAddressFound, err := FindBMCMacAddress(ctx, tx, o.BMCMacAddress) + if err != nil { + t.Error(err) + } + + if bmcMacAddressFound == nil { + t.Error("want a record, got nil") + } +} + +func testBMCMacAddressesBind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if err = BMCMacAddresses().Bind(ctx, tx, o); err != nil { + t.Error(err) + } +} + +func testBMCMacAddressesOne(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if x, err := BMCMacAddresses().One(ctx, tx); err != nil { + t.Error(err) + } else if x == nil { + t.Error("expected to get a non nil record") + } +} + +func testBMCMacAddressesAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + bmcMacAddressOne := &BMCMacAddress{} + bmcMacAddressTwo := &BMCMacAddress{} + if err = randomize.Struct(seed, bmcMacAddressOne, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + if err = randomize.Struct(seed, bmcMacAddressTwo, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = bmcMacAddressOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = bmcMacAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := BMCMacAddresses().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 2 { + t.Error("want 2 records, got:", len(slice)) + } +} + +func testBMCMacAddressesCount(t *testing.T) { + t.Parallel() + + var err error + seed := randomize.NewSeed() + bmcMacAddressOne := &BMCMacAddress{} + bmcMacAddressTwo := &BMCMacAddress{} + if err = randomize.Struct(seed, bmcMacAddressOne, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + if err = randomize.Struct(seed, bmcMacAddressTwo, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = bmcMacAddressOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = bmcMacAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 2 { + t.Error("want 2 records, got:", count) + } +} + +func bmcMacAddressBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func bmcMacAddressAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *BMCMacAddress) error { + *o = BMCMacAddress{} + return nil +} + +func testBMCMacAddressesHooks(t *testing.T) { + t.Parallel() + + var err error + + ctx := context.Background() + empty := &BMCMacAddress{} + o := &BMCMacAddress{} + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, false); err != nil { + t.Errorf("Unable to randomize BMCMacAddress object: %s", err) + } + + AddBMCMacAddressHook(boil.BeforeInsertHook, bmcMacAddressBeforeInsertHook) + if err = o.doBeforeInsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeInsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o) + } + bmcMacAddressBeforeInsertHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.AfterInsertHook, bmcMacAddressAfterInsertHook) + if err = o.doAfterInsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterInsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterInsertHook function to empty object, but got: %#v", o) + } + bmcMacAddressAfterInsertHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.AfterSelectHook, bmcMacAddressAfterSelectHook) + if err = o.doAfterSelectHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterSelectHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterSelectHook function to empty object, but got: %#v", o) + } + bmcMacAddressAfterSelectHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.BeforeUpdateHook, bmcMacAddressBeforeUpdateHook) + if err = o.doBeforeUpdateHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeUpdateHook function to empty object, but got: %#v", o) + } + bmcMacAddressBeforeUpdateHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.AfterUpdateHook, bmcMacAddressAfterUpdateHook) + if err = o.doAfterUpdateHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterUpdateHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterUpdateHook function to empty object, but got: %#v", o) + } + bmcMacAddressAfterUpdateHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.BeforeDeleteHook, bmcMacAddressBeforeDeleteHook) + if err = o.doBeforeDeleteHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeDeleteHook function to empty object, but got: %#v", o) + } + bmcMacAddressBeforeDeleteHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.AfterDeleteHook, bmcMacAddressAfterDeleteHook) + if err = o.doAfterDeleteHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterDeleteHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterDeleteHook function to empty object, but got: %#v", o) + } + bmcMacAddressAfterDeleteHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.BeforeUpsertHook, bmcMacAddressBeforeUpsertHook) + if err = o.doBeforeUpsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeUpsertHook function to empty object, but got: %#v", o) + } + bmcMacAddressBeforeUpsertHooks = []BMCMacAddressHook{} + + AddBMCMacAddressHook(boil.AfterUpsertHook, bmcMacAddressAfterUpsertHook) + if err = o.doAfterUpsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterUpsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterUpsertHook function to empty object, but got: %#v", o) + } + bmcMacAddressAfterUpsertHooks = []BMCMacAddressHook{} +} + +func testBMCMacAddressesInsert(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testBMCMacAddressesInsertWhitelist(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Whitelist(bmcMacAddressColumnsWithoutDefault...)); err != nil { + t.Error(err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testBMCMacAddressToOneBomInfoUsingSerialNumBomInfo(t *testing.T) { + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var local BMCMacAddress + var foreign BomInfo + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &local, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + if err := randomize.Struct(seed, &foreign, bomInfoDBTypes, false, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + local.SerialNum = foreign.SerialNum + if err := local.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + check, err := local.SerialNumBomInfo().One(ctx, tx) + if err != nil { + t.Fatal(err) + } + + if check.SerialNum != foreign.SerialNum { + t.Errorf("want: %v, got %v", foreign.SerialNum, check.SerialNum) + } + + slice := BMCMacAddressSlice{&local} + if err = local.L.LoadSerialNumBomInfo(ctx, tx, false, (*[]*BMCMacAddress)(&slice), nil); err != nil { + t.Fatal(err) + } + if local.R.SerialNumBomInfo == nil { + t.Error("struct should have been eager loaded") + } + + local.R.SerialNumBomInfo = nil + if err = local.L.LoadSerialNumBomInfo(ctx, tx, true, &local, nil); err != nil { + t.Fatal(err) + } + if local.R.SerialNumBomInfo == nil { + t.Error("struct should have been eager loaded") + } +} + +func testBMCMacAddressToOneSetOpBomInfoUsingSerialNumBomInfo(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a BMCMacAddress + var b, c BomInfo + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, bmcMacAddressDBTypes, false, strmangle.SetComplement(bmcMacAddressPrimaryKeyColumns, bmcMacAddressColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &b, bomInfoDBTypes, false, strmangle.SetComplement(bomInfoPrimaryKeyColumns, bomInfoColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, bomInfoDBTypes, false, strmangle.SetComplement(bomInfoPrimaryKeyColumns, bomInfoColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + + if err := a.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + if err = b.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + for i, x := range []*BomInfo{&b, &c} { + err = a.SetSerialNumBomInfo(ctx, tx, i != 0, x) + if err != nil { + t.Fatal(err) + } + + if a.R.SerialNumBomInfo != x { + t.Error("relationship struct not set to correct value") + } + + if x.R.SerialNumBMCMacAddresses[0] != &a { + t.Error("failed to append to foreign relationship struct") + } + if a.SerialNum != x.SerialNum { + t.Error("foreign key was wrong value", a.SerialNum) + } + + zero := reflect.Zero(reflect.TypeOf(a.SerialNum)) + reflect.Indirect(reflect.ValueOf(&a.SerialNum)).Set(zero) + + if err = a.Reload(ctx, tx); err != nil { + t.Fatal("failed to reload", err) + } + + if a.SerialNum != x.SerialNum { + t.Error("foreign key was wrong value", a.SerialNum, x.SerialNum) + } + } +} + +func testBMCMacAddressesReload(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if err = o.Reload(ctx, tx); err != nil { + t.Error(err) + } +} + +func testBMCMacAddressesReloadAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice := BMCMacAddressSlice{o} + + if err = slice.ReloadAll(ctx, tx); err != nil { + t.Error(err) + } +} + +func testBMCMacAddressesSelect(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := BMCMacAddresses().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 1 { + t.Error("want one record, got:", len(slice)) + } +} + +var ( + bmcMacAddressDBTypes = map[string]string{`BMCMacAddress`: `string`, `SerialNum`: `string`} + _ = bytes.MinRead +) + +func testBMCMacAddressesUpdate(t *testing.T) { + t.Parallel() + + if 0 == len(bmcMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with no primary key columns") + } + if len(bmcMacAddressAllColumns) == len(bmcMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } + + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + if rowsAff, err := o.Update(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only affect one row but affected", rowsAff) + } +} + +func testBMCMacAddressesSliceUpdateAll(t *testing.T) { + t.Parallel() + + if len(bmcMacAddressAllColumns) == len(bmcMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &BMCMacAddress{} + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := BMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } + + if err = randomize.Struct(seed, o, bmcMacAddressDBTypes, true, bmcMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BMCMacAddress struct: %s", err) + } + + // Remove Primary keys and unique columns from what we plan to update + var fields []string + if strmangle.StringSliceMatch(bmcMacAddressAllColumns, bmcMacAddressPrimaryKeyColumns) { + fields = bmcMacAddressAllColumns + } else { + fields = strmangle.SetComplement( + bmcMacAddressAllColumns, + bmcMacAddressPrimaryKeyColumns, + ) + } + + value := reflect.Indirect(reflect.ValueOf(o)) + typ := reflect.TypeOf(o).Elem() + n := typ.NumField() + + updateMap := M{} + for _, col := range fields { + for i := 0; i < n; i++ { + f := typ.Field(i) + if f.Tag.Get("boil") == col { + updateMap[col] = value.Field(i).Interface() + } + } + } + + slice := BMCMacAddressSlice{o} + if rowsAff, err := slice.UpdateAll(ctx, tx, updateMap); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("wanted one record updated but got", rowsAff) + } +} diff --git a/internal/models/boil_suites_test.go b/internal/models/boil_suites_test.go index 348b3db4..5f58c379 100644 --- a/internal/models/boil_suites_test.go +++ b/internal/models/boil_suites_test.go @@ -15,6 +15,7 @@ func TestParent(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddresses) t.Run("Attributes", testAttributes) t.Run("AttributesFirmwareSets", testAttributesFirmwareSets) + t.Run("BMCMacAddresses", testBMCMacAddresses) t.Run("BomInfos", testBomInfos) t.Run("ComponentFirmwareSets", testComponentFirmwareSets) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMaps) @@ -43,6 +44,7 @@ func TestDelete(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesDelete) t.Run("Attributes", testAttributesDelete) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsDelete) + t.Run("BMCMacAddresses", testBMCMacAddressesDelete) t.Run("BomInfos", testBomInfosDelete) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsDelete) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsDelete) @@ -59,6 +61,7 @@ func TestQueryDeleteAll(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesQueryDeleteAll) t.Run("Attributes", testAttributesQueryDeleteAll) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsQueryDeleteAll) + t.Run("BMCMacAddresses", testBMCMacAddressesQueryDeleteAll) t.Run("BomInfos", testBomInfosQueryDeleteAll) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsQueryDeleteAll) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsQueryDeleteAll) @@ -75,6 +78,7 @@ func TestSliceDeleteAll(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesSliceDeleteAll) t.Run("Attributes", testAttributesSliceDeleteAll) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsSliceDeleteAll) + t.Run("BMCMacAddresses", testBMCMacAddressesSliceDeleteAll) t.Run("BomInfos", testBomInfosSliceDeleteAll) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsSliceDeleteAll) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsSliceDeleteAll) @@ -91,6 +95,7 @@ func TestExists(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesExists) t.Run("Attributes", testAttributesExists) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsExists) + t.Run("BMCMacAddresses", testBMCMacAddressesExists) t.Run("BomInfos", testBomInfosExists) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsExists) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsExists) @@ -107,6 +112,7 @@ func TestFind(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesFind) t.Run("Attributes", testAttributesFind) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsFind) + t.Run("BMCMacAddresses", testBMCMacAddressesFind) t.Run("BomInfos", testBomInfosFind) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsFind) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsFind) @@ -123,6 +129,7 @@ func TestBind(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesBind) t.Run("Attributes", testAttributesBind) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsBind) + t.Run("BMCMacAddresses", testBMCMacAddressesBind) t.Run("BomInfos", testBomInfosBind) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsBind) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsBind) @@ -139,6 +146,7 @@ func TestOne(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesOne) t.Run("Attributes", testAttributesOne) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsOne) + t.Run("BMCMacAddresses", testBMCMacAddressesOne) t.Run("BomInfos", testBomInfosOne) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsOne) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsOne) @@ -155,6 +163,7 @@ func TestAll(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesAll) t.Run("Attributes", testAttributesAll) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsAll) + t.Run("BMCMacAddresses", testBMCMacAddressesAll) t.Run("BomInfos", testBomInfosAll) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsAll) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsAll) @@ -171,6 +180,7 @@ func TestCount(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesCount) t.Run("Attributes", testAttributesCount) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsCount) + t.Run("BMCMacAddresses", testBMCMacAddressesCount) t.Run("BomInfos", testBomInfosCount) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsCount) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsCount) @@ -187,6 +197,7 @@ func TestHooks(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesHooks) t.Run("Attributes", testAttributesHooks) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsHooks) + t.Run("BMCMacAddresses", testBMCMacAddressesHooks) t.Run("BomInfos", testBomInfosHooks) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsHooks) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsHooks) @@ -206,6 +217,8 @@ func TestInsert(t *testing.T) { t.Run("Attributes", testAttributesInsertWhitelist) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsInsert) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsInsertWhitelist) + t.Run("BMCMacAddresses", testBMCMacAddressesInsert) + t.Run("BMCMacAddresses", testBMCMacAddressesInsertWhitelist) t.Run("BomInfos", testBomInfosInsert) t.Run("BomInfos", testBomInfosInsertWhitelist) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsInsert) @@ -235,6 +248,7 @@ func TestToOne(t *testing.T) { t.Run("AttributeToServerUsingServer", testAttributeToOneServerUsingServer) t.Run("AttributeToServerComponentUsingServerComponent", testAttributeToOneServerComponentUsingServerComponent) t.Run("AttributesFirmwareSetToComponentFirmwareSetUsingFirmwareSet", testAttributesFirmwareSetToOneComponentFirmwareSetUsingFirmwareSet) + t.Run("BMCMacAddressToBomInfoUsingSerialNumBomInfo", testBMCMacAddressToOneBomInfoUsingSerialNumBomInfo) t.Run("ComponentFirmwareSetMapToComponentFirmwareSetUsingFirmwareSet", testComponentFirmwareSetMapToOneComponentFirmwareSetUsingFirmwareSet) t.Run("ComponentFirmwareSetMapToComponentFirmwareVersionUsingFirmware", testComponentFirmwareSetMapToOneComponentFirmwareVersionUsingFirmware) t.Run("ServerComponentToServerUsingServer", testServerComponentToOneServerUsingServer) @@ -253,6 +267,7 @@ func TestOneToOne(t *testing.T) {} // or deadlocks can occur. func TestToMany(t *testing.T) { t.Run("BomInfoToSerialNumAocMacAddresses", testBomInfoToManySerialNumAocMacAddresses) + t.Run("BomInfoToSerialNumBMCMacAddresses", testBomInfoToManySerialNumBMCMacAddresses) t.Run("ComponentFirmwareSetToFirmwareSetAttributesFirmwareSets", testComponentFirmwareSetToManyFirmwareSetAttributesFirmwareSets) t.Run("ComponentFirmwareSetToFirmwareSetComponentFirmwareSetMaps", testComponentFirmwareSetToManyFirmwareSetComponentFirmwareSetMaps) t.Run("ComponentFirmwareVersionToFirmwareComponentFirmwareSetMaps", testComponentFirmwareVersionToManyFirmwareComponentFirmwareSetMaps) @@ -273,6 +288,7 @@ func TestToOneSet(t *testing.T) { t.Run("AttributeToServerUsingAttributes", testAttributeToOneSetOpServerUsingServer) t.Run("AttributeToServerComponentUsingAttributes", testAttributeToOneSetOpServerComponentUsingServerComponent) t.Run("AttributesFirmwareSetToComponentFirmwareSetUsingFirmwareSetAttributesFirmwareSets", testAttributesFirmwareSetToOneSetOpComponentFirmwareSetUsingFirmwareSet) + t.Run("BMCMacAddressToBomInfoUsingSerialNumBMCMacAddresses", testBMCMacAddressToOneSetOpBomInfoUsingSerialNumBomInfo) t.Run("ComponentFirmwareSetMapToComponentFirmwareSetUsingFirmwareSetComponentFirmwareSetMaps", testComponentFirmwareSetMapToOneSetOpComponentFirmwareSetUsingFirmwareSet) t.Run("ComponentFirmwareSetMapToComponentFirmwareVersionUsingFirmwareComponentFirmwareSetMaps", testComponentFirmwareSetMapToOneSetOpComponentFirmwareVersionUsingFirmware) t.Run("ServerComponentToServerUsingServerComponents", testServerComponentToOneSetOpServerUsingServer) @@ -305,6 +321,7 @@ func TestOneToOneRemove(t *testing.T) {} // or deadlocks can occur. func TestToManyAdd(t *testing.T) { t.Run("BomInfoToSerialNumAocMacAddresses", testBomInfoToManyAddOpSerialNumAocMacAddresses) + t.Run("BomInfoToSerialNumBMCMacAddresses", testBomInfoToManyAddOpSerialNumBMCMacAddresses) t.Run("ComponentFirmwareSetToFirmwareSetAttributesFirmwareSets", testComponentFirmwareSetToManyAddOpFirmwareSetAttributesFirmwareSets) t.Run("ComponentFirmwareSetToFirmwareSetComponentFirmwareSetMaps", testComponentFirmwareSetToManyAddOpFirmwareSetComponentFirmwareSetMaps) t.Run("ComponentFirmwareVersionToFirmwareComponentFirmwareSetMaps", testComponentFirmwareVersionToManyAddOpFirmwareComponentFirmwareSetMaps) @@ -342,6 +359,7 @@ func TestReload(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesReload) t.Run("Attributes", testAttributesReload) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsReload) + t.Run("BMCMacAddresses", testBMCMacAddressesReload) t.Run("BomInfos", testBomInfosReload) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsReload) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsReload) @@ -358,6 +376,7 @@ func TestReloadAll(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesReloadAll) t.Run("Attributes", testAttributesReloadAll) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsReloadAll) + t.Run("BMCMacAddresses", testBMCMacAddressesReloadAll) t.Run("BomInfos", testBomInfosReloadAll) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsReloadAll) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsReloadAll) @@ -374,6 +393,7 @@ func TestSelect(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesSelect) t.Run("Attributes", testAttributesSelect) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsSelect) + t.Run("BMCMacAddresses", testBMCMacAddressesSelect) t.Run("BomInfos", testBomInfosSelect) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsSelect) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsSelect) @@ -390,6 +410,7 @@ func TestUpdate(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesUpdate) t.Run("Attributes", testAttributesUpdate) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsUpdate) + t.Run("BMCMacAddresses", testBMCMacAddressesUpdate) t.Run("BomInfos", testBomInfosUpdate) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsUpdate) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsUpdate) @@ -406,6 +427,7 @@ func TestSliceUpdateAll(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesSliceUpdateAll) t.Run("Attributes", testAttributesSliceUpdateAll) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsSliceUpdateAll) + t.Run("BMCMacAddresses", testBMCMacAddressesSliceUpdateAll) t.Run("BomInfos", testBomInfosSliceUpdateAll) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsSliceUpdateAll) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsSliceUpdateAll) diff --git a/internal/models/boil_table_names.go b/internal/models/boil_table_names.go index 76460db3..f8efec53 100644 --- a/internal/models/boil_table_names.go +++ b/internal/models/boil_table_names.go @@ -7,6 +7,7 @@ var TableNames = struct { AocMacAddress string Attributes string AttributesFirmwareSet string + BMCMacAddress string BomInfo string ComponentFirmwareSet string ComponentFirmwareSetMap string @@ -21,6 +22,7 @@ var TableNames = struct { AocMacAddress: "aoc_mac_address", Attributes: "attributes", AttributesFirmwareSet: "attributes_firmware_set", + BMCMacAddress: "bmc_mac_address", BomInfo: "bom_info", ComponentFirmwareSet: "component_firmware_set", ComponentFirmwareSetMap: "component_firmware_set_map", diff --git a/internal/models/bom_info.go b/internal/models/bom_info.go index 49ca2ae9..64c600a6 100644 --- a/internal/models/bom_info.go +++ b/internal/models/bom_info.go @@ -88,13 +88,16 @@ var BomInfoWhere = struct { // BomInfoRels is where relationship names are stored. var BomInfoRels = struct { SerialNumAocMacAddresses string + SerialNumBMCMacAddresses string }{ SerialNumAocMacAddresses: "SerialNumAocMacAddresses", + SerialNumBMCMacAddresses: "SerialNumBMCMacAddresses", } // bomInfoR is where relationships are stored. type bomInfoR struct { SerialNumAocMacAddresses AocMacAddressSlice `boil:"SerialNumAocMacAddresses" json:"SerialNumAocMacAddresses" toml:"SerialNumAocMacAddresses" yaml:"SerialNumAocMacAddresses"` + SerialNumBMCMacAddresses BMCMacAddressSlice `boil:"SerialNumBMCMacAddresses" json:"SerialNumBMCMacAddresses" toml:"SerialNumBMCMacAddresses" yaml:"SerialNumBMCMacAddresses"` } // NewStruct creates a new relationship struct @@ -109,6 +112,13 @@ func (r *bomInfoR) GetSerialNumAocMacAddresses() AocMacAddressSlice { return r.SerialNumAocMacAddresses } +func (r *bomInfoR) GetSerialNumBMCMacAddresses() BMCMacAddressSlice { + if r == nil { + return nil + } + return r.SerialNumBMCMacAddresses +} + // bomInfoL is where Load methods for each relationship are stored. type bomInfoL struct{} @@ -412,6 +422,20 @@ func (o *BomInfo) SerialNumAocMacAddresses(mods ...qm.QueryMod) aocMacAddressQue return AocMacAddresses(queryMods...) } +// SerialNumBMCMacAddresses retrieves all the bmc_mac_address's BMCMacAddresses with an executor via serial_num column. +func (o *BomInfo) SerialNumBMCMacAddresses(mods ...qm.QueryMod) bmcMacAddressQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"bmc_mac_address\".\"serial_num\"=?", o.SerialNum), + ) + + return BMCMacAddresses(queryMods...) +} + // LoadSerialNumAocMacAddresses allows an eager lookup of values, cached into the // loaded structs of the objects. This is for a 1-M or N-M relationship. func (bomInfoL) LoadSerialNumAocMacAddresses(ctx context.Context, e boil.ContextExecutor, singular bool, maybeBomInfo interface{}, mods queries.Applicator) error { @@ -510,6 +534,104 @@ func (bomInfoL) LoadSerialNumAocMacAddresses(ctx context.Context, e boil.Context return nil } +// LoadSerialNumBMCMacAddresses allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for a 1-M or N-M relationship. +func (bomInfoL) LoadSerialNumBMCMacAddresses(ctx context.Context, e boil.ContextExecutor, singular bool, maybeBomInfo interface{}, mods queries.Applicator) error { + var slice []*BomInfo + var object *BomInfo + + if singular { + object = maybeBomInfo.(*BomInfo) + } else { + slice = *maybeBomInfo.(*[]*BomInfo) + } + + args := make([]interface{}, 0, 1) + if singular { + if object.R == nil { + object.R = &bomInfoR{} + } + args = append(args, object.SerialNum) + } else { + Outer: + for _, obj := range slice { + if obj.R == nil { + obj.R = &bomInfoR{} + } + + for _, a := range args { + if a == obj.SerialNum { + continue Outer + } + } + + args = append(args, obj.SerialNum) + } + } + + if len(args) == 0 { + return nil + } + + query := NewQuery( + qm.From(`bmc_mac_address`), + qm.WhereIn(`bmc_mac_address.serial_num in ?`, args...), + ) + if mods != nil { + mods.Apply(query) + } + + results, err := query.QueryContext(ctx, e) + if err != nil { + return errors.Wrap(err, "failed to eager load bmc_mac_address") + } + + var resultSlice []*BMCMacAddress + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice bmc_mac_address") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on bmc_mac_address") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for bmc_mac_address") + } + + if len(bmcMacAddressAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.SerialNumBMCMacAddresses = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &bmcMacAddressR{} + } + foreign.R.SerialNumBomInfo = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.SerialNum == foreign.SerialNum { + local.R.SerialNumBMCMacAddresses = append(local.R.SerialNumBMCMacAddresses, foreign) + if foreign.R == nil { + foreign.R = &bmcMacAddressR{} + } + foreign.R.SerialNumBomInfo = local + break + } + } + } + + return nil +} + // AddSerialNumAocMacAddresses adds the given related objects to the existing relationships // of the bom_info, optionally inserting them as new records. // Appends related to o.R.SerialNumAocMacAddresses. @@ -563,6 +685,59 @@ func (o *BomInfo) AddSerialNumAocMacAddresses(ctx context.Context, exec boil.Con return nil } +// AddSerialNumBMCMacAddresses adds the given related objects to the existing relationships +// of the bom_info, optionally inserting them as new records. +// Appends related to o.R.SerialNumBMCMacAddresses. +// Sets related.R.SerialNumBomInfo appropriately. +func (o *BomInfo) AddSerialNumBMCMacAddresses(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*BMCMacAddress) error { + var err error + for _, rel := range related { + if insert { + rel.SerialNum = o.SerialNum + if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } else { + updateQuery := fmt.Sprintf( + "UPDATE \"bmc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"serial_num"}), + strmangle.WhereClause("\"", "\"", 2, bmcMacAddressPrimaryKeyColumns), + ) + values := []interface{}{o.SerialNum, rel.BMCMacAddress} + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, updateQuery) + fmt.Fprintln(writer, values) + } + if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { + return errors.Wrap(err, "failed to update foreign table") + } + + rel.SerialNum = o.SerialNum + } + } + + if o.R == nil { + o.R = &bomInfoR{ + SerialNumBMCMacAddresses: related, + } + } else { + o.R.SerialNumBMCMacAddresses = append(o.R.SerialNumBMCMacAddresses, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &bmcMacAddressR{ + SerialNumBomInfo: o, + } + } else { + rel.R.SerialNumBomInfo = o + } + } + return nil +} + // BomInfos retrieves all the records using an executor. func BomInfos(mods ...qm.QueryMod) bomInfoQuery { mods = append(mods, qm.From("\"bom_info\"")) diff --git a/internal/models/bom_info_test.go b/internal/models/bom_info_test.go index 4a716dcb..e1dbd28d 100644 --- a/internal/models/bom_info_test.go +++ b/internal/models/bom_info_test.go @@ -620,6 +620,84 @@ func testBomInfoToManySerialNumAocMacAddresses(t *testing.T) { } } +func testBomInfoToManySerialNumBMCMacAddresses(t *testing.T) { + var err error + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a BomInfo + var b, c BMCMacAddress + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + if err := a.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + if err = randomize.Struct(seed, &b, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, bmcMacAddressDBTypes, false, bmcMacAddressColumnsWithDefault...); err != nil { + t.Fatal(err) + } + + b.SerialNum = a.SerialNum + c.SerialNum = a.SerialNum + + if err = b.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + if err = c.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + check, err := a.SerialNumBMCMacAddresses().All(ctx, tx) + if err != nil { + t.Fatal(err) + } + + bFound, cFound := false, false + for _, v := range check { + if v.SerialNum == b.SerialNum { + bFound = true + } + if v.SerialNum == c.SerialNum { + cFound = true + } + } + + if !bFound { + t.Error("expected to find b") + } + if !cFound { + t.Error("expected to find c") + } + + slice := BomInfoSlice{&a} + if err = a.L.LoadSerialNumBMCMacAddresses(ctx, tx, false, (*[]*BomInfo)(&slice), nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.SerialNumBMCMacAddresses); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + a.R.SerialNumBMCMacAddresses = nil + if err = a.L.LoadSerialNumBMCMacAddresses(ctx, tx, true, &a, nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.SerialNumBMCMacAddresses); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + if t.Failed() { + t.Logf("%#v", check) + } +} + func testBomInfoToManyAddOpSerialNumAocMacAddresses(t *testing.T) { var err error @@ -695,6 +773,81 @@ func testBomInfoToManyAddOpSerialNumAocMacAddresses(t *testing.T) { } } } +func testBomInfoToManyAddOpSerialNumBMCMacAddresses(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a BomInfo + var b, c, d, e BMCMacAddress + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, bomInfoDBTypes, false, strmangle.SetComplement(bomInfoPrimaryKeyColumns, bomInfoColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + foreigners := []*BMCMacAddress{&b, &c, &d, &e} + for _, x := range foreigners { + if err = randomize.Struct(seed, x, bmcMacAddressDBTypes, false, strmangle.SetComplement(bmcMacAddressPrimaryKeyColumns, bmcMacAddressColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + } + + if err := a.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + if err = b.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + if err = c.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + foreignersSplitByInsertion := [][]*BMCMacAddress{ + {&b, &c}, + {&d, &e}, + } + + for i, x := range foreignersSplitByInsertion { + err = a.AddSerialNumBMCMacAddresses(ctx, tx, i != 0, x...) + if err != nil { + t.Fatal(err) + } + + first := x[0] + second := x[1] + + if a.SerialNum != first.SerialNum { + t.Error("foreign key was wrong value", a.SerialNum, first.SerialNum) + } + if a.SerialNum != second.SerialNum { + t.Error("foreign key was wrong value", a.SerialNum, second.SerialNum) + } + + if first.R.SerialNumBomInfo != &a { + t.Error("relationship was not added properly to the foreign slice") + } + if second.R.SerialNumBomInfo != &a { + t.Error("relationship was not added properly to the foreign slice") + } + + if a.R.SerialNumBMCMacAddresses[i*2] != first { + t.Error("relationship struct slice not set to correct value") + } + if a.R.SerialNumBMCMacAddresses[i*2+1] != second { + t.Error("relationship struct slice not set to correct value") + } + + count, err := a.SerialNumBMCMacAddresses().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if want := int64((i + 1) * 2); count != want { + t.Error("want", want, "got", count) + } + } +} func testBomInfosReload(t *testing.T) { t.Parallel() diff --git a/internal/models/crdb_suites_test.go b/internal/models/crdb_suites_test.go index 880df390..1ebf0ae8 100644 --- a/internal/models/crdb_suites_test.go +++ b/internal/models/crdb_suites_test.go @@ -9,6 +9,7 @@ func TestUpsert(t *testing.T) { t.Run("AocMacAddresses", testAocMacAddressesUpsert) t.Run("Attributes", testAttributesUpsert) t.Run("AttributesFirmwareSets", testAttributesFirmwareSetsUpsert) + t.Run("BMCMacAddresses", testBMCMacAddressesUpsert) t.Run("BomInfos", testBomInfosUpsert) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsUpsert) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsUpsert)