package database

import (
	"fmt"
	"log"

	"git.paulbsd.com/paulbsd/qrz/src/config"
	"github.com/jmoiron/sqlx"
)

// Initialize creates connection to database and exec Schema
func Initialize(config *config.Config) (err error) {
	config.Db, err = sqlx.Connect("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s", config.DbUsername, config.DbPassword, config.DbHostname, config.DbName))
	if err != nil {
		log.Fatalln(err)
	}

	_, err = config.Db.Exec(config.DbSchema)
	if err != nil {
		log.Fatalln(err)
	}

	return
}
