updates
This commit is contained in:
41
server/scripts/migration.go
Normal file
41
server/scripts/migration.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package scripts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"go-server/internal/config"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Define models for migration
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Email string `gorm:"uniqueIndex"`
|
||||
Password string
|
||||
}
|
||||
|
||||
// Migrate function to run database migrations
|
||||
func Migrate() {
|
||||
// Load from environment variables or configuration
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dsn := cfg.Database.DSN()
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// AutoMigrate runs the migration
|
||||
err = db.AutoMigrate(&User{})
|
||||
if err != nil {
|
||||
log.Fatalf("Migration failed: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Migration completed successfully!")
|
||||
}
|
||||
Reference in New Issue
Block a user