feat: migrations

This commit is contained in:
2025-04-10 19:15:21 -04:00
parent 1667b78a0a
commit f9772bce47
35 changed files with 1144 additions and 370 deletions

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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
@ -15,19 +15,22 @@ import (
)
var TableNames = struct {
Files string
Items string
Users string
Files string
Items string
SchemaMigrations string
Users string
}{
Files: "file",
Items: "item",
Users: "user",
Files: "file",
Items: "item",
SchemaMigrations: "schema_migrations",
Users: "user",
}
var ColumnNames = struct {
Files fileColumnNames
Items itemColumnNames
Users userColumnNames
Files fileColumnNames
Items itemColumnNames
SchemaMigrations schemaMigrationColumnNames
Users userColumnNames
}{
Files: fileColumnNames{
ID: "id",
@ -44,6 +47,9 @@ var ColumnNames = struct {
Quantity: "quantity",
UserID: "user_id",
},
SchemaMigrations: schemaMigrationColumnNames{
Version: "version",
},
Users: userColumnNames{
ID: "id",
Username: "username",
@ -60,18 +66,21 @@ var (
)
func Where[Q sqlite.Filterable]() struct {
Files fileWhere[Q]
Items itemWhere[Q]
Users userWhere[Q]
Files fileWhere[Q]
Items itemWhere[Q]
SchemaMigrations schemaMigrationWhere[Q]
Users userWhere[Q]
} {
return struct {
Files fileWhere[Q]
Items itemWhere[Q]
Users userWhere[Q]
Files fileWhere[Q]
Items itemWhere[Q]
SchemaMigrations schemaMigrationWhere[Q]
Users userWhere[Q]
}{
Files: buildFileWhere[Q](FileColumns),
Items: buildItemWhere[Q](ItemColumns),
Users: buildUserWhere[Q](UserColumns),
Files: buildFileWhere[Q](FileColumns),
Items: buildItemWhere[Q](ItemColumns),
SchemaMigrations: buildSchemaMigrationWhere[Q](SchemaMigrationColumns),
Users: buildUserWhere[Q](UserColumns),
}
}

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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
@ -11,5 +11,8 @@ var _ bob.HookableType = &File{}
// Make sure the type Item runs hooks after queries
var _ bob.HookableType = &Item{}
// Make sure the type SchemaMigration runs hooks after queries
var _ bob.HookableType = &SchemaMigration{}
// Make sure the type User runs hooks after queries
var _ bob.HookableType = &User{}

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory
@ -12,9 +12,10 @@ import (
type contextKey string
var (
fileCtx = newContextual[*models.File]("file")
itemCtx = newContextual[*models.Item]("item")
userCtx = newContextual[*models.User]("user")
fileCtx = newContextual[*models.File]("file")
itemCtx = newContextual[*models.Item]("item")
schemaMigrationCtx = newContextual[*models.SchemaMigration]("schemaMigration")
userCtx = newContextual[*models.User]("user")
)
// Contextual is a convienience wrapper around context.WithValue and context.Value

View File

@ -1,12 +1,13 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory
type Factory struct {
baseFileMods FileModSlice
baseItemMods ItemModSlice
baseUserMods UserModSlice
baseFileMods FileModSlice
baseItemMods ItemModSlice
baseSchemaMigrationMods SchemaMigrationModSlice
baseUserMods UserModSlice
}
func New() *Factory {
@ -37,6 +38,18 @@ func (f *Factory) NewItem(mods ...ItemMod) *ItemTemplate {
return o
}
func (f *Factory) NewSchemaMigration(mods ...SchemaMigrationMod) *SchemaMigrationTemplate {
o := &SchemaMigrationTemplate{f: f}
if f != nil {
f.baseSchemaMigrationMods.Apply(o)
}
SchemaMigrationModSlice(mods).Apply(o)
return o
}
func (f *Factory) NewUser(mods ...UserMod) *UserTemplate {
o := &UserTemplate{f: f}
@ -65,6 +78,14 @@ func (f *Factory) AddBaseItemMod(mods ...ItemMod) {
f.baseItemMods = append(f.baseItemMods, mods...)
}
func (f *Factory) ClearBaseSchemaMigrationMods() {
f.baseSchemaMigrationMods = nil
}
func (f *Factory) AddBaseSchemaMigrationMod(mods ...SchemaMigrationMod) {
f.baseSchemaMigrationMods = append(f.baseSchemaMigrationMods, mods...)
}
func (f *Factory) ClearBaseUserMods() {
f.baseUserMods = nil
}

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory
@ -8,9 +8,7 @@ import (
"testing"
"time"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/jaswdr/faker/v2"
models "github.com/spotdemo4/trevstack/server/internal/models"
"github.com/stephenafamo/bob"
@ -40,9 +38,9 @@ type ItemTemplate struct {
ID func() int64
Name func() string
Added func() time.Time
Description func() null.Val[string]
Price func() null.Val[float32]
Quantity func() null.Val[int64]
Description func() string
Price func() float32
Quantity func() int64
UserID func() int64
r itemR
@ -132,13 +130,13 @@ func (o ItemTemplate) BuildSetter() *models.ItemSetter {
m.Added = omit.From(o.Added())
}
if o.Description != nil {
m.Description = omitnull.FromNull(o.Description())
m.Description = omit.From(o.Description())
}
if o.Price != nil {
m.Price = omitnull.FromNull(o.Price())
m.Price = omit.From(o.Price())
}
if o.Quantity != nil {
m.Quantity = omitnull.FromNull(o.Quantity())
m.Quantity = omit.From(o.Quantity())
}
if o.UserID != nil {
m.UserID = omit.From(o.UserID())
@ -189,6 +187,15 @@ func ensureCreatableItem(m *models.ItemSetter) {
if m.Added.IsUnset() {
m.Added = omit.From(random_time_Time(nil))
}
if m.Description.IsUnset() {
m.Description = omit.From(random_string(nil))
}
if m.Price.IsUnset() {
m.Price = omit.From(random_float32(nil))
}
if m.Quantity.IsUnset() {
m.Quantity = omit.From(random_int64(nil))
}
if m.UserID.IsUnset() {
m.UserID = omit.From(random_int64(nil))
}
@ -429,14 +436,14 @@ func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
}
// Set the model columns to this value
func (m itemMods) Description(val null.Val[string]) ItemMod {
func (m itemMods) Description(val string) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = func() null.Val[string] { return val }
o.Description = func() string { return val }
})
}
// Set the Column from the function
func (m itemMods) DescriptionFunc(f func() null.Val[string]) ItemMod {
func (m itemMods) DescriptionFunc(f func() string) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = f
})
@ -453,29 +460,21 @@ func (m itemMods) UnsetDescription() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomDescription(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
o.Description = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m itemMods) Price(val null.Val[float32]) ItemMod {
func (m itemMods) Price(val float32) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = func() null.Val[float32] { return val }
o.Price = func() float32 { return val }
})
}
// Set the Column from the function
func (m itemMods) PriceFunc(f func() null.Val[float32]) ItemMod {
func (m itemMods) PriceFunc(f func() float32) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = f
})
@ -492,29 +491,21 @@ func (m itemMods) UnsetPrice() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = func() null.Val[float32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[float32](nil)
}
return null.From(random_float32(f))
o.Price = func() float32 {
return random_float32(f)
}
})
}
// Set the model columns to this value
func (m itemMods) Quantity(val null.Val[int64]) ItemMod {
func (m itemMods) Quantity(val int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int64] { return val }
o.Quantity = func() int64 { return val }
})
}
// Set the Column from the function
func (m itemMods) QuantityFunc(f func() null.Val[int64]) ItemMod {
func (m itemMods) QuantityFunc(f func() int64) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = f
})
@ -531,16 +522,8 @@ func (m itemMods) UnsetQuantity() ItemMod {
// if faker is nil, a default faker is used
func (m itemMods) RandomQuantity(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int64] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int64](nil)
}
return null.From(random_int64(f))
o.Quantity = func() int64 {
return random_int64(f)
}
})
}

