database

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 15 Imported by: 0

README

Database

This is a package for database setup. More specific postgresql. The package will assume a migrations folder. This is using golang-migrate to perform migrations. However this supports also inserting user and password to do this:

CREATE USER {{.User}} WITH PASSWORD '{{.Password}}';

The package will setup a connection to a postgresql database and perform migrations.

For local development there is a setting to start up a postgresql docker container.

For testing there is a method to be used as testmain which will start up a postgresql docker container and stop it after the tests are done.

Dependencies
  • docker (needs to be installed)
  • golang-migrate (go dependency)
  • testcontainers (go dependency)

Usage

Setup in main function or beginning of app. This will setup a docker container and db connection.

// assuming dbConfig is populated
if dbConfig.StartupLocal == "true" {
    containerCleanUp, err := database.SetupContainer(dbConfig)
    if err != nil {
        panic(err)
    }
    defer containerCleanUp()
}

// Sets up db connection and migrations
db, err := dbSetup(dbConfig) 
if err != nil {
    panic(err)
}
defer db.Close()

// Do stuff with db connection

For testing use the testmain method

// For instance in a file called main_test.go

// package variable to be used in tests
var dbConfig database.DbConfig

func TestMain(m *testing.M) {
	dbConfig = database.DbConfig{
		User:        "test",
		Password:    "test",
		Name:        "test",
		AppUser:     "app_user",
		AppPassword: "app_password",
	}
	database.SinglePostgresTestMain(m, &dbConfig, "../../../migrations")
}

In a test file:

func TestInsertGetTodos(t *testing.T) {
    // using dbConfig variable from testmain
	db, err := database.Setup(&dbConfig)
	if err != nil {
		t.Fatalf("Failed to setup database: %v", err)
	}
	defer db.Close()
	todoRepository := NewTodoRepository(db)

    // Do stuff with todoRepository
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(dbConfig *DbConfig, migrationsPath string) error

func Setup

func Setup(dbConfig *DbConfig) (*sql.DB, error)

func SetupContainer

func SetupContainer(dbConfig *DbConfig) (func(), error)

*

  • This will setup a new postgres container and it's cleanup function
  • The function will also change the host and port of the dbConfig to the container's host and port

func SinglePostgresTestMain

func SinglePostgresTestMain(m *testing.M, dbConfig *DbConfig, migrationPath string)

*

  • This function is used to run a test that requires a single postgres container
  • The function will setup the container, run the test, and clean up the container

Types

type DbConfig

type DbConfig struct {
	Host         string
	Name         string
	Port         string
	User         string
	Password     string
	AppUser      string
	AppPassword  string
	RunBaseLine  string
	StartupLocal string // Startup local database when app starts
}

func NewDbConfig

func NewDbConfig(
	host string,
	name string,
	port string,
	user string,
	password string,
	appUser string,
	appPassword string,
	runBaseLine string,
	startupLocal string,
) *DbConfig

Jump to

Keyboard shortcuts

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