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 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