From f4db7a2cdfb4193903dadc89bed8283041bb0f6f Mon Sep 17 00:00:00 2001 From: Alva Zhang Date: Wed, 16 Aug 2023 17:22:47 -0700 Subject: [PATCH] =?UTF-8?q?=E2=80=9Crebase=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrations/00021_bom.sql | 33 + internal/models/aoc_mac_address.go | 1085 +++++++++++++++++++ internal/models/aoc_mac_address_test.go | 841 +++++++++++++++ internal/models/attributes.go | 23 - internal/models/bmc_mac_address.go | 1062 +++++++++++++++++++ internal/models/bmc_mac_address_test.go | 841 +++++++++++++++ internal/models/boil_suites_test.go | 62 ++ internal/models/boil_table_names.go | 6 + internal/models/bom_info.go | 1269 +++++++++++++++++++++++ internal/models/bom_info_test.go | 1039 +++++++++++++++++++ internal/models/crdb_suites_test.go | 3 + 11 files changed, 6241 insertions(+), 23 deletions(-) create mode 100644 db/migrations/00021_bom.sql create mode 100644 internal/models/aoc_mac_address.go create mode 100644 internal/models/aoc_mac_address_test.go create mode 100644 internal/models/bmc_mac_address.go create mode 100644 internal/models/bmc_mac_address_test.go create mode 100644 internal/models/bom_info.go create mode 100644 internal/models/bom_info_test.go diff --git a/db/migrations/00021_bom.sql b/db/migrations/00021_bom.sql new file mode 100644 index 00000000..92cdae56 --- /dev/null +++ b/db/migrations/00021_bom.sql @@ -0,0 +1,33 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE bom_info ( + serial_num STRING PRIMARY KEY NOT NULL, + aoc_mac_address STRING NULL, + bmc_mac_address STRING NULL, + num_defi_pmi STRING NULL, + num_def_pwd STRING NULL, + metro STRING NULL +); + +CREATE TABLE aoc_mac_address ( + aoc_mac_address STRING PRIMARY KEY NOT NULL, + serial_num STRING NOT NULL REFERENCES bom_info(serial_num) ON DELETE CASCADE +); + +CREATE TABLE bmc_mac_address ( + bmc_mac_address STRING PRIMARY KEY NOT NULL, + serial_num STRING NOT NULL REFERENCES bom_info(serial_num) ON DELETE CASCADE +); + + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin + +DROP TABLE bom_info; +DROP TABLE aoc_mac_address; +DROP TABLE bmc_mac_address; + +-- +goose StatementEnd \ No newline at end of file diff --git a/internal/models/aoc_mac_address.go b/internal/models/aoc_mac_address.go new file mode 100644 index 00000000..2a8d14b3 --- /dev/null +++ b/internal/models/aoc_mac_address.go @@ -0,0 +1,1085 @@ +// 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" +) + +// AocMacAddress is an object representing the database table. +type AocMacAddress struct { + AocMacAddress string `boil:"aoc_mac_address" json:"aoc_mac_address" toml:"aoc_mac_address" yaml:"aoc_mac_address"` + SerialNum string `boil:"serial_num" json:"serial_num" toml:"serial_num" yaml:"serial_num"` + + R *aocMacAddressR `boil:"-" json:"-" toml:"-" yaml:"-"` + L aocMacAddressL `boil:"-" json:"-" toml:"-" yaml:"-"` +} + +var AocMacAddressColumns = struct { + AocMacAddress string + SerialNum string +}{ + AocMacAddress: "aoc_mac_address", + SerialNum: "serial_num", +} + +var AocMacAddressTableColumns = struct { + AocMacAddress string + SerialNum string +}{ + AocMacAddress: "aoc_mac_address.aoc_mac_address", + SerialNum: "aoc_mac_address.serial_num", +} + +// Generated where + +type whereHelperstring struct{ field string } + +func (w whereHelperstring) EQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } +func (w whereHelperstring) NEQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } +func (w whereHelperstring) LT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } +func (w whereHelperstring) LTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } +func (w whereHelperstring) GT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } +func (w whereHelperstring) GTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } +func (w whereHelperstring) IN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) +} +func (w whereHelperstring) NIN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) +} + +var AocMacAddressWhere = struct { + AocMacAddress whereHelperstring + SerialNum whereHelperstring +}{ + AocMacAddress: whereHelperstring{field: "\"aoc_mac_address\".\"aoc_mac_address\""}, + SerialNum: whereHelperstring{field: "\"aoc_mac_address\".\"serial_num\""}, +} + +// AocMacAddressRels is where relationship names are stored. +var AocMacAddressRels = struct { + SerialNumBomInfo string +}{ + SerialNumBomInfo: "SerialNumBomInfo", +} + +// aocMacAddressR is where relationships are stored. +type aocMacAddressR struct { + SerialNumBomInfo *BomInfo `boil:"SerialNumBomInfo" json:"SerialNumBomInfo" toml:"SerialNumBomInfo" yaml:"SerialNumBomInfo"` +} + +// NewStruct creates a new relationship struct +func (*aocMacAddressR) NewStruct() *aocMacAddressR { + return &aocMacAddressR{} +} + +func (r *aocMacAddressR) GetSerialNumBomInfo() *BomInfo { + if r == nil { + return nil + } + return r.SerialNumBomInfo +} + +// aocMacAddressL is where Load methods for each relationship are stored. +type aocMacAddressL struct{} + +var ( + aocMacAddressAllColumns = []string{"aoc_mac_address", "serial_num"} + aocMacAddressColumnsWithoutDefault = []string{"aoc_mac_address", "serial_num"} + aocMacAddressColumnsWithDefault = []string{} + aocMacAddressPrimaryKeyColumns = []string{"aoc_mac_address"} + aocMacAddressGeneratedColumns = []string{} +) + +type ( + // AocMacAddressSlice is an alias for a slice of pointers to AocMacAddress. + // This should almost always be used instead of []AocMacAddress. + AocMacAddressSlice []*AocMacAddress + // AocMacAddressHook is the signature for custom AocMacAddress hook methods + AocMacAddressHook func(context.Context, boil.ContextExecutor, *AocMacAddress) error + + aocMacAddressQuery struct { + *queries.Query + } +) + +// Cache for insert, update and upsert +var ( + aocMacAddressType = reflect.TypeOf(&AocMacAddress{}) + aocMacAddressMapping = queries.MakeStructMapping(aocMacAddressType) + aocMacAddressPrimaryKeyMapping, _ = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, aocMacAddressPrimaryKeyColumns) + aocMacAddressInsertCacheMut sync.RWMutex + aocMacAddressInsertCache = make(map[string]insertCache) + aocMacAddressUpdateCacheMut sync.RWMutex + aocMacAddressUpdateCache = make(map[string]updateCache) + aocMacAddressUpsertCacheMut sync.RWMutex + aocMacAddressUpsertCache = 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 aocMacAddressAfterSelectHooks []AocMacAddressHook + +var aocMacAddressBeforeInsertHooks []AocMacAddressHook +var aocMacAddressAfterInsertHooks []AocMacAddressHook + +var aocMacAddressBeforeUpdateHooks []AocMacAddressHook +var aocMacAddressAfterUpdateHooks []AocMacAddressHook + +var aocMacAddressBeforeDeleteHooks []AocMacAddressHook +var aocMacAddressAfterDeleteHooks []AocMacAddressHook + +var aocMacAddressBeforeUpsertHooks []AocMacAddressHook +var aocMacAddressAfterUpsertHooks []AocMacAddressHook + +// doAfterSelectHooks executes all "after Select" hooks. +func (o *AocMacAddress) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressAfterSelectHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeInsertHooks executes all "before insert" hooks. +func (o *AocMacAddress) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressBeforeInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterInsertHooks executes all "after Insert" hooks. +func (o *AocMacAddress) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressAfterInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpdateHooks executes all "before Update" hooks. +func (o *AocMacAddress) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressBeforeUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpdateHooks executes all "after Update" hooks. +func (o *AocMacAddress) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressAfterUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeDeleteHooks executes all "before Delete" hooks. +func (o *AocMacAddress) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressBeforeDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterDeleteHooks executes all "after Delete" hooks. +func (o *AocMacAddress) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressAfterDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpsertHooks executes all "before Upsert" hooks. +func (o *AocMacAddress) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressBeforeUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpsertHooks executes all "after Upsert" hooks. +func (o *AocMacAddress) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range aocMacAddressAfterUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// AddAocMacAddressHook registers your hook function for all future operations. +func AddAocMacAddressHook(hookPoint boil.HookPoint, aocMacAddressHook AocMacAddressHook) { + switch hookPoint { + case boil.AfterSelectHook: + aocMacAddressAfterSelectHooks = append(aocMacAddressAfterSelectHooks, aocMacAddressHook) + case boil.BeforeInsertHook: + aocMacAddressBeforeInsertHooks = append(aocMacAddressBeforeInsertHooks, aocMacAddressHook) + case boil.AfterInsertHook: + aocMacAddressAfterInsertHooks = append(aocMacAddressAfterInsertHooks, aocMacAddressHook) + case boil.BeforeUpdateHook: + aocMacAddressBeforeUpdateHooks = append(aocMacAddressBeforeUpdateHooks, aocMacAddressHook) + case boil.AfterUpdateHook: + aocMacAddressAfterUpdateHooks = append(aocMacAddressAfterUpdateHooks, aocMacAddressHook) + case boil.BeforeDeleteHook: + aocMacAddressBeforeDeleteHooks = append(aocMacAddressBeforeDeleteHooks, aocMacAddressHook) + case boil.AfterDeleteHook: + aocMacAddressAfterDeleteHooks = append(aocMacAddressAfterDeleteHooks, aocMacAddressHook) + case boil.BeforeUpsertHook: + aocMacAddressBeforeUpsertHooks = append(aocMacAddressBeforeUpsertHooks, aocMacAddressHook) + case boil.AfterUpsertHook: + aocMacAddressAfterUpsertHooks = append(aocMacAddressAfterUpsertHooks, aocMacAddressHook) + } +} + +// One returns a single aocMacAddress record from the query. +func (q aocMacAddressQuery) One(ctx context.Context, exec boil.ContextExecutor) (*AocMacAddress, error) { + o := &AocMacAddress{} + + 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 aoc_mac_address") + } + + if err := o.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + + return o, nil +} + +// All returns all AocMacAddress records from the query. +func (q aocMacAddressQuery) All(ctx context.Context, exec boil.ContextExecutor) (AocMacAddressSlice, error) { + var o []*AocMacAddress + + err := q.Bind(ctx, exec, &o) + if err != nil { + return nil, errors.Wrap(err, "models: failed to assign all query results to AocMacAddress slice") + } + + if len(aocMacAddressAfterSelectHooks) != 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 AocMacAddress records in the query. +func (q aocMacAddressQuery) 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 aoc_mac_address rows") + } + + return count, nil +} + +// Exists checks if the row exists in the table. +func (q aocMacAddressQuery) 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 aoc_mac_address exists") + } + + return count > 0, nil +} + +// SerialNumBomInfo pointed to by the foreign key. +func (o *AocMacAddress) 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 (aocMacAddressL) LoadSerialNumBomInfo(ctx context.Context, e boil.ContextExecutor, singular bool, maybeAocMacAddress interface{}, mods queries.Applicator) error { + var slice []*AocMacAddress + var object *AocMacAddress + + if singular { + object = maybeAocMacAddress.(*AocMacAddress) + } else { + slice = *maybeAocMacAddress.(*[]*AocMacAddress) + } + + args := make([]interface{}, 0, 1) + if singular { + if object.R == nil { + object.R = &aocMacAddressR{} + } + args = append(args, object.SerialNum) + + } else { + Outer: + for _, obj := range slice { + if obj.R == nil { + obj.R = &aocMacAddressR{} + } + + 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(aocMacAddressAfterSelectHooks) != 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.SerialNumAocMacAddresses = append(foreign.R.SerialNumAocMacAddresses, 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.SerialNumAocMacAddresses = append(foreign.R.SerialNumAocMacAddresses, local) + break + } + } + } + + return nil +} + +// SetSerialNumBomInfo of the aocMacAddress to the related item. +// Sets o.R.SerialNumBomInfo to related. +// Adds o to related.R.SerialNumAocMacAddresses. +func (o *AocMacAddress) 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 \"aoc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"serial_num"}), + strmangle.WhereClause("\"", "\"", 2, aocMacAddressPrimaryKeyColumns), + ) + values := []interface{}{related.SerialNum, o.AocMacAddress} + + 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 = &aocMacAddressR{ + SerialNumBomInfo: related, + } + } else { + o.R.SerialNumBomInfo = related + } + + if related.R == nil { + related.R = &bomInfoR{ + SerialNumAocMacAddresses: AocMacAddressSlice{o}, + } + } else { + related.R.SerialNumAocMacAddresses = append(related.R.SerialNumAocMacAddresses, o) + } + + return nil +} + +// AocMacAddresses retrieves all the records using an executor. +func AocMacAddresses(mods ...qm.QueryMod) aocMacAddressQuery { + mods = append(mods, qm.From("\"aoc_mac_address\"")) + q := NewQuery(mods...) + if len(queries.GetSelect(q)) == 0 { + queries.SetSelect(q, []string{"\"aoc_mac_address\".*"}) + } + + return aocMacAddressQuery{q} +} + +// FindAocMacAddress retrieves a single record by ID with an executor. +// If selectCols is empty Find will return all columns. +func FindAocMacAddress(ctx context.Context, exec boil.ContextExecutor, aocMacAddress string, selectCols ...string) (*AocMacAddress, error) { + aocMacAddressObj := &AocMacAddress{} + + sel := "*" + if len(selectCols) > 0 { + sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") + } + query := fmt.Sprintf( + "select %s from \"aoc_mac_address\" where \"aoc_mac_address\"=$1", sel, + ) + + q := queries.Raw(query, aocMacAddress) + + err := q.Bind(ctx, exec, aocMacAddressObj) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: unable to select from aoc_mac_address") + } + + if err = aocMacAddressObj.doAfterSelectHooks(ctx, exec); err != nil { + return aocMacAddressObj, err + } + + return aocMacAddressObj, nil +} + +// Insert a single record using an executor. +// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. +func (o *AocMacAddress) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { + if o == nil { + return errors.New("models: no aoc_mac_address provided for insertion") + } + + var err error + + if err := o.doBeforeInsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(aocMacAddressColumnsWithDefault, o) + + key := makeCacheKey(columns, nzDefaults) + aocMacAddressInsertCacheMut.RLock() + cache, cached := aocMacAddressInsertCache[key] + aocMacAddressInsertCacheMut.RUnlock() + + if !cached { + wl, returnColumns := columns.InsertColumnSet( + aocMacAddressAllColumns, + aocMacAddressColumnsWithDefault, + aocMacAddressColumnsWithoutDefault, + nzDefaults, + ) + + cache.valueMapping, err = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, wl) + if err != nil { + return err + } + cache.retMapping, err = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, returnColumns) + if err != nil { + return err + } + if len(wl) != 0 { + cache.query = fmt.Sprintf("INSERT INTO \"aoc_mac_address\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) + } else { + cache.query = "INSERT INTO \"aoc_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 aoc_mac_address") + } + + if !cached { + aocMacAddressInsertCacheMut.Lock() + aocMacAddressInsertCache[key] = cache + aocMacAddressInsertCacheMut.Unlock() + } + + return o.doAfterInsertHooks(ctx, exec) +} + +// Update uses an executor to update the AocMacAddress. +// 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 *AocMacAddress) 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) + aocMacAddressUpdateCacheMut.RLock() + cache, cached := aocMacAddressUpdateCache[key] + aocMacAddressUpdateCacheMut.RUnlock() + + if !cached { + wl := columns.UpdateColumnSet( + aocMacAddressAllColumns, + aocMacAddressPrimaryKeyColumns, + ) + + if !columns.IsWhitelist() { + wl = strmangle.SetComplement(wl, []string{"created_at"}) + } + if len(wl) == 0 { + return 0, errors.New("models: unable to update aoc_mac_address, could not build whitelist") + } + + cache.query = fmt.Sprintf("UPDATE \"aoc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, wl), + strmangle.WhereClause("\"", "\"", len(wl)+1, aocMacAddressPrimaryKeyColumns), + ) + cache.valueMapping, err = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, append(wl, aocMacAddressPrimaryKeyColumns...)) + 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 aoc_mac_address row") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by update for aoc_mac_address") + } + + if !cached { + aocMacAddressUpdateCacheMut.Lock() + aocMacAddressUpdateCache[key] = cache + aocMacAddressUpdateCacheMut.Unlock() + } + + return rowsAff, o.doAfterUpdateHooks(ctx, exec) +} + +// UpdateAll updates all rows with the specified column values. +func (q aocMacAddressQuery) 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 aoc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected for aoc_mac_address") + } + + return rowsAff, nil +} + +// UpdateAll updates all rows with the specified column values, using an executor. +func (o AocMacAddressSlice) 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)), aocMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := fmt.Sprintf("UPDATE \"aoc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, colNames), + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, aocMacAddressPrimaryKeyColumns, 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 aocMacAddress slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all aocMacAddress") + } + return rowsAff, nil +} + +// Delete deletes a single AocMacAddress record with an executor. +// Delete will match against the primary key column to find the record to delete. +func (o *AocMacAddress) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if o == nil { + return 0, errors.New("models: no AocMacAddress provided for delete") + } + + if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), aocMacAddressPrimaryKeyMapping) + sql := "DELETE FROM \"aoc_mac_address\" WHERE \"aoc_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 aoc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by delete for aoc_mac_address") + } + + if err := o.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + return rowsAff, nil +} + +// DeleteAll deletes all matching rows. +func (q aocMacAddressQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if q.Query == nil { + return 0, errors.New("models: no aocMacAddressQuery 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 aoc_mac_address") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for aoc_mac_address") + } + + return rowsAff, nil +} + +// DeleteAll deletes all rows in the slice, using an executor. +func (o AocMacAddressSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if len(o) == 0 { + return 0, nil + } + + if len(aocMacAddressBeforeDeleteHooks) != 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)), aocMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "DELETE FROM \"aoc_mac_address\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, aocMacAddressPrimaryKeyColumns, 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 aocMacAddress slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for aoc_mac_address") + } + + if len(aocMacAddressAfterDeleteHooks) != 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 *AocMacAddress) Reload(ctx context.Context, exec boil.ContextExecutor) error { + ret, err := FindAocMacAddress(ctx, exec, o.AocMacAddress) + 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 *AocMacAddressSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { + if o == nil || len(*o) == 0 { + return nil + } + + slice := AocMacAddressSlice{} + var args []interface{} + for _, obj := range *o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), aocMacAddressPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "SELECT \"aoc_mac_address\".* FROM \"aoc_mac_address\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, aocMacAddressPrimaryKeyColumns, 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 AocMacAddressSlice") + } + + *o = slice + + return nil +} + +// AocMacAddressExists checks if the AocMacAddress row exists. +func AocMacAddressExists(ctx context.Context, exec boil.ContextExecutor, aocMacAddress string) (bool, error) { + var exists bool + sql := "select exists(select 1 from \"aoc_mac_address\" where \"aoc_mac_address\"=$1 limit 1)" + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, aocMacAddress) + } + row := exec.QueryRowContext(ctx, sql, aocMacAddress) + + err := row.Scan(&exists) + if err != nil { + return false, errors.Wrap(err, "models: unable to check if aoc_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 *AocMacAddress) Upsert(ctx context.Context, exec boil.ContextExecutor, updateOnConflict bool, conflictColumns []string, updateColumns, insertColumns boil.Columns) error { + if o == nil { + return errors.New("models: no aoc_mac_address provided for upsert") + } + + if err := o.doBeforeUpsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(aocMacAddressColumnsWithDefault, 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) + + aocMacAddressUpsertCacheMut.RLock() + cache, cached := aocMacAddressUpsertCache[key] + aocMacAddressUpsertCacheMut.RUnlock() + + var err error + + if !cached { + insert, ret := insertColumns.InsertColumnSet( + aocMacAddressAllColumns, + aocMacAddressColumnsWithDefault, + aocMacAddressColumnsWithoutDefault, + nzDefaults, + ) + update := updateColumns.UpdateColumnSet( + aocMacAddressAllColumns, + aocMacAddressPrimaryKeyColumns, + ) + + if updateOnConflict && len(update) == 0 { + return errors.New("models: unable to upsert aoc_mac_address, could not build update column list") + } + + conflict := conflictColumns + if len(conflict) == 0 { + conflict = make([]string, len(aocMacAddressPrimaryKeyColumns)) + copy(conflict, aocMacAddressPrimaryKeyColumns) + } + cache.query = buildUpsertQueryCockroachDB(dialect, "\"aoc_mac_address\"", updateOnConflict, ret, update, conflict, insert) + + cache.valueMapping, err = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, insert) + if err != nil { + return err + } + if len(ret) != 0 { + cache.retMapping, err = queries.BindMapping(aocMacAddressType, aocMacAddressMapping, 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 aoc_mac_address") + } + + if !cached { + aocMacAddressUpsertCacheMut.Lock() + aocMacAddressUpsertCache[key] = cache + aocMacAddressUpsertCacheMut.Unlock() + } + + return o.doAfterUpsertHooks(ctx, exec) +} diff --git a/internal/models/aoc_mac_address_test.go b/internal/models/aoc_mac_address_test.go new file mode 100644 index 00000000..b7f287e1 --- /dev/null +++ b/internal/models/aoc_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 testAocMacAddressesUpsert(t *testing.T) { + t.Parallel() + + if len(aocMacAddressAllColumns) == len(aocMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + // Attempt the INSERT side of an UPSERT + o := AocMacAddress{} + if err = randomize.Struct(seed, &o, aocMacAddressDBTypes, true); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 AocMacAddress: %s", err) + } + + count, err := AocMacAddresses().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, aocMacAddressDBTypes, false, aocMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + + if err = o.Upsert(ctx, tx, true, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert AocMacAddress: %s", err) + } + + count, err = AocMacAddresses().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 testAocMacAddresses(t *testing.T) { + t.Parallel() + + query := AocMacAddresses() + + if query.Query == nil { + t.Error("expected a query, got nothing") + } +} + +func testAocMacAddressesDelete(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testAocMacAddressesQueryDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().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 := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testAocMacAddressesSliceDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddressSlice{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 := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testAocMacAddressesExists(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddressExists(ctx, tx, o.AocMacAddress) + if err != nil { + t.Errorf("Unable to check if AocMacAddress exists: %s", err) + } + if !e { + t.Errorf("Expected AocMacAddressExists to return true, but got false.") + } +} + +func testAocMacAddressesFind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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) + } + + aocMacAddressFound, err := FindAocMacAddress(ctx, tx, o.AocMacAddress) + if err != nil { + t.Error(err) + } + + if aocMacAddressFound == nil { + t.Error("want a record, got nil") + } +} + +func testAocMacAddressesBind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 = AocMacAddresses().Bind(ctx, tx, o); err != nil { + t.Error(err) + } +} + +func testAocMacAddressesOne(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().One(ctx, tx); err != nil { + t.Error(err) + } else if x == nil { + t.Error("expected to get a non nil record") + } +} + +func testAocMacAddressesAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + aocMacAddressOne := &AocMacAddress{} + aocMacAddressTwo := &AocMacAddress{} + if err = randomize.Struct(seed, aocMacAddressOne, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + if err = randomize.Struct(seed, aocMacAddressTwo, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = aocMacAddressOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = aocMacAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := AocMacAddresses().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 2 { + t.Error("want 2 records, got:", len(slice)) + } +} + +func testAocMacAddressesCount(t *testing.T) { + t.Parallel() + + var err error + seed := randomize.NewSeed() + aocMacAddressOne := &AocMacAddress{} + aocMacAddressTwo := &AocMacAddress{} + if err = randomize.Struct(seed, aocMacAddressOne, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + if err = randomize.Struct(seed, aocMacAddressTwo, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = aocMacAddressOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = aocMacAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 2 { + t.Error("want 2 records, got:", count) + } +} + +func aocMacAddressBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func aocMacAddressAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *AocMacAddress) error { + *o = AocMacAddress{} + return nil +} + +func testAocMacAddressesHooks(t *testing.T) { + t.Parallel() + + var err error + + ctx := context.Background() + empty := &AocMacAddress{} + o := &AocMacAddress{} + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, false); err != nil { + t.Errorf("Unable to randomize AocMacAddress object: %s", err) + } + + AddAocMacAddressHook(boil.BeforeInsertHook, aocMacAddressBeforeInsertHook) + 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) + } + aocMacAddressBeforeInsertHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.AfterInsertHook, aocMacAddressAfterInsertHook) + 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) + } + aocMacAddressAfterInsertHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.AfterSelectHook, aocMacAddressAfterSelectHook) + 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) + } + aocMacAddressAfterSelectHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.BeforeUpdateHook, aocMacAddressBeforeUpdateHook) + 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) + } + aocMacAddressBeforeUpdateHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.AfterUpdateHook, aocMacAddressAfterUpdateHook) + 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) + } + aocMacAddressAfterUpdateHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.BeforeDeleteHook, aocMacAddressBeforeDeleteHook) + 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) + } + aocMacAddressBeforeDeleteHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.AfterDeleteHook, aocMacAddressAfterDeleteHook) + 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) + } + aocMacAddressAfterDeleteHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.BeforeUpsertHook, aocMacAddressBeforeUpsertHook) + 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) + } + aocMacAddressBeforeUpsertHooks = []AocMacAddressHook{} + + AddAocMacAddressHook(boil.AfterUpsertHook, aocMacAddressAfterUpsertHook) + 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) + } + aocMacAddressAfterUpsertHooks = []AocMacAddressHook{} +} + +func testAocMacAddressesInsert(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testAocMacAddressesInsertWhitelist(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Whitelist(aocMacAddressColumnsWithoutDefault...)); err != nil { + t.Error(err) + } + + count, err := AocMacAddresses().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testAocMacAddressToOneBomInfoUsingSerialNumBomInfo(t *testing.T) { + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var local AocMacAddress + var foreign BomInfo + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &local, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddressSlice{&local} + if err = local.L.LoadSerialNumBomInfo(ctx, tx, false, (*[]*AocMacAddress)(&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 testAocMacAddressToOneSetOpBomInfoUsingSerialNumBomInfo(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a AocMacAddress + var b, c BomInfo + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, aocMacAddressDBTypes, false, strmangle.SetComplement(aocMacAddressPrimaryKeyColumns, aocMacAddressColumnsWithoutDefault)...); 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.SerialNumAocMacAddresses[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 testAocMacAddressesReload(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 testAocMacAddressesReloadAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddressSlice{o} + + if err = slice.ReloadAll(ctx, tx); err != nil { + t.Error(err) + } +} + +func testAocMacAddressesSelect(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 1 { + t.Error("want one record, got:", len(slice)) + } +} + +var ( + aocMacAddressDBTypes = map[string]string{`AocMacAddress`: `string`, `SerialNum`: `string`} + _ = bytes.MinRead +) + +func testAocMacAddressesUpdate(t *testing.T) { + t.Parallel() + + if 0 == len(aocMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with no primary key columns") + } + if len(aocMacAddressAllColumns) == len(aocMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().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, aocMacAddressDBTypes, true, aocMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 testAocMacAddressesSliceUpdateAll(t *testing.T) { + t.Parallel() + + if len(aocMacAddressAllColumns) == len(aocMacAddressPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &AocMacAddress{} + if err = randomize.Struct(seed, o, aocMacAddressDBTypes, true, aocMacAddressColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize AocMacAddress 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 := AocMacAddresses().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, aocMacAddressDBTypes, true, aocMacAddressPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize AocMacAddress struct: %s", err) + } + + // Remove Primary keys and unique columns from what we plan to update + var fields []string + if strmangle.StringSliceMatch(aocMacAddressAllColumns, aocMacAddressPrimaryKeyColumns) { + fields = aocMacAddressAllColumns + } else { + fields = strmangle.SetComplement( + aocMacAddressAllColumns, + aocMacAddressPrimaryKeyColumns, + ) + } + + 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 := AocMacAddressSlice{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/attributes.go b/internal/models/attributes.go index 91c933a7..b838690f 100644 --- a/internal/models/attributes.go +++ b/internal/models/attributes.go @@ -75,29 +75,6 @@ var AttributeTableColumns = struct { // Generated where -type whereHelperstring struct{ field string } - -func (w whereHelperstring) EQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } -func (w whereHelperstring) NEQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } -func (w whereHelperstring) LT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } -func (w whereHelperstring) LTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } -func (w whereHelperstring) GT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } -func (w whereHelperstring) GTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } -func (w whereHelperstring) IN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) -} -func (w whereHelperstring) NIN(slice []string) qm.QueryMod { - values := make([]interface{}, 0, len(slice)) - for _, value := range slice { - values = append(values, value) - } - return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) -} - type whereHelpernull_String struct{ field string } func (w whereHelpernull_String) EQ(x null.String) qm.QueryMod { 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 ed809aa7..5f58c379 100644 --- a/internal/models/boil_suites_test.go +++ b/internal/models/boil_suites_test.go @@ -12,8 +12,11 @@ import "testing" // It does NOT run each operation group in parallel. // Separating the tests thusly grants avoidance of Postgres deadlocks. 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersions) @@ -38,8 +41,11 @@ func TestSliceSoftDeleteAll(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsDelete) @@ -52,8 +58,11 @@ func TestDelete(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsQueryDeleteAll) @@ -66,8 +75,11 @@ func TestQueryDeleteAll(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsSliceDeleteAll) @@ -80,8 +92,11 @@ func TestSliceDeleteAll(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsExists) @@ -94,8 +109,11 @@ func TestExists(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsFind) @@ -108,8 +126,11 @@ func TestFind(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsBind) @@ -122,8 +143,11 @@ func TestBind(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsOne) @@ -136,8 +160,11 @@ func TestOne(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsAll) @@ -150,8 +177,11 @@ func TestAll(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsCount) @@ -164,8 +194,11 @@ func TestCount(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsHooks) @@ -178,10 +211,16 @@ func TestHooks(t *testing.T) { } func TestInsert(t *testing.T) { + t.Run("AocMacAddresses", testAocMacAddressesInsert) + t.Run("AocMacAddresses", testAocMacAddressesInsertWhitelist) t.Run("Attributes", testAttributesInsert) 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) t.Run("ComponentFirmwareSets", testComponentFirmwareSetsInsertWhitelist) t.Run("ComponentFirmwareSetMaps", testComponentFirmwareSetMapsInsert) @@ -205,9 +244,11 @@ func TestInsert(t *testing.T) { // TestToOne tests cannot be run in parallel // or deadlocks can occur. func TestToOne(t *testing.T) { + t.Run("AocMacAddressToBomInfoUsingSerialNumBomInfo", testAocMacAddressToOneBomInfoUsingSerialNumBomInfo) 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) @@ -225,6 +266,8 @@ func TestOneToOne(t *testing.T) {} // TestToMany tests cannot be run in parallel // 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) @@ -241,9 +284,11 @@ func TestToMany(t *testing.T) { // TestToOneSet tests cannot be run in parallel // or deadlocks can occur. func TestToOneSet(t *testing.T) { + t.Run("AocMacAddressToBomInfoUsingSerialNumAocMacAddresses", testAocMacAddressToOneSetOpBomInfoUsingSerialNumBomInfo) 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) @@ -275,6 +320,8 @@ func TestOneToOneRemove(t *testing.T) {} // TestToManyAdd tests cannot be run in parallel // 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) @@ -309,8 +356,11 @@ func TestToManyRemove(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsReload) @@ -323,8 +373,11 @@ func TestReload(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsReloadAll) @@ -337,8 +390,11 @@ func TestReloadAll(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsSelect) @@ -351,8 +407,11 @@ func TestSelect(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsUpdate) @@ -365,8 +424,11 @@ func TestUpdate(t *testing.T) { } 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsSliceUpdateAll) diff --git a/internal/models/boil_table_names.go b/internal/models/boil_table_names.go index 4f33a8de..f8efec53 100644 --- a/internal/models/boil_table_names.go +++ b/internal/models/boil_table_names.go @@ -4,8 +4,11 @@ package models var TableNames = struct { + AocMacAddress string Attributes string AttributesFirmwareSet string + BMCMacAddress string + BomInfo string ComponentFirmwareSet string ComponentFirmwareSetMap string ComponentFirmwareVersion string @@ -16,8 +19,11 @@ var TableNames = struct { Servers string VersionedAttributes string }{ + 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", ComponentFirmwareVersion: "component_firmware_version", diff --git a/internal/models/bom_info.go b/internal/models/bom_info.go new file mode 100644 index 00000000..64c600a6 --- /dev/null +++ b/internal/models/bom_info.go @@ -0,0 +1,1269 @@ +// 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/null/v8" + "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" +) + +// BomInfo is an object representing the database table. +type BomInfo struct { + SerialNum string `boil:"serial_num" json:"serial_num" toml:"serial_num" yaml:"serial_num"` + AocMacAddress null.String `boil:"aoc_mac_address" json:"aoc_mac_address,omitempty" toml:"aoc_mac_address" yaml:"aoc_mac_address,omitempty"` + BMCMacAddress null.String `boil:"bmc_mac_address" json:"bmc_mac_address,omitempty" toml:"bmc_mac_address" yaml:"bmc_mac_address,omitempty"` + NumDefiPmi null.String `boil:"num_defi_pmi" json:"num_defi_pmi,omitempty" toml:"num_defi_pmi" yaml:"num_defi_pmi,omitempty"` + NumDefPWD null.String `boil:"num_def_pwd" json:"num_def_pwd,omitempty" toml:"num_def_pwd" yaml:"num_def_pwd,omitempty"` + Metro null.String `boil:"metro" json:"metro,omitempty" toml:"metro" yaml:"metro,omitempty"` + + R *bomInfoR `boil:"-" json:"-" toml:"-" yaml:"-"` + L bomInfoL `boil:"-" json:"-" toml:"-" yaml:"-"` +} + +var BomInfoColumns = struct { + SerialNum string + AocMacAddress string + BMCMacAddress string + NumDefiPmi string + NumDefPWD string + Metro string +}{ + SerialNum: "serial_num", + AocMacAddress: "aoc_mac_address", + BMCMacAddress: "bmc_mac_address", + NumDefiPmi: "num_defi_pmi", + NumDefPWD: "num_def_pwd", + Metro: "metro", +} + +var BomInfoTableColumns = struct { + SerialNum string + AocMacAddress string + BMCMacAddress string + NumDefiPmi string + NumDefPWD string + Metro string +}{ + SerialNum: "bom_info.serial_num", + AocMacAddress: "bom_info.aoc_mac_address", + BMCMacAddress: "bom_info.bmc_mac_address", + NumDefiPmi: "bom_info.num_defi_pmi", + NumDefPWD: "bom_info.num_def_pwd", + Metro: "bom_info.metro", +} + +// Generated where + +var BomInfoWhere = struct { + SerialNum whereHelperstring + AocMacAddress whereHelpernull_String + BMCMacAddress whereHelpernull_String + NumDefiPmi whereHelpernull_String + NumDefPWD whereHelpernull_String + Metro whereHelpernull_String +}{ + SerialNum: whereHelperstring{field: "\"bom_info\".\"serial_num\""}, + AocMacAddress: whereHelpernull_String{field: "\"bom_info\".\"aoc_mac_address\""}, + BMCMacAddress: whereHelpernull_String{field: "\"bom_info\".\"bmc_mac_address\""}, + NumDefiPmi: whereHelpernull_String{field: "\"bom_info\".\"num_defi_pmi\""}, + NumDefPWD: whereHelpernull_String{field: "\"bom_info\".\"num_def_pwd\""}, + Metro: whereHelpernull_String{field: "\"bom_info\".\"metro\""}, +} + +// 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 +func (*bomInfoR) NewStruct() *bomInfoR { + return &bomInfoR{} +} + +func (r *bomInfoR) GetSerialNumAocMacAddresses() AocMacAddressSlice { + if r == nil { + return nil + } + 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{} + +var ( + bomInfoAllColumns = []string{"serial_num", "aoc_mac_address", "bmc_mac_address", "num_defi_pmi", "num_def_pwd", "metro"} + bomInfoColumnsWithoutDefault = []string{"serial_num"} + bomInfoColumnsWithDefault = []string{"aoc_mac_address", "bmc_mac_address", "num_defi_pmi", "num_def_pwd", "metro"} + bomInfoPrimaryKeyColumns = []string{"serial_num"} + bomInfoGeneratedColumns = []string{} +) + +type ( + // BomInfoSlice is an alias for a slice of pointers to BomInfo. + // This should almost always be used instead of []BomInfo. + BomInfoSlice []*BomInfo + // BomInfoHook is the signature for custom BomInfo hook methods + BomInfoHook func(context.Context, boil.ContextExecutor, *BomInfo) error + + bomInfoQuery struct { + *queries.Query + } +) + +// Cache for insert, update and upsert +var ( + bomInfoType = reflect.TypeOf(&BomInfo{}) + bomInfoMapping = queries.MakeStructMapping(bomInfoType) + bomInfoPrimaryKeyMapping, _ = queries.BindMapping(bomInfoType, bomInfoMapping, bomInfoPrimaryKeyColumns) + bomInfoInsertCacheMut sync.RWMutex + bomInfoInsertCache = make(map[string]insertCache) + bomInfoUpdateCacheMut sync.RWMutex + bomInfoUpdateCache = make(map[string]updateCache) + bomInfoUpsertCacheMut sync.RWMutex + bomInfoUpsertCache = 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 bomInfoAfterSelectHooks []BomInfoHook + +var bomInfoBeforeInsertHooks []BomInfoHook +var bomInfoAfterInsertHooks []BomInfoHook + +var bomInfoBeforeUpdateHooks []BomInfoHook +var bomInfoAfterUpdateHooks []BomInfoHook + +var bomInfoBeforeDeleteHooks []BomInfoHook +var bomInfoAfterDeleteHooks []BomInfoHook + +var bomInfoBeforeUpsertHooks []BomInfoHook +var bomInfoAfterUpsertHooks []BomInfoHook + +// doAfterSelectHooks executes all "after Select" hooks. +func (o *BomInfo) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoAfterSelectHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeInsertHooks executes all "before insert" hooks. +func (o *BomInfo) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoBeforeInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterInsertHooks executes all "after Insert" hooks. +func (o *BomInfo) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoAfterInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpdateHooks executes all "before Update" hooks. +func (o *BomInfo) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoBeforeUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpdateHooks executes all "after Update" hooks. +func (o *BomInfo) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoAfterUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeDeleteHooks executes all "before Delete" hooks. +func (o *BomInfo) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoBeforeDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterDeleteHooks executes all "after Delete" hooks. +func (o *BomInfo) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoAfterDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpsertHooks executes all "before Upsert" hooks. +func (o *BomInfo) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoBeforeUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpsertHooks executes all "after Upsert" hooks. +func (o *BomInfo) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range bomInfoAfterUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// AddBomInfoHook registers your hook function for all future operations. +func AddBomInfoHook(hookPoint boil.HookPoint, bomInfoHook BomInfoHook) { + switch hookPoint { + case boil.AfterSelectHook: + bomInfoAfterSelectHooks = append(bomInfoAfterSelectHooks, bomInfoHook) + case boil.BeforeInsertHook: + bomInfoBeforeInsertHooks = append(bomInfoBeforeInsertHooks, bomInfoHook) + case boil.AfterInsertHook: + bomInfoAfterInsertHooks = append(bomInfoAfterInsertHooks, bomInfoHook) + case boil.BeforeUpdateHook: + bomInfoBeforeUpdateHooks = append(bomInfoBeforeUpdateHooks, bomInfoHook) + case boil.AfterUpdateHook: + bomInfoAfterUpdateHooks = append(bomInfoAfterUpdateHooks, bomInfoHook) + case boil.BeforeDeleteHook: + bomInfoBeforeDeleteHooks = append(bomInfoBeforeDeleteHooks, bomInfoHook) + case boil.AfterDeleteHook: + bomInfoAfterDeleteHooks = append(bomInfoAfterDeleteHooks, bomInfoHook) + case boil.BeforeUpsertHook: + bomInfoBeforeUpsertHooks = append(bomInfoBeforeUpsertHooks, bomInfoHook) + case boil.AfterUpsertHook: + bomInfoAfterUpsertHooks = append(bomInfoAfterUpsertHooks, bomInfoHook) + } +} + +// One returns a single bomInfo record from the query. +func (q bomInfoQuery) One(ctx context.Context, exec boil.ContextExecutor) (*BomInfo, error) { + o := &BomInfo{} + + 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 bom_info") + } + + if err := o.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + + return o, nil +} + +// All returns all BomInfo records from the query. +func (q bomInfoQuery) All(ctx context.Context, exec boil.ContextExecutor) (BomInfoSlice, error) { + var o []*BomInfo + + err := q.Bind(ctx, exec, &o) + if err != nil { + return nil, errors.Wrap(err, "models: failed to assign all query results to BomInfo slice") + } + + if len(bomInfoAfterSelectHooks) != 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 BomInfo records in the query. +func (q bomInfoQuery) 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 bom_info rows") + } + + return count, nil +} + +// Exists checks if the row exists in the table. +func (q bomInfoQuery) 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 bom_info exists") + } + + return count > 0, nil +} + +// SerialNumAocMacAddresses retrieves all the aoc_mac_address's AocMacAddresses with an executor via serial_num column. +func (o *BomInfo) SerialNumAocMacAddresses(mods ...qm.QueryMod) aocMacAddressQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"aoc_mac_address\".\"serial_num\"=?", o.SerialNum), + ) + + 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 { + 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(`aoc_mac_address`), + qm.WhereIn(`aoc_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 aoc_mac_address") + } + + var resultSlice []*AocMacAddress + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice aoc_mac_address") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on aoc_mac_address") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for aoc_mac_address") + } + + if len(aocMacAddressAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.SerialNumAocMacAddresses = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &aocMacAddressR{} + } + foreign.R.SerialNumBomInfo = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if local.SerialNum == foreign.SerialNum { + local.R.SerialNumAocMacAddresses = append(local.R.SerialNumAocMacAddresses, foreign) + if foreign.R == nil { + foreign.R = &aocMacAddressR{} + } + foreign.R.SerialNumBomInfo = local + break + } + } + } + + 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. +// Sets related.R.SerialNumBomInfo appropriately. +func (o *BomInfo) AddSerialNumAocMacAddresses(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*AocMacAddress) 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 \"aoc_mac_address\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"serial_num"}), + strmangle.WhereClause("\"", "\"", 2, aocMacAddressPrimaryKeyColumns), + ) + values := []interface{}{o.SerialNum, rel.AocMacAddress} + + 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{ + SerialNumAocMacAddresses: related, + } + } else { + o.R.SerialNumAocMacAddresses = append(o.R.SerialNumAocMacAddresses, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &aocMacAddressR{ + SerialNumBomInfo: o, + } + } else { + rel.R.SerialNumBomInfo = o + } + } + 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\"")) + q := NewQuery(mods...) + if len(queries.GetSelect(q)) == 0 { + queries.SetSelect(q, []string{"\"bom_info\".*"}) + } + + return bomInfoQuery{q} +} + +// FindBomInfo retrieves a single record by ID with an executor. +// If selectCols is empty Find will return all columns. +func FindBomInfo(ctx context.Context, exec boil.ContextExecutor, serialNum string, selectCols ...string) (*BomInfo, error) { + bomInfoObj := &BomInfo{} + + sel := "*" + if len(selectCols) > 0 { + sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") + } + query := fmt.Sprintf( + "select %s from \"bom_info\" where \"serial_num\"=$1", sel, + ) + + q := queries.Raw(query, serialNum) + + err := q.Bind(ctx, exec, bomInfoObj) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: unable to select from bom_info") + } + + if err = bomInfoObj.doAfterSelectHooks(ctx, exec); err != nil { + return bomInfoObj, err + } + + return bomInfoObj, nil +} + +// Insert a single record using an executor. +// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. +func (o *BomInfo) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { + if o == nil { + return errors.New("models: no bom_info provided for insertion") + } + + var err error + + if err := o.doBeforeInsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(bomInfoColumnsWithDefault, o) + + key := makeCacheKey(columns, nzDefaults) + bomInfoInsertCacheMut.RLock() + cache, cached := bomInfoInsertCache[key] + bomInfoInsertCacheMut.RUnlock() + + if !cached { + wl, returnColumns := columns.InsertColumnSet( + bomInfoAllColumns, + bomInfoColumnsWithDefault, + bomInfoColumnsWithoutDefault, + nzDefaults, + ) + + cache.valueMapping, err = queries.BindMapping(bomInfoType, bomInfoMapping, wl) + if err != nil { + return err + } + cache.retMapping, err = queries.BindMapping(bomInfoType, bomInfoMapping, returnColumns) + if err != nil { + return err + } + if len(wl) != 0 { + cache.query = fmt.Sprintf("INSERT INTO \"bom_info\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) + } else { + cache.query = "INSERT INTO \"bom_info\" %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 bom_info") + } + + if !cached { + bomInfoInsertCacheMut.Lock() + bomInfoInsertCache[key] = cache + bomInfoInsertCacheMut.Unlock() + } + + return o.doAfterInsertHooks(ctx, exec) +} + +// Update uses an executor to update the BomInfo. +// 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 *BomInfo) 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) + bomInfoUpdateCacheMut.RLock() + cache, cached := bomInfoUpdateCache[key] + bomInfoUpdateCacheMut.RUnlock() + + if !cached { + wl := columns.UpdateColumnSet( + bomInfoAllColumns, + bomInfoPrimaryKeyColumns, + ) + + if !columns.IsWhitelist() { + wl = strmangle.SetComplement(wl, []string{"created_at"}) + } + if len(wl) == 0 { + return 0, errors.New("models: unable to update bom_info, could not build whitelist") + } + + cache.query = fmt.Sprintf("UPDATE \"bom_info\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, wl), + strmangle.WhereClause("\"", "\"", len(wl)+1, bomInfoPrimaryKeyColumns), + ) + cache.valueMapping, err = queries.BindMapping(bomInfoType, bomInfoMapping, append(wl, bomInfoPrimaryKeyColumns...)) + 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 bom_info row") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by update for bom_info") + } + + if !cached { + bomInfoUpdateCacheMut.Lock() + bomInfoUpdateCache[key] = cache + bomInfoUpdateCacheMut.Unlock() + } + + return rowsAff, o.doAfterUpdateHooks(ctx, exec) +} + +// UpdateAll updates all rows with the specified column values. +func (q bomInfoQuery) 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 bom_info") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected for bom_info") + } + + return rowsAff, nil +} + +// UpdateAll updates all rows with the specified column values, using an executor. +func (o BomInfoSlice) 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)), bomInfoPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := fmt.Sprintf("UPDATE \"bom_info\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, colNames), + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, bomInfoPrimaryKeyColumns, 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 bomInfo slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all bomInfo") + } + return rowsAff, nil +} + +// Delete deletes a single BomInfo record with an executor. +// Delete will match against the primary key column to find the record to delete. +func (o *BomInfo) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if o == nil { + return 0, errors.New("models: no BomInfo provided for delete") + } + + if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), bomInfoPrimaryKeyMapping) + sql := "DELETE FROM \"bom_info\" WHERE \"serial_num\"=$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 bom_info") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by delete for bom_info") + } + + if err := o.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + return rowsAff, nil +} + +// DeleteAll deletes all matching rows. +func (q bomInfoQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if q.Query == nil { + return 0, errors.New("models: no bomInfoQuery 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 bom_info") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for bom_info") + } + + return rowsAff, nil +} + +// DeleteAll deletes all rows in the slice, using an executor. +func (o BomInfoSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if len(o) == 0 { + return 0, nil + } + + if len(bomInfoBeforeDeleteHooks) != 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)), bomInfoPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "DELETE FROM \"bom_info\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, bomInfoPrimaryKeyColumns, 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 bomInfo slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for bom_info") + } + + if len(bomInfoAfterDeleteHooks) != 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 *BomInfo) Reload(ctx context.Context, exec boil.ContextExecutor) error { + ret, err := FindBomInfo(ctx, exec, o.SerialNum) + 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 *BomInfoSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { + if o == nil || len(*o) == 0 { + return nil + } + + slice := BomInfoSlice{} + var args []interface{} + for _, obj := range *o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), bomInfoPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "SELECT \"bom_info\".* FROM \"bom_info\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, bomInfoPrimaryKeyColumns, 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 BomInfoSlice") + } + + *o = slice + + return nil +} + +// BomInfoExists checks if the BomInfo row exists. +func BomInfoExists(ctx context.Context, exec boil.ContextExecutor, serialNum string) (bool, error) { + var exists bool + sql := "select exists(select 1 from \"bom_info\" where \"serial_num\"=$1 limit 1)" + + if boil.IsDebug(ctx) { + writer := boil.DebugWriterFrom(ctx) + fmt.Fprintln(writer, sql) + fmt.Fprintln(writer, serialNum) + } + row := exec.QueryRowContext(ctx, sql, serialNum) + + err := row.Scan(&exists) + if err != nil { + return false, errors.Wrap(err, "models: unable to check if bom_info 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 *BomInfo) Upsert(ctx context.Context, exec boil.ContextExecutor, updateOnConflict bool, conflictColumns []string, updateColumns, insertColumns boil.Columns) error { + if o == nil { + return errors.New("models: no bom_info provided for upsert") + } + + if err := o.doBeforeUpsertHooks(ctx, exec); err != nil { + return err + } + + nzDefaults := queries.NonZeroDefaultSet(bomInfoColumnsWithDefault, 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) + + bomInfoUpsertCacheMut.RLock() + cache, cached := bomInfoUpsertCache[key] + bomInfoUpsertCacheMut.RUnlock() + + var err error + + if !cached { + insert, ret := insertColumns.InsertColumnSet( + bomInfoAllColumns, + bomInfoColumnsWithDefault, + bomInfoColumnsWithoutDefault, + nzDefaults, + ) + update := updateColumns.UpdateColumnSet( + bomInfoAllColumns, + bomInfoPrimaryKeyColumns, + ) + + if updateOnConflict && len(update) == 0 { + return errors.New("models: unable to upsert bom_info, could not build update column list") + } + + conflict := conflictColumns + if len(conflict) == 0 { + conflict = make([]string, len(bomInfoPrimaryKeyColumns)) + copy(conflict, bomInfoPrimaryKeyColumns) + } + cache.query = buildUpsertQueryCockroachDB(dialect, "\"bom_info\"", updateOnConflict, ret, update, conflict, insert) + + cache.valueMapping, err = queries.BindMapping(bomInfoType, bomInfoMapping, insert) + if err != nil { + return err + } + if len(ret) != 0 { + cache.retMapping, err = queries.BindMapping(bomInfoType, bomInfoMapping, 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 bom_info") + } + + if !cached { + bomInfoUpsertCacheMut.Lock() + bomInfoUpsertCache[key] = cache + bomInfoUpsertCacheMut.Unlock() + } + + return o.doAfterUpsertHooks(ctx, exec) +} diff --git a/internal/models/bom_info_test.go b/internal/models/bom_info_test.go new file mode 100644 index 00000000..e1dbd28d --- /dev/null +++ b/internal/models/bom_info_test.go @@ -0,0 +1,1039 @@ +// 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 testBomInfosUpsert(t *testing.T) { + t.Parallel() + + if len(bomInfoAllColumns) == len(bomInfoPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + // Attempt the INSERT side of an UPSERT + o := BomInfo{} + if err = randomize.Struct(seed, &o, bomInfoDBTypes, true); err != nil { + t.Errorf("Unable to randomize BomInfo 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 BomInfo: %s", err) + } + + count, err := BomInfos().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, bomInfoDBTypes, false, bomInfoPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + if err = o.Upsert(ctx, tx, true, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert BomInfo: %s", err) + } + + count, err = BomInfos().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 testBomInfos(t *testing.T) { + t.Parallel() + + query := BomInfos() + + if query.Query == nil { + t.Error("expected a query, got nothing") + } +} + +func testBomInfosDelete(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBomInfosQueryDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().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 := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBomInfosSliceDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfoSlice{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 := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testBomInfosExists(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfoExists(ctx, tx, o.SerialNum) + if err != nil { + t.Errorf("Unable to check if BomInfo exists: %s", err) + } + if !e { + t.Errorf("Expected BomInfoExists to return true, but got false.") + } +} + +func testBomInfosFind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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) + } + + bomInfoFound, err := FindBomInfo(ctx, tx, o.SerialNum) + if err != nil { + t.Error(err) + } + + if bomInfoFound == nil { + t.Error("want a record, got nil") + } +} + +func testBomInfosBind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 = BomInfos().Bind(ctx, tx, o); err != nil { + t.Error(err) + } +} + +func testBomInfosOne(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().One(ctx, tx); err != nil { + t.Error(err) + } else if x == nil { + t.Error("expected to get a non nil record") + } +} + +func testBomInfosAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + bomInfoOne := &BomInfo{} + bomInfoTwo := &BomInfo{} + if err = randomize.Struct(seed, bomInfoOne, bomInfoDBTypes, false, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + if err = randomize.Struct(seed, bomInfoTwo, bomInfoDBTypes, false, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = bomInfoOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = bomInfoTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := BomInfos().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 2 { + t.Error("want 2 records, got:", len(slice)) + } +} + +func testBomInfosCount(t *testing.T) { + t.Parallel() + + var err error + seed := randomize.NewSeed() + bomInfoOne := &BomInfo{} + bomInfoTwo := &BomInfo{} + if err = randomize.Struct(seed, bomInfoOne, bomInfoDBTypes, false, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + if err = randomize.Struct(seed, bomInfoTwo, bomInfoDBTypes, false, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = bomInfoOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = bomInfoTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 2 { + t.Error("want 2 records, got:", count) + } +} + +func bomInfoBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func bomInfoAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *BomInfo) error { + *o = BomInfo{} + return nil +} + +func testBomInfosHooks(t *testing.T) { + t.Parallel() + + var err error + + ctx := context.Background() + empty := &BomInfo{} + o := &BomInfo{} + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, o, bomInfoDBTypes, false); err != nil { + t.Errorf("Unable to randomize BomInfo object: %s", err) + } + + AddBomInfoHook(boil.BeforeInsertHook, bomInfoBeforeInsertHook) + 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) + } + bomInfoBeforeInsertHooks = []BomInfoHook{} + + AddBomInfoHook(boil.AfterInsertHook, bomInfoAfterInsertHook) + 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) + } + bomInfoAfterInsertHooks = []BomInfoHook{} + + AddBomInfoHook(boil.AfterSelectHook, bomInfoAfterSelectHook) + 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) + } + bomInfoAfterSelectHooks = []BomInfoHook{} + + AddBomInfoHook(boil.BeforeUpdateHook, bomInfoBeforeUpdateHook) + 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) + } + bomInfoBeforeUpdateHooks = []BomInfoHook{} + + AddBomInfoHook(boil.AfterUpdateHook, bomInfoAfterUpdateHook) + 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) + } + bomInfoAfterUpdateHooks = []BomInfoHook{} + + AddBomInfoHook(boil.BeforeDeleteHook, bomInfoBeforeDeleteHook) + 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) + } + bomInfoBeforeDeleteHooks = []BomInfoHook{} + + AddBomInfoHook(boil.AfterDeleteHook, bomInfoAfterDeleteHook) + 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) + } + bomInfoAfterDeleteHooks = []BomInfoHook{} + + AddBomInfoHook(boil.BeforeUpsertHook, bomInfoBeforeUpsertHook) + 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) + } + bomInfoBeforeUpsertHooks = []BomInfoHook{} + + AddBomInfoHook(boil.AfterUpsertHook, bomInfoAfterUpsertHook) + 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) + } + bomInfoAfterUpsertHooks = []BomInfoHook{} +} + +func testBomInfosInsert(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testBomInfosInsertWhitelist(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Whitelist(bomInfoColumnsWithoutDefault...)); err != nil { + t.Error(err) + } + + count, err := BomInfos().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testBomInfoToManySerialNumAocMacAddresses(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 AocMacAddress + + 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, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, aocMacAddressDBTypes, false, aocMacAddressColumnsWithDefault...); 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.SerialNumAocMacAddresses().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.LoadSerialNumAocMacAddresses(ctx, tx, false, (*[]*BomInfo)(&slice), nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.SerialNumAocMacAddresses); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + a.R.SerialNumAocMacAddresses = nil + if err = a.L.LoadSerialNumAocMacAddresses(ctx, tx, true, &a, nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.SerialNumAocMacAddresses); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + if t.Failed() { + t.Logf("%#v", check) + } +} + +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 + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a BomInfo + var b, c, d, e AocMacAddress + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, bomInfoDBTypes, false, strmangle.SetComplement(bomInfoPrimaryKeyColumns, bomInfoColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + foreigners := []*AocMacAddress{&b, &c, &d, &e} + for _, x := range foreigners { + if err = randomize.Struct(seed, x, aocMacAddressDBTypes, false, strmangle.SetComplement(aocMacAddressPrimaryKeyColumns, aocMacAddressColumnsWithoutDefault)...); 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 := [][]*AocMacAddress{ + {&b, &c}, + {&d, &e}, + } + + for i, x := range foreignersSplitByInsertion { + err = a.AddSerialNumAocMacAddresses(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.SerialNumAocMacAddresses[i*2] != first { + t.Error("relationship struct slice not set to correct value") + } + if a.R.SerialNumAocMacAddresses[i*2+1] != second { + t.Error("relationship struct slice not set to correct value") + } + + count, err := a.SerialNumAocMacAddresses().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if want := int64((i + 1) * 2); count != want { + t.Error("want", want, "got", count) + } + } +} +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() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 testBomInfosReloadAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfoSlice{o} + + if err = slice.ReloadAll(ctx, tx); err != nil { + t.Error(err) + } +} + +func testBomInfosSelect(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 1 { + t.Error("want one record, got:", len(slice)) + } +} + +var ( + bomInfoDBTypes = map[string]string{`SerialNum`: `string`, `AocMacAddress`: `string`, `BMCMacAddress`: `string`, `NumDefiPmi`: `string`, `NumDefPWD`: `string`, `Metro`: `string`} + _ = bytes.MinRead +) + +func testBomInfosUpdate(t *testing.T) { + t.Parallel() + + if 0 == len(bomInfoPrimaryKeyColumns) { + t.Skip("Skipping table with no primary key columns") + } + if len(bomInfoAllColumns) == len(bomInfoPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().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, bomInfoDBTypes, true, bomInfoPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 testBomInfosSliceUpdateAll(t *testing.T) { + t.Parallel() + + if len(bomInfoAllColumns) == len(bomInfoPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &BomInfo{} + if err = randomize.Struct(seed, o, bomInfoDBTypes, true, bomInfoColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize BomInfo 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 := BomInfos().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, bomInfoDBTypes, true, bomInfoPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize BomInfo struct: %s", err) + } + + // Remove Primary keys and unique columns from what we plan to update + var fields []string + if strmangle.StringSliceMatch(bomInfoAllColumns, bomInfoPrimaryKeyColumns) { + fields = bomInfoAllColumns + } else { + fields = strmangle.SetComplement( + bomInfoAllColumns, + bomInfoPrimaryKeyColumns, + ) + } + + 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 := BomInfoSlice{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/crdb_suites_test.go b/internal/models/crdb_suites_test.go index 984cf615..1ebf0ae8 100644 --- a/internal/models/crdb_suites_test.go +++ b/internal/models/crdb_suites_test.go @@ -6,8 +6,11 @@ package models import "testing" 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) t.Run("ComponentFirmwareVersions", testComponentFirmwareVersionsUpsert)