Skip to content

Commit

Permalink
fix: ignore generated column when migrating (#208)
Browse files Browse the repository at this point in the history
Workaround for this issue in GORM: go-gorm/gorm#5534
  • Loading branch information
bfabio authored Aug 23, 2023
1 parent 7a11205 commit 079780c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,16 @@ func NewDatabase(connection string) (*gorm.DB, error) {
return nil, fmt.Errorf("can't migrate database: %w", err)
}

// Migrate logs only if there is no "entity" column yet, which should mean when the database
// is empty.
// This is a workaround for https://github.com/go-gorm/gorm/issues/5534 where GORM
// fails to migrate an existing generated column on PostgreSQL if it already exists.
var entity string
if database.Raw("SELECT entity from logs LIMIT 1").Scan(&entity).Error != nil {
if err = database.AutoMigrate(&models.Log{}); err != nil {
return nil, fmt.Errorf("can't migrate database: %w", err)
}
}

return database, nil
}

0 comments on commit 079780c

Please sign in to comment.