(db *gorm.DB)
| 98 | } |
| 99 | |
| 100 | func fillMissingSortKeys(db *gorm.DB) error { |
| 101 | missingSort := int64(0) |
| 102 | if err := db.Model(new(model.Application)).Where("sort_key IS NULL OR sort_key = ''").Count(&missingSort).Error; err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | if missingSort == 0 { |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | var apps []*model.Application |
| 111 | if err := db.Order("user_id, sort_key, id ASC").Find(&apps).Error; err != nil && err != gorm.ErrRecordNotFound { |
| 112 | return err |
| 113 | } |
| 114 | fmt.Println("Migrating", len(apps), "application sort keys") |
| 115 | |
| 116 | sortKey := "" |
| 117 | currentUser := uint(math.MaxUint) |
| 118 | var err error |
| 119 | for _, app := range apps { |
| 120 | if currentUser != app.UserID { |
| 121 | sortKey = "" |
| 122 | currentUser = app.UserID |
| 123 | } |
| 124 | sortKey, err = fracdex.KeyBetween(sortKey, "") |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | app.SortKey = sortKey |
| 129 | } |
| 130 | return db.Save(apps).Error |
| 131 | } |
| 132 | |
| 133 | func createDirectoryIfSqlite(dialect, connection string) { |
| 134 | if dialect == "sqlite3" { |
searching dependent graphs…