helper

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustStartPostgresContainer

func MustStartPostgresContainer() (func(ctx context.Context, opts ...testcontainers.TerminateOption) error, string, error)

MustStartPostgresContainer starts a PostgreSQL container for testing purposes. It uses the timescale/timescaledb image with PostgreSQL 17. It returns a function to terminate the container, the port on which the database is accessible, and an error if the container could not be started.

func PrepareModel

func PrepareModel(modelName string, onnxFilePath string) (string, error)

PrepareModel downloads the model if it doesn't exist and returns the model path

func SetTestDatabaseConfigEnvs

func SetTestDatabaseConfigEnvs(t *testing.T, port string)

SetTestDatabaseConfigEnvs sets the environment variables for the test database configuration. It sets the host, port, database name, username, password, schema, and table drop options for the test database.

Types

type Database

type Database struct {
	Name     string
	Logger   *slog.Logger
	Instance *sql.DB
}

Database represents a service that interacts with a database.

func NewDatabase

func NewDatabase(name string, dbConfig *DatabaseConfiguration, logger *slog.Logger) *Database

func NewDatabaseWithDB

func NewDatabaseWithDB(name string, dbConnnection *sql.DB, logger *slog.Logger) *Database

func NewTestDatabase

func NewTestDatabase(config *DatabaseConfiguration) *Database

NewTestDatabase creates a new Database instance for testing purposes. It initializes the database with the provided configuration and the name "test_db". It returns a pointer to the new Database instance.

func (*Database) CheckTableExistance

func (d *Database) CheckTableExistance(tableName string) (bool, error)

CheckTableExistance checks if a table with the specified name exists in the database. It queries the information_schema.tables to check for the existence of the table. It returns true if the table exists, false otherwise, and an error if the query fails.

func (*Database) Close

func (d *Database) Close() error

Close closes the database connection. It logs a message indicating the disconnection from the specific database. If the connection is successfully closed, it returns nil. If an error occurs while closing the connection, it returns the error.

func (*Database) ConnectToDatabase

func (d *Database) ConnectToDatabase(dbConfig *DatabaseConfiguration, logger *slog.Logger)

Internal function for the service creation to connect to a database. DatabaseConfiguration must contain uri, username and password. It initializes the database connection and sets the Instance field of the Database struct.

func (*Database) CreateCombinedIndex

func (d *Database) CreateCombinedIndex(tableName string, columnName1 string, columnName2 string) error

CreateCombinedIndex creates a combined index on the specified columns of the specified table. It uses the PostgreSQL CREATE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) CreateIndex

func (d *Database) CreateIndex(tableName string, columnName string) error

CreateIndex creates an index on the specified column of the specified table. It uses the PostgreSQL CREATE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) CreateIndexes

func (d *Database) CreateIndexes(tableName string, columnNames ...string) error

CreateIndexes creates indexes on the specified columns of the specified table. It iterates over the column names and calls CreateIndex for each one. It returns an error if any of the index creations fail.

func (*Database) CreateUniqueCombinedIndex

func (d *Database) CreateUniqueCombinedIndex(tableName string, columnName1 string, columnName2 string) error

CreateUniqueCombinedIndex creates a unique combined index on the specified columns of the specified table. It uses the PostgreSQL CREATE UNIQUE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) DropIndex

func (d *Database) DropIndex(tableName string, jsonMapKey string) error

DropIndex drops the index on the specified table and column. It uses the PostgreSQL DROP INDEX statement to drop the index. If the index does not exist, it will not return an error. It returns an error if the index dropping fails.

func (*Database) Health

func (d *Database) Health() map[string]string

Health checks the health of the database connection by pinging the database. It returns a map with keys indicating various health statistics.

type DatabaseConfiguration

type DatabaseConfiguration struct {
	Host          string
	Port          string
	Database      string
	Username      string
	Password      string
	Schema        string
	SSLMode       string
	WithTableDrop bool
}

func NewDatabaseConfiguration

func NewDatabaseConfiguration() (*DatabaseConfiguration, error)

NewDatabaseConfiguration creates a new DatabaseConfiguration instance. It reads the database configuration from environment variables. It returns a pointer to the new DatabaseConfiguration instance or an error if any required environment variable is not set.

func (*DatabaseConfiguration) DatabaseConnectionString

func (d *DatabaseConfiguration) DatabaseConnectionString() string

type Error

type Error struct {
	Original error
	Trace    []string
}

func NewError

func NewError(trace string, original error) Error

func (Error) Error

func (e Error) Error() string

type PrettyHandler

type PrettyHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

func NewPrettyHandler

func NewPrettyHandler(
	out io.Writer,
	opts PrettyHandlerOptions,
) *PrettyHandler

func (*PrettyHandler) Handle

func (h *PrettyHandler) Handle(ctx context.Context, r slog.Record) error

type PrettyHandlerOptions

type PrettyHandlerOptions struct {
	SlogOpts slog.HandlerOptions
}

Jump to

Keyboard shortcuts

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