View File

@ -0,0 +1,276 @@
// 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 factory
import (
"context"
"testing"
"github.com/aarondl/opt/omit"
"github.com/jaswdr/faker/v2"
models "github.com/spotdemo4/trevstack/server/internal/models"
"github.com/stephenafamo/bob"
)
type SchemaMigrationMod interface {
Apply(*SchemaMigrationTemplate)
}
type SchemaMigrationModFunc func(*SchemaMigrationTemplate)
func (f SchemaMigrationModFunc) Apply(n *SchemaMigrationTemplate) {
f(n)
}
type SchemaMigrationModSlice []SchemaMigrationMod
func (mods SchemaMigrationModSlice) Apply(n *SchemaMigrationTemplate) {
for _, f := range mods {
f.Apply(n)
}
}
// SchemaMigrationTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type SchemaMigrationTemplate struct {
Version func() string
f *Factory
}
// Apply mods to the SchemaMigrationTemplate
func (o *SchemaMigrationTemplate) Apply(mods ...SchemaMigrationMod) {
for _, mod := range mods {
mod.Apply(o)
}
}
// toModel returns an *models.SchemaMigration
// this does nothing with the relationship templates
func (o SchemaMigrationTemplate) toModel() *models.SchemaMigration {
m := &models.SchemaMigration{}
if o.Version != nil {
m.Version = o.Version()
}
return m
}
// toModels returns an models.SchemaMigrationSlice
// this does nothing with the relationship templates
func (o SchemaMigrationTemplate) toModels(number int) models.SchemaMigrationSlice {
m := make(models.SchemaMigrationSlice, number)
for i := range m {
m[i] = o.toModel()
}
return m
}
// setModelRels creates and sets the relationships on *models.SchemaMigration
// according to the relationships in the template. Nothing is inserted into the db
func (t SchemaMigrationTemplate) setModelRels(o *models.SchemaMigration) {}
// BuildSetter returns an *models.SchemaMigrationSetter
// this does nothing with the relationship templates
func (o SchemaMigrationTemplate) BuildSetter() *models.SchemaMigrationSetter {
m := &models.SchemaMigrationSetter{}
if o.Version != nil {
m.Version = omit.From(o.Version())
}
return m
}
// BuildManySetter returns an []*models.SchemaMigrationSetter
// this does nothing with the relationship templates
func (o SchemaMigrationTemplate) BuildManySetter(number int) []*models.SchemaMigrationSetter {
m := make([]*models.SchemaMigrationSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.SchemaMigration
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use SchemaMigrationTemplate.Create
func (o SchemaMigrationTemplate) Build() *models.SchemaMigration {
m := o.toModel()
o.setModelRels(m)
return m
}
// BuildMany returns an models.SchemaMigrationSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use SchemaMigrationTemplate.CreateMany
func (o SchemaMigrationTemplate) BuildMany(number int) models.SchemaMigrationSlice {
m := make(models.SchemaMigrationSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableSchemaMigration(m *models.SchemaMigrationSetter) {
if m.Version.IsUnset() {
m.Version = omit.From(random_string(nil))
}
}
// insertOptRels creates and inserts any optional the relationships on *models.SchemaMigration
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *SchemaMigrationTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.SchemaMigration) (context.Context, error) {
var err error
return ctx, err
}
// Create builds a schemaMigration and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *SchemaMigrationTemplate) Create(ctx context.Context, exec bob.Executor) (*models.SchemaMigration, error) {
_, m, err := o.create(ctx, exec)
return m, err
}
// MustCreate builds a schemaMigration and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *SchemaMigrationTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.SchemaMigration {
_, m, err := o.create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a schemaMigration and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o *SchemaMigrationTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.SchemaMigration {
tb.Helper()
_, m, err := o.create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// create builds a schemaMigration and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// this returns a context that includes the newly inserted model
func (o *SchemaMigrationTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.SchemaMigration, error) {
var err error
opt := o.BuildSetter()
ensureCreatableSchemaMigration(opt)
m, err := models.SchemaMigrations.Insert(opt).One(ctx, exec)
if err != nil {
return ctx, nil, err
}
ctx = schemaMigrationCtx.WithValue(ctx, m)
ctx, err = o.insertOptRels(ctx, exec, m)
return ctx, m, err
}
// CreateMany builds multiple schemaMigrations and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o SchemaMigrationTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.SchemaMigrationSlice, error) {
_, m, err := o.createMany(ctx, exec, number)
return m, err
}
// MustCreateMany builds multiple schemaMigrations and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o SchemaMigrationTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.SchemaMigrationSlice {
_, m, err := o.createMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple schemaMigrations and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o SchemaMigrationTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.SchemaMigrationSlice {
tb.Helper()
_, m, err := o.createMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// createMany builds multiple schemaMigrations and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// this returns a context that includes the newly inserted models
func (o SchemaMigrationTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.SchemaMigrationSlice, error) {
var err error
m := make(models.SchemaMigrationSlice, number)
for i := range m {
ctx, m[i], err = o.create(ctx, exec)
if err != nil {
return ctx, nil, err
}
}
return ctx, m, nil
}
// SchemaMigration has methods that act as mods for the SchemaMigrationTemplate
var SchemaMigrationMods schemaMigrationMods
type schemaMigrationMods struct{}
func (m schemaMigrationMods) RandomizeAllColumns(f *faker.Faker) SchemaMigrationMod {
return SchemaMigrationModSlice{
SchemaMigrationMods.RandomVersion(f),
}
}
// Set the model columns to this value
func (m schemaMigrationMods) Version(val string) SchemaMigrationMod {
return SchemaMigrationModFunc(func(o *SchemaMigrationTemplate) {
o.Version = func() string { return val }
})
}
// Set the Column from the function
func (m schemaMigrationMods) VersionFunc(f func() string) SchemaMigrationMod {
return SchemaMigrationModFunc(func(o *SchemaMigrationTemplate) {
o.Version = f
})
}
// Clear any values for the column
func (m schemaMigrationMods) UnsetVersion() SchemaMigrationMod {
return SchemaMigrationModFunc(func(o *SchemaMigrationTemplate) {
o.Version = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m schemaMigrationMods) RandomVersion(f *faker.Faker) SchemaMigrationMod {
return SchemaMigrationModFunc(func(o *SchemaMigrationTemplate) {
o.Version = func() string {
return random_string(f)
}
})
}

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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 factory

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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
@ -11,9 +11,7 @@ import (
"io"
"time"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
@ -27,13 +25,13 @@ import (
// Item is an object representing the database table.
type Item struct {
ID int64 `db:"id,pk" `
Name string `db:"name" `
Added time.Time `db:"added" `
Description null.Val[string] `db:"description" `
Price null.Val[float32] `db:"price" `
Quantity null.Val[int64] `db:"quantity" `
UserID int64 `db:"user_id" `
ID int64 `db:"id,pk" `
Name string `db:"name" `
Added time.Time `db:"added" `
Description string `db:"description" `
Price float32 `db:"price" `
Quantity int64 `db:"quantity" `
UserID int64 `db:"user_id" `
R itemR `db:"-" `
}
@ -101,9 +99,9 @@ type itemWhere[Q sqlite.Filterable] struct {
ID sqlite.WhereMod[Q, int64]
Name sqlite.WhereMod[Q, string]
Added sqlite.WhereMod[Q, time.Time]
Description sqlite.WhereNullMod[Q, string]
Price sqlite.WhereNullMod[Q, float32]
Quantity sqlite.WhereNullMod[Q, int64]
Description sqlite.WhereMod[Q, string]
Price sqlite.WhereMod[Q, float32]
Quantity sqlite.WhereMod[Q, int64]
UserID sqlite.WhereMod[Q, int64]
}
@ -116,9 +114,9 @@ func buildItemWhere[Q sqlite.Filterable](cols itemColumns) itemWhere[Q] {
ID: sqlite.Where[Q, int64](cols.ID),
Name: sqlite.Where[Q, string](cols.Name),
Added: sqlite.Where[Q, time.Time](cols.Added),
Description: sqlite.WhereNull[Q, string](cols.Description),
Price: sqlite.WhereNull[Q, float32](cols.Price),
Quantity: sqlite.WhereNull[Q, int64](cols.Quantity),
Description: sqlite.Where[Q, string](cols.Description),
Price: sqlite.Where[Q, float32](cols.Price),
Quantity: sqlite.Where[Q, int64](cols.Quantity),
UserID: sqlite.Where[Q, int64](cols.UserID),
}
}
@ -135,13 +133,13 @@ type itemErrors struct {
// All values are optional, and do not have to be set
// Generated columns are not included
type ItemSetter struct {
ID omit.Val[int64] `db:"id,pk" `
Name omit.Val[string] `db:"name" `
Added omit.Val[time.Time] `db:"added" `
Description omitnull.Val[string] `db:"description" `
Price omitnull.Val[float32] `db:"price" `
Quantity omitnull.Val[int64] `db:"quantity" `
UserID omit.Val[int64] `db:"user_id" `
ID omit.Val[int64] `db:"id,pk" `
Name omit.Val[string] `db:"name" `
Added omit.Val[time.Time] `db:"added" `
Description omit.Val[string] `db:"description" `
Price omit.Val[float32] `db:"price" `
Quantity omit.Val[int64] `db:"quantity" `
UserID omit.Val[int64] `db:"user_id" `
}
func (s ItemSetter) SetColumns() []string {
@ -188,13 +186,13 @@ func (s ItemSetter) Overwrite(t *Item) {
t.Added, _ = s.Added.Get()
}
if !s.Description.IsUnset() {
t.Description, _ = s.Description.GetNull()
t.Description, _ = s.Description.Get()
}
if !s.Price.IsUnset() {
t.Price, _ = s.Price.GetNull()
t.Price, _ = s.Price.Get()
}
if !s.Quantity.IsUnset() {
t.Quantity, _ = s.Quantity.GetNull()
t.Quantity, _ = s.Quantity.Get()
}
if !s.UserID.IsUnset() {
t.UserID, _ = s.UserID.Get()

View File

@ -0,0 +1,361 @@
// 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
}

View File

@ -1,4 +1,4 @@
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
// 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