Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"fmt"
"testing/fstest"
sconfig "github.com/exoscale/stelling/config"
"github.com/exoscale/stelling/fxsqlite"
"go.uber.org/fx"
"gorm.io/gorm"
)
type Config struct {
fxsqlite.DB
}
func main() {
conf := &Config{}
args := []string{"sqlite-test", "--db.path", "/tmp/db.sqlite"}
if err := sconfig.Load(conf, args); err != nil {
panic(err)
}
migrations := makeTestFs()
app := fx.New(fx.Options(
fxsqlite.NewModule(
conf,
fxsqlite.WithMigrations(migrations, "."),
fxsqlite.WithSQLiteOptions(fxsqlite.WithInMemory()),
),
fx.Invoke(run),
))
app.Run()
}
type example struct {
Name string `gorm:"name"`
Value int `gorm:"value"`
}
func run(lc fx.Lifecycle, sd fx.Shutdowner, db *gorm.DB) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
row, err := gorm.G[example](db).First(ctx)
if err != nil {
return err
}
fmt.Println("name", row.Name)
fmt.Println("value", row.Value)
return sd.Shutdown()
},
})
}
const schemaUp = `CREATE TABLE IF NOT EXISTS examples (
name text,
value number,
PRIMARY KEY(name)
);
INSERT INTO examples(name, value) VALUES ('test', 1);`
const schemaDown = `DELETE TABLE IF EXISTS exampes;`
func makeTestFs() fstest.MapFS {
output := make(map[string]*fstest.MapFile)
output["0001_init_schema.up.sql"] = &fstest.MapFile{
Data: []byte(schemaUp),
}
output["0001_init_schema.down.sql"] = &fstest.MapFile{
Data: []byte(schemaDown),
}
return output
}
Output: name test value 1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ModuleOption ¶
func WithMigrations ¶
func WithMigrations(migrations fs.FS, path string) ModuleOption
WithMigrations will run the migrations at the path in the given FS during system startup By default it will bring the schema to the latest version, but this can be tweaked by using the WithSchemaVersion ModuleOption
func WithSQLiteOptions ¶
func WithSQLiteOptions(sqliteOpts ...Option) ModuleOption
WithSQLiteOptions will provide the given SqliteOptions to the sql.DB produced by the module
func WithSchemaVersion ¶
func WithSchemaVersion(version uint64) ModuleOption
WithSchemaVersion will bring the schema to the given version rather than the latest version It has no effect if WithMigrations is not specified as well
type Option ¶
type Option func(*sqliteConfig)
func WithBusyTimeout ¶
WithBusyTimeout sets a busy handler on each connection that waits up to Duration to acquire the write lock See: https://www.sqlite.org/c3ref/busy_timeout.html
func WithInMemory ¶
func WithInMemory() Option
WithInMemory will create a new in memory database rather than opening an existing one from disk
func WithPoolSize ¶
WithPoolSize sets the maximum number of connections to the sqlite database The default value is 1, since sqlite only allows a single thread to write, but for read intensive applications it can be useful to increase this value
type SchemaVersion ¶
type SchemaVersion uint64