trevstack/server/internal/models/schema_migrations.bob.go
2025-04-10 19:15:21 -04:00

362 lines
11 KiB
Go

// Code generated by BobGen sql (devel). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"context"
"io"
"github.com/aarondl/opt/omit"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
"github.com/stephenafamo/bob/dialect/sqlite/dm"
"github.com/stephenafamo/bob/dialect/sqlite/sm"
"github.com/stephenafamo/bob/dialect/sqlite/um"
"github.com/stephenafamo/bob/expr"
)
// SchemaMigration is an object representing the database table.
type SchemaMigration struct {
Version string `db:"version,pk" `
}
// SchemaMigrationSlice is an alias for a slice of pointers to SchemaMigration.
// This should almost always be used instead of []*SchemaMigration.
type SchemaMigrationSlice []*SchemaMigration
// SchemaMigrations contains methods to work with the schema_migrations table
var SchemaMigrations = sqlite.NewTablex[*SchemaMigration, SchemaMigrationSlice, *SchemaMigrationSetter]("", "schema_migrations")
// SchemaMigrationsQuery is a query on the schema_migrations table
type SchemaMigrationsQuery = *sqlite.ViewQuery[*SchemaMigration, SchemaMigrationSlice]
type schemaMigrationColumnNames struct {
Version string
}
var SchemaMigrationColumns = buildSchemaMigrationColumns("schema_migrations")
type schemaMigrationColumns struct {
tableAlias string
Version sqlite.Expression
}
func (c schemaMigrationColumns) Alias() string {
return c.tableAlias
}
func (schemaMigrationColumns) AliasedAs(alias string) schemaMigrationColumns {
return buildSchemaMigrationColumns(alias)
}
func buildSchemaMigrationColumns(alias string) schemaMigrationColumns {
return schemaMigrationColumns{
tableAlias: alias,
Version: sqlite.Quote(alias, "version"),
}
}
type schemaMigrationWhere[Q sqlite.Filterable] struct {
Version sqlite.WhereMod[Q, string]
}
func (schemaMigrationWhere[Q]) AliasedAs(alias string) schemaMigrationWhere[Q] {
return buildSchemaMigrationWhere[Q](buildSchemaMigrationColumns(alias))
}
func buildSchemaMigrationWhere[Q sqlite.Filterable](cols schemaMigrationColumns) schemaMigrationWhere[Q] {
return schemaMigrationWhere[Q]{
Version: sqlite.Where[Q, string](cols.Version),
}
}
var SchemaMigrationErrors = &schemaMigrationErrors{
ErrUniqueSqliteAutoindexSchemaMigrations1: &UniqueConstraintError{s: "sqlite_autoindex_schema_migrations_1"},
}
type schemaMigrationErrors struct {
ErrUniqueSqliteAutoindexSchemaMigrations1 *UniqueConstraintError
}
// SchemaMigrationSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type SchemaMigrationSetter struct {
Version omit.Val[string] `db:"version,pk" `
}
func (s SchemaMigrationSetter) SetColumns() []string {
vals := make([]string, 0, 1)
if !s.Version.IsUnset() {
vals = append(vals, "version")
}
return vals
}
func (s SchemaMigrationSetter) Overwrite(t *SchemaMigration) {
if !s.Version.IsUnset() {
t.Version, _ = s.Version.Get()
}
}
func (s *SchemaMigrationSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return SchemaMigrations.BeforeInsertHooks.RunHooks(ctx, exec, s)
})
if len(q.Table.Columns) == 0 {
q.Table.Columns = s.SetColumns()
}
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
vals := make([]bob.Expression, 0, 1)
if !s.Version.IsUnset() {
vals = append(vals, sqlite.Arg(s.Version))
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
func (s SchemaMigrationSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s SchemaMigrationSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 1)
if !s.Version.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "version")...),
sqlite.Arg(s.Version),
}})
}
return exprs
}
// FindSchemaMigration retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindSchemaMigration(ctx context.Context, exec bob.Executor, VersionPK string, cols ...string) (*SchemaMigration, error) {
if len(cols) == 0 {
return SchemaMigrations.Query(
SelectWhere.SchemaMigrations.Version.EQ(VersionPK),
).One(ctx, exec)
}
return SchemaMigrations.Query(
SelectWhere.SchemaMigrations.Version.EQ(VersionPK),
sm.Columns(SchemaMigrations.Columns().Only(cols...)),
).One(ctx, exec)
}
// SchemaMigrationExists checks the presence of a single record by primary key
func SchemaMigrationExists(ctx context.Context, exec bob.Executor, VersionPK string) (bool, error) {
return SchemaMigrations.Query(
SelectWhere.SchemaMigrations.Version.EQ(VersionPK),
).Exists(ctx, exec)
}
// AfterQueryHook is called after SchemaMigration is retrieved from the database
func (o *SchemaMigration) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = SchemaMigrations.AfterSelectHooks.RunHooks(ctx, exec, SchemaMigrationSlice{o})
case bob.QueryTypeInsert:
ctx, err = SchemaMigrations.AfterInsertHooks.RunHooks(ctx, exec, SchemaMigrationSlice{o})
case bob.QueryTypeUpdate:
ctx, err = SchemaMigrations.AfterUpdateHooks.RunHooks(ctx, exec, SchemaMigrationSlice{o})
case bob.QueryTypeDelete:
ctx, err = SchemaMigrations.AfterDeleteHooks.RunHooks(ctx, exec, SchemaMigrationSlice{o})
}
return err
}
// PrimaryKeyVals returns the primary key values of the SchemaMigration
func (o *SchemaMigration) PrimaryKeyVals() bob.Expression {
return sqlite.Arg(o.Version)
}
func (o *SchemaMigration) pkEQ() dialect.Expression {
return sqlite.Quote("schema_migrations", "version").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return o.PrimaryKeyVals().WriteSQL(ctx, w, d, start)
}))
}
// Update uses an executor to update the SchemaMigration
func (o *SchemaMigration) Update(ctx context.Context, exec bob.Executor, s *SchemaMigrationSetter) error {
v, err := SchemaMigrations.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
if err != nil {
return err
}
*o = *v
return nil
}
// Delete deletes a single SchemaMigration record with an executor
func (o *SchemaMigration) Delete(ctx context.Context, exec bob.Executor) error {
_, err := SchemaMigrations.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the SchemaMigration using the executor
func (o *SchemaMigration) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := SchemaMigrations.Query(
SelectWhere.SchemaMigrations.Version.EQ(o.Version),
).One(ctx, exec)
if err != nil {
return err
}
*o = *o2
return nil
}
// AfterQueryHook is called after SchemaMigrationSlice is retrieved from the database
func (o SchemaMigrationSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = SchemaMigrations.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = SchemaMigrations.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = SchemaMigrations.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = SchemaMigrations.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o SchemaMigrationSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return sqlite.Raw("NULL")
}
return sqlite.Quote("schema_migrations", "version").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, 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 SchemaMigrationSlice) copyMatchingRows(from ...*SchemaMigration) {
for i, old := range o {
for _, new := range from {
if new.Version != old.Version {
continue
}
o[i] = new
break
}
}
}
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o SchemaMigrationSlice) 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 SchemaMigrations.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 *SchemaMigration:
o.copyMatchingRows(retrieved)
case []*SchemaMigration:
o.copyMatchingRows(retrieved...)
case SchemaMigrationSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a SchemaMigration or a slice of SchemaMigration
// then run the AfterUpdateHooks on the slice
_, err = SchemaMigrations.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o SchemaMigrationSlice) 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 SchemaMigrations.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 *SchemaMigration:
o.copyMatchingRows(retrieved)
case []*SchemaMigration:
o.copyMatchingRows(retrieved...)
case SchemaMigrationSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a SchemaMigration or a slice of SchemaMigration
// then run the AfterDeleteHooks on the slice
_, err = SchemaMigrations.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o SchemaMigrationSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals SchemaMigrationSetter) error {
if len(o) == 0 {
return nil
}
_, err := SchemaMigrations.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o SchemaMigrationSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := SchemaMigrations.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o SchemaMigrationSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := SchemaMigrations.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}