mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 10:46:50 +00:00
25 lines
492 B
Go
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
|
|
}
|