seeder

package
v1.10.0-sp2242.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package seeder main function is to generate sqlite3 databases for use with the api.

It uses schemas from sqlc to create the tagles within the database and will either seed those tables with data from a series of existing files or by generating new|fake data.

Seed is the main function to call and will create the database and insert data.

Used by:

  • ./commands/seed (which in turn is called within Dockerfile)
  • various test files to create dummy data to test against the api

Index

Constants

This section is empty.

Variables

View Source
var GENERATOR_FUNCTIONS map[string]generatorF = map[string]generatorF{
	"github_standards": githubStandardsGenerator,
	"aws_costs":        awsCostsGenerator,
	"aws_uptime":       awsUptimeGenerator,
}

map of funcs that inset data from files

View Source
var INSERT_FUNCTIONS map[string]insertF = map[string]insertF{
	"github_standards": githubStandardsInsert,
	"aws_costs":        awsCostInsert,
	"aws_uptime":       awsUptimeInsert,
}
View Source
var TRACKER_FUNCTIONS map[string]trackerF = map[string]trackerF{
	"github_standards": githubStandardsTrack,
	"aws_costs":        awsCostsTracker,
	"aws_uptime":       awsUptimeTracker,
}

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)

InsertFile checks the file exists and reads its content into the insertFunc along with the db pointer.

Small wrapper that also find the modification time of the file. This time is then used to track the age of the data being used

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)

Seed tries to create a database at the filepath set (`dbF`) and return the db pointer. This pointer is *NOT* closed, it will need to be handled outside of this function

If there is already a file at `dbF` location, this will exit without error, but will *NOT* create a new version.

A database connection is created (and returned) using the consts.SQL_CONNECTION_PARAMS. The schema file (`schemaF`) is then read and passed into the database to execute and generate the empty tables and indexes. This schema is part of this projects use of sqlc

An insert and tracker function are then checked for, comparing the table name (`table`) against the known list. These are required to be used, so the function will error if a match is not found. See generators.go, insertors.go and trackers.go for list of current versions.

The file pattern (`dataF`) is then used with Glob to find matching files. If they are found, they are iterated over and their contents passed into the insertor function - this function will handle marshaling / coversion and the db record creation

If no files are found but there is a generator function, this will be called instead to place dummy data into the database. The amount of records created is controlled by the `N` parameter

Types

This section is empty.

Jump to

Keyboard shortcuts

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