This refactor was born out of the inter-dependency cycles developing between the "background" module and just about every other module which was caused by the background module becoming a dependency of every module that needed to background work and the fact that the background module was also supposedly responsible for the logic for processing those tasks. Instead the "background" module is now very, very shallow and relies entirely on the Postgres NOTIFY logic for triggering jobs. There's a new table, `job` which holds just a type and single row ID. All told, this means that jobs can be added to the queue as part of the API-level or platform-level transaction, ensuring atomicity, and processing coordination is handled by the platform module, which can depend on anything.
425 lines
11 KiB
Go
425 lines
11 KiB
Go
// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
|
|
|
package models
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/Gleipnir-Technology/bob"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
|
"github.com/Gleipnir-Technology/bob/expr"
|
|
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
"github.com/aarondl/opt/omit"
|
|
)
|
|
|
|
// Job is an object representing the database table.
|
|
type Job struct {
|
|
Created time.Time `db:"created" `
|
|
ID int32 `db:"id,pk" `
|
|
Type enums.Jobtype `db:"type_" `
|
|
RowID int32 `db:"row_id" `
|
|
}
|
|
|
|
// JobSlice is an alias for a slice of pointers to Job.
|
|
// This should almost always be used instead of []*Job.
|
|
type JobSlice []*Job
|
|
|
|
// Jobs contains methods to work with the job table
|
|
var Jobs = psql.NewTablex[*Job, JobSlice, *JobSetter]("", "job", buildJobColumns("job"))
|
|
|
|
// JobsQuery is a query on the job table
|
|
type JobsQuery = *psql.ViewQuery[*Job, JobSlice]
|
|
|
|
func buildJobColumns(alias string) jobColumns {
|
|
return jobColumns{
|
|
ColumnsExpr: expr.NewColumnsExpr(
|
|
"created", "id", "type_", "row_id",
|
|
).WithParent("job"),
|
|
tableAlias: alias,
|
|
Created: psql.Quote(alias, "created"),
|
|
ID: psql.Quote(alias, "id"),
|
|
Type: psql.Quote(alias, "type_"),
|
|
RowID: psql.Quote(alias, "row_id"),
|
|
}
|
|
}
|
|
|
|
type jobColumns struct {
|
|
expr.ColumnsExpr
|
|
tableAlias string
|
|
Created psql.Expression
|
|
ID psql.Expression
|
|
Type psql.Expression
|
|
RowID psql.Expression
|
|
}
|
|
|
|
func (c jobColumns) Alias() string {
|
|
return c.tableAlias
|
|
}
|
|
|
|
func (jobColumns) AliasedAs(alias string) jobColumns {
|
|
return buildJobColumns(alias)
|
|
}
|
|
|
|
// JobSetter is used for insert/upsert/update operations
|
|
// All values are optional, and do not have to be set
|
|
// Generated columns are not included
|
|
type JobSetter struct {
|
|
Created omit.Val[time.Time] `db:"created" `
|
|
ID omit.Val[int32] `db:"id,pk" `
|
|
Type omit.Val[enums.Jobtype] `db:"type_" `
|
|
RowID omit.Val[int32] `db:"row_id" `
|
|
}
|
|
|
|
func (s JobSetter) SetColumns() []string {
|
|
vals := make([]string, 0, 4)
|
|
if s.Created.IsValue() {
|
|
vals = append(vals, "created")
|
|
}
|
|
if s.ID.IsValue() {
|
|
vals = append(vals, "id")
|
|
}
|
|
if s.Type.IsValue() {
|
|
vals = append(vals, "type_")
|
|
}
|
|
if s.RowID.IsValue() {
|
|
vals = append(vals, "row_id")
|
|
}
|
|
return vals
|
|
}
|
|
|
|
func (s JobSetter) Overwrite(t *Job) {
|
|
if s.Created.IsValue() {
|
|
t.Created = s.Created.MustGet()
|
|
}
|
|
if s.ID.IsValue() {
|
|
t.ID = s.ID.MustGet()
|
|
}
|
|
if s.Type.IsValue() {
|
|
t.Type = s.Type.MustGet()
|
|
}
|
|
if s.RowID.IsValue() {
|
|
t.RowID = s.RowID.MustGet()
|
|
}
|
|
}
|
|
|
|
func (s *JobSetter) Apply(q *dialect.InsertQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Jobs.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
|
})
|
|
|
|
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
|
vals := make([]bob.Expression, 4)
|
|
if s.Created.IsValue() {
|
|
vals[0] = psql.Arg(s.Created.MustGet())
|
|
} else {
|
|
vals[0] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if s.ID.IsValue() {
|
|
vals[1] = psql.Arg(s.ID.MustGet())
|
|
} else {
|
|
vals[1] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if s.Type.IsValue() {
|
|
vals[2] = psql.Arg(s.Type.MustGet())
|
|
} else {
|
|
vals[2] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
if s.RowID.IsValue() {
|
|
vals[3] = psql.Arg(s.RowID.MustGet())
|
|
} else {
|
|
vals[3] = psql.Raw("DEFAULT")
|
|
}
|
|
|
|
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
|
}))
|
|
}
|
|
|
|
func (s JobSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|
return um.Set(s.Expressions()...)
|
|
}
|
|
|
|
func (s JobSetter) Expressions(prefix ...string) []bob.Expression {
|
|
exprs := make([]bob.Expression, 0, 4)
|
|
|
|
if s.Created.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "created")...),
|
|
psql.Arg(s.Created),
|
|
}})
|
|
}
|
|
|
|
if s.ID.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "id")...),
|
|
psql.Arg(s.ID),
|
|
}})
|
|
}
|
|
|
|
if s.Type.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "type_")...),
|
|
psql.Arg(s.Type),
|
|
}})
|
|
}
|
|
|
|
if s.RowID.IsValue() {
|
|
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
|
psql.Quote(append(prefix, "row_id")...),
|
|
psql.Arg(s.RowID),
|
|
}})
|
|
}
|
|
|
|
return exprs
|
|
}
|
|
|
|
// FindJob retrieves a single record by primary key
|
|
// If cols is empty Find will return all columns.
|
|
func FindJob(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*Job, error) {
|
|
if len(cols) == 0 {
|
|
return Jobs.Query(
|
|
sm.Where(Jobs.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
return Jobs.Query(
|
|
sm.Where(Jobs.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
sm.Columns(Jobs.Columns.Only(cols...)),
|
|
).One(ctx, exec)
|
|
}
|
|
|
|
// JobExists checks the presence of a single record by primary key
|
|
func JobExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
|
|
return Jobs.Query(
|
|
sm.Where(Jobs.Columns.ID.EQ(psql.Arg(IDPK))),
|
|
).Exists(ctx, exec)
|
|
}
|
|
|
|
// AfterQueryHook is called after Job is retrieved from the database
|
|
func (o *Job) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Jobs.AfterSelectHooks.RunHooks(ctx, exec, JobSlice{o})
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Jobs.AfterInsertHooks.RunHooks(ctx, exec, JobSlice{o})
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Jobs.AfterUpdateHooks.RunHooks(ctx, exec, JobSlice{o})
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Jobs.AfterDeleteHooks.RunHooks(ctx, exec, JobSlice{o})
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// primaryKeyVals returns the primary key values of the Job
|
|
func (o *Job) primaryKeyVals() bob.Expression {
|
|
return psql.Arg(o.ID)
|
|
}
|
|
|
|
func (o *Job) pkEQ() dialect.Expression {
|
|
return psql.Quote("job", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
|
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
|
|
}))
|
|
}
|
|
|
|
// Update uses an executor to update the Job
|
|
func (o *Job) Update(ctx context.Context, exec bob.Executor, s *JobSetter) error {
|
|
v, err := Jobs.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*o = *v
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes a single Job record with an executor
|
|
func (o *Job) Delete(ctx context.Context, exec bob.Executor) error {
|
|
_, err := Jobs.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
// Reload refreshes the Job using the executor
|
|
func (o *Job) Reload(ctx context.Context, exec bob.Executor) error {
|
|
o2, err := Jobs.Query(
|
|
sm.Where(Jobs.Columns.ID.EQ(psql.Arg(o.ID))),
|
|
).One(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*o = *o2
|
|
|
|
return nil
|
|
}
|
|
|
|
// AfterQueryHook is called after JobSlice is retrieved from the database
|
|
func (o JobSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
|
var err error
|
|
|
|
switch queryType {
|
|
case bob.QueryTypeSelect:
|
|
ctx, err = Jobs.AfterSelectHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeInsert:
|
|
ctx, err = Jobs.AfterInsertHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeUpdate:
|
|
ctx, err = Jobs.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
case bob.QueryTypeDelete:
|
|
ctx, err = Jobs.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (o JobSlice) pkIN() dialect.Expression {
|
|
if len(o) == 0 {
|
|
return psql.Raw("NULL")
|
|
}
|
|
|
|
return psql.Quote("job", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
|
pkPairs := make([]bob.Expression, len(o))
|
|
for i, row := range o {
|
|
pkPairs[i] = row.primaryKeyVals()
|
|
}
|
|
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
|
|
}))
|
|
}
|
|
|
|
// copyMatchingRows finds models in the given slice that have the same primary key
|
|
// then it first copies the existing relationships from the old model to the new model
|
|
// and then replaces the old model in the slice with the new model
|
|
func (o JobSlice) copyMatchingRows(from ...*Job) {
|
|
for i, old := range o {
|
|
for _, new := range from {
|
|
if new.ID != old.ID {
|
|
continue
|
|
}
|
|
|
|
o[i] = new
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
|
func (o JobSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Jobs.BeforeUpdateHooks.RunHooks(ctx, exec, o)
|
|
})
|
|
|
|
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
|
var err error
|
|
switch retrieved := retrieved.(type) {
|
|
case *Job:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*Job:
|
|
o.copyMatchingRows(retrieved...)
|
|
case JobSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a Job or a slice of Job
|
|
// then run the AfterUpdateHooks on the slice
|
|
_, err = Jobs.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
|
func (o JobSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
|
|
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
|
|
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
|
return Jobs.BeforeDeleteHooks.RunHooks(ctx, exec, o)
|
|
})
|
|
|
|
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
|
|
var err error
|
|
switch retrieved := retrieved.(type) {
|
|
case *Job:
|
|
o.copyMatchingRows(retrieved)
|
|
case []*Job:
|
|
o.copyMatchingRows(retrieved...)
|
|
case JobSlice:
|
|
o.copyMatchingRows(retrieved...)
|
|
default:
|
|
// If the retrieved value is not a Job or a slice of Job
|
|
// then run the AfterDeleteHooks on the slice
|
|
_, err = Jobs.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
|
}
|
|
|
|
return err
|
|
}))
|
|
|
|
q.AppendWhere(o.pkIN())
|
|
})
|
|
}
|
|
|
|
func (o JobSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals JobSetter) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Jobs.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o JobSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
_, err := Jobs.Delete(o.DeleteMod()).Exec(ctx, exec)
|
|
return err
|
|
}
|
|
|
|
func (o JobSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
|
if len(o) == 0 {
|
|
return nil
|
|
}
|
|
|
|
o2, err := Jobs.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
o.copyMatchingRows(o2...)
|
|
|
|
return nil
|
|
}
|
|
|
|
type jobWhere[Q psql.Filterable] struct {
|
|
Created psql.WhereMod[Q, time.Time]
|
|
ID psql.WhereMod[Q, int32]
|
|
Type psql.WhereMod[Q, enums.Jobtype]
|
|
RowID psql.WhereMod[Q, int32]
|
|
}
|
|
|
|
func (jobWhere[Q]) AliasedAs(alias string) jobWhere[Q] {
|
|
return buildJobWhere[Q](buildJobColumns(alias))
|
|
}
|
|
|
|
func buildJobWhere[Q psql.Filterable](cols jobColumns) jobWhere[Q] {
|
|
return jobWhere[Q]{
|
|
Created: psql.Where[Q, time.Time](cols.Created),
|
|
ID: psql.Where[Q, int32](cols.ID),
|
|
Type: psql.Where[Q, enums.Jobtype](cols.Type),
|
|
RowID: psql.Where[Q, int32](cols.RowID),
|
|
}
|
|
}
|