Skip to content

Commit

Permalink
Create indices on recently added fields (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgiannuzzi authored May 2, 2023
1 parent 5441d97 commit 4ad03ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 19 additions & 2 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func ConnectDB(dsn string, slowThreshold time.Duration, poolMax int, reset bool,
tx.First(&schemaVersion)
}

if alembicVersion.Version != "97727af70f4d" || schemaVersion.Version != "8073e7e037e5" {
if alembicVersion.Version != "97727af70f4d" || schemaVersion.Version != "ed364de02645" {
if !migrate && alembicVersion.Version != "" {
return fmt.Errorf("unsupported database schema versions alembic %s, fasttrack %s", alembicVersion.Version, schemaVersion.Version)
}
Expand Down Expand Up @@ -311,6 +311,23 @@ func ConnectDB(dsn string, slowThreshold time.Duration, poolMax int, reset bool,
return fmt.Errorf("error migrating database to fasttrack schema 8073e7e037e5: %w", err)
}

case "8073e7e037e5":
log.Info("Migrating database to fasttrack schema ed364de02645")
if err := DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Migrator().CreateIndex(&Run{}, "RowNum"); err != nil {
return err
}
if err := tx.Migrator().CreateIndex(&Metric{}, "Iter"); err != nil {
return err
}
return tx.Model(&SchemaVersion{}).
Where("1 = 1").
Update("Version", "ed364de02645").
Error
}); err != nil {
return fmt.Errorf("error migrating database to fasttrack schema ed364de02645: %w", err)
}

default:
return fmt.Errorf("unsupported database fasttrack schema version %s", schemaVersion.Version)
}
Expand Down Expand Up @@ -339,7 +356,7 @@ func ConnectDB(dsn string, slowThreshold time.Duration, poolMax int, reset bool,
Version: "97727af70f4d",
})
tx.Create(&SchemaVersion{
Version: "8073e7e037e5",
Version: "ed364de02645",
})
tx.Commit()
if tx.Error != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Run struct {
ExperimentID int32
Experiment Experiment
DeletedTime sql.NullInt64 `gorm:"type:bigint"`
RowNum RowNum
RowNum RowNum `gorm:"index"`
Params []Param
Tags []Tag
Metrics []Metric
Expand Down Expand Up @@ -110,7 +110,7 @@ type Metric struct {
RunID string `gorm:"column:run_uuid;not null;primaryKey;index"`
Step int64 `gorm:"default:0;not null;primaryKey"`
IsNan bool `gorm:"default:false;not null;primaryKey"`
Iter int64
Iter int64 `gorm:"index"`
}

type LatestMetric struct {
Expand Down

0 comments on commit 4ad03ec

Please sign in to comment.