seeder

package
v1.0.0-sql.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GENERATOR_FUNCTIONS map[string]generatorF = map[string]generatorF{

	"github_standards": func(ctx context.Context, num int, db *sql.DB) (err error) {
		mu := &sync.Mutex{}
		wg := sync.WaitGroup{}

		owner := fake.String(12)
		tx, err := db.Begin()
		if err != nil {
			return
		}
		defer tx.Rollback()
		q := ghs.New(db)
		qtx := q.WithTx(tx)

		slog.Info("starting generation", slog.String("for", "github_standards"))
		tick := testhelpers.T()
		for x := 0; x < num; x++ {
			wg.Add(1)
			go func(i int) {
				mu.Lock()
				g := ghs.Fake(nil, &owner)
				qtx.Insert(ctx, g.Insertable())
				mu.Unlock()
				wg.Done()
			}(x)
		}

		wg.Wait()
		slog.Info("generation complete", slog.String("seconds", tick.Stop().Seconds()), slog.String("for", "github_standards"))

		return tx.Commit()
	},
}

map of funcs that inset data from files

View Source
var INSERT_FUNCTIONS map[string]insertF = map[string]insertF{
	"github_standards": func(ctx context.Context, fileContent []byte, db *sql.DB) (err error) {

		repos := []*ghs.GithubStandard{}
		repos, err = convert.Unmarshals[*ghs.GithubStandard](fileContent)
		if err != nil {
			return
		}

		mu := &sync.Mutex{}
		wg := sync.WaitGroup{}
		tx, err := db.Begin()
		if err != nil {
			return
		}
		defer tx.Rollback()
		q := ghs.New(db)
		qtx := q.WithTx(tx)

		slog.Debug("inserting content", slog.String("for", "github_standards"), slog.Int("count", len(repos)))
		tick := testhelpers.T()
		for _, item := range repos {
			wg.Add(1)
			go func(g *ghs.GithubStandard) {
				mu.Lock()
				qtx.Insert(ctx, g.Insertable())
				mu.Unlock()
				wg.Done()
			}(item)
		}

		wg.Wait()
		slog.Info("insert complete",
			slog.Int("count", len(repos)),
			slog.String("seconds", tick.Stop().Seconds()),
			slog.String("for", "github_standards"))

		return tx.Commit()
	},
}
View Source
var TRACKER_FUNCTIONS map[string]trackerF = map[string]trackerF{
	"github_standards": func(ctx context.Context, ts time.Time, db *sql.DB) (err error) {
		q := ghs.New(db)
		err = q.Track(ctx, ts.String())
		return
	},
}

Functions

func DB

func DB(dbFile string) (db *sql.DB, err error)

create a db

func InsertFile

func InsertFile(ctx context.Context, file string, insertFunc insertF, db *sql.DB) (err error, ts *time.Time)

func SchemaLoad

func SchemaLoad(db *sql.DB, schemaFile string) (err error)

SchemaLoad generates a schema from a sqlc file whose filepath is passed

func Seed

func Seed(ctx context.Context, dbF string, schemaF string, dataF string, table string, N int) (db *sql.DB, err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL