mirror of
https://github.com/ddworken/hishtory.git
synced 2025-01-23 06:38:52 +01:00
Add completely broken support for an incrementing id
This commit is contained in:
parent
158c2f2c57
commit
da624cf8aa
@ -26,6 +26,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type HistoryEntry struct {
|
type HistoryEntry struct {
|
||||||
|
Local_Id int `json:"local_id" gorm:"type:int;autoIncrement:true"`
|
||||||
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
|
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
|
||||||
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
|
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
|
||||||
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
|
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
|
||||||
|
@ -320,7 +320,7 @@ func AddToDbIfNew(db *gorm.DB, entry data.HistoryEntry) {
|
|||||||
var results []data.HistoryEntry
|
var results []data.HistoryEntry
|
||||||
tx.Limit(1).Find(&results)
|
tx.Limit(1).Find(&results)
|
||||||
if len(results) == 0 {
|
if len(results) == 0 {
|
||||||
db.Create(entry)
|
db.Create(&entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,11 +859,55 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
db.AutoMigrate(&data.HistoryEntry{})
|
err = migrateDbToAddIdColumn(db)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// db.AutoMigrate(&data.HistoryEntry{})
|
||||||
db.Exec("PRAGMA journal_mode = WAL")
|
db.Exec("PRAGMA journal_mode = WAL")
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrateDbToAddIdColumn(db *gorm.DB) error {
|
||||||
|
if !db.Migrator().HasTable(&data.HistoryEntry{}) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if db.Migrator().HasTable("history_entry_tmp") {
|
||||||
|
err := db.Migrator().RenameTable("history_entry_tmp", &data.HistoryEntry{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to rename an existing tmp table: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if db.Migrator().HasColumn(&data.HistoryEntry{}, "local_id") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := db.Migrator().DropIndex(&data.HistoryEntry{}, "compositeindex")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to delete the index: %v", err)
|
||||||
|
}
|
||||||
|
err = db.Migrator().RenameTable(&data.HistoryEntry{}, "history_entry_tmp")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to rename the history table to a tmp table: %v", err)
|
||||||
|
}
|
||||||
|
err = db.Migrator().CreateTable(&data.HistoryEntry{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to create a new history table: %v", err)
|
||||||
|
}
|
||||||
|
rawDb, err := db.DB()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to retrieve a raw DB pointer: %v", err)
|
||||||
|
}
|
||||||
|
_, err = rawDb.Exec("INSERT INTO history_entries SELECT ROW_NUMBER() OVER(ORDER BY CAST(strftime(\"%s\",start_time) AS INTEGER)) as local_id, * FROM history_entry_tmp")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to insert into a new history table: %v", err)
|
||||||
|
}
|
||||||
|
err = db.Migrator().DropTable("history_entry_tmp")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("DB Migration failed: Failed to delete the tmp table: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func ApiGet(path string) ([]byte, error) {
|
func ApiGet(path string) ([]byte, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
resp, err := http.Get(getServerHostname() + path)
|
resp, err := http.Get(getServerHostname() + path)
|
||||||
|
Loading…
Reference in New Issue
Block a user