2024-05-07 21:49:09 +03:00
|
|
|
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 {
|
2024-05-09 20:48:59 +03:00
|
|
|
Chat *repository.Chat
|
|
|
|
User *repository.User
|
|
|
|
Integration *repository.Integration
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(dsn string) (*gorm.DB, error) {
|
|
|
|
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitRepositories(db *gorm.DB) *Repositories {
|
|
|
|
return &Repositories{
|
2024-05-09 20:48:59 +03:00
|
|
|
Chat: repository.NewChat(db),
|
|
|
|
User: repository.NewUser(db),
|
|
|
|
Integration: repository.NewIntegration(db),
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|