utils

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetDeltaPath = func() (string, error) {
	cwd, err := os.Getwd()
	if err != nil {
		return "", &errschemer.SchemerErr{
			Code:    "0011",
			Message: "failed to get current working.",
			Err:     err,
		}
	}
	return filepath.Join(cwd, "deltas"), nil
}

GetDeltaPath returns the absolute path to the deltas directory. Assumes the deltas directory is located in the current working directory.

Returns:

  • string: full path to the deltas directory
  • error: non-nil if the current working directory cannot be resolved
View Source
var WithConn = func(connString string, fn func(*pgx.Conn, context.Context) error) error {
	ctx := context.Background()
	connection, err := pgx.Connect(ctx, connString)
	if err != nil {
		return &er.SchemerErr{
			Code:    "0008",
			Message: "failed to conntect with database.",
			Err:     err,
		}
	}
	defer connection.Close(ctx)

	return fn(connection, ctx)
}

WithConn establishes a pgx connection and executes the provided function with it. Automatically handles connection opening, context setup, and deferred closing.

Params:

  • connString: PostgreSQL connection string
  • fn: a callback function that receives the opened connection and context

Returns:

  • error: any error encountered during connection or from the callback execution

Functions

func ConnectDatabase

func ConnectDatabase(connString string) (*sql.DB, error)

ConnectDatabase opens and verifies a PostgreSQL connection using the given connection string. Uses the pgx driver and ensures the connection is valid by performing a ping.

Params:

  • connString: PostgreSQL connection string

Returns:

  • *sql.DB: an open and verified database connection
  • error: non-nil if connection or ping fails

func CreateSchemerTable

func CreateSchemerTable(database *pgx.Conn, ctx context.Context) error

CreateSchemerTable creates the schemer tracking table if it does not already exist. Reads the table schema from schemer.sql located in the deltas directory.

Params:

  • database: pointer to an open pgx.Conn
  • ctx: context for executing the database operations

Returns:

  • error: non-nil if the table check, file read, or table creation fails

func LoadDotEnv

func LoadDotEnv() (bool, error)

LoadDotEnv attempts to load environment variables from a .env file in the current directory. Logs a message if the file is missing (which is expected in production).

Returns:

  • bool: true if the .env file was successfully loaded; false if it was not found
  • error: non-nil if an error occurred while attempting to read the file (other than not found)

func ToPrefix

func ToPrefix(tag int) string

ToPrefix returns the tag as a zero padded string

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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