vegapokerbot/internal/db/init.go
2024-05-07 21:49:09 +03:00

40 lines
847 B
Go

package db
import (
"errors"
_ "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/migrations"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/repository"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Repositories struct {
Chat *repository.Chat
User *repository.User
}
func Connect(dsn string) (*gorm.DB, error) {
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
func InitRepositories(db *gorm.DB) *Repositories {
return &Repositories{
Chat: repository.NewChat(db),
User: repository.NewUser(db),
}
}
func Migrate(dsn string) error {
migrator, err := migrate.New("embed://", dsn)
if err != nil {
return err
}
err = migrator.Up()
if errors.Is(err, migrate.ErrNoChange) {
return nil
}
return err
}