Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore generated column when migrating #208

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading