rentease/internal/driver/database/postgres.go
2024-09-13 18:55:35 +02:00

25 lines
492 B
Go

package database
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func New(connectionString string) (*gorm.DB, error) {
db, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("error connecting to the database %w", err)
}
return db, nil
}
func Migrate(db *gorm.DB, tables ...any) error {
err := db.AutoMigrate(tables...)
if err != nil {
return fmt.Errorf("error migrating the database %w", err)
}
return nil
}