mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-19 19:37:59 +02:00
Add initial code to support unique per-entry IDs
This code has two caveats for old entries: 1. the ID is being backfiled with a random per-(device,entry) ID. So the ID won't match cross-device. 2. the server-side ID will still be a random ID that is unrelated to the entry ID
This commit is contained in:
parent
2a5a6d65c4
commit
a5f11af150
@ -171,9 +171,21 @@ func install(secretKey string, offline bool) error {
|
|||||||
// No config, so set up a new installation
|
// No config, so set up a new installation
|
||||||
return lib.Setup(secretKey, offline)
|
return lib.Setup(secretKey, offline)
|
||||||
}
|
}
|
||||||
|
err = handleDbUpgrades(hctx.MakeContext())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handles people running `hishtory update` when the DB needs updating.
|
||||||
|
func handleDbUpgrades(ctx context.Context) error {
|
||||||
|
db := hctx.GetDb(ctx)
|
||||||
|
return db.Exec(`
|
||||||
|
UPDATE history_entries SET entry_id = lower(hex(randomblob(12))) WHERE entry_id IS NULL
|
||||||
|
`).Error
|
||||||
|
}
|
||||||
|
|
||||||
// Handles people running `hishtory update` from an old version of hishtory that
|
// Handles people running `hishtory update` from an old version of hishtory that
|
||||||
// doesn't support the control-r integration, so that they'll get control-r enabled
|
// doesn't support the control-r integration, so that they'll get control-r enabled
|
||||||
// but someone who has it explicitly disabled will keep it that way.
|
// but someone who has it explicitly disabled will keep it that way.
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/ddworken/hishtory/client/hctx"
|
"github.com/ddworken/hishtory/client/hctx"
|
||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -281,6 +282,9 @@ func buildPreArgsHistoryEntry(ctx context.Context) (*data.HistoryEntry, error) {
|
|||||||
config := hctx.GetConf(ctx)
|
config := hctx.GetConf(ctx)
|
||||||
entry.DeviceId = config.DeviceId
|
entry.DeviceId = config.DeviceId
|
||||||
|
|
||||||
|
// entry ID
|
||||||
|
entry.EntryId = uuid.Must(uuid.NewRandom()).String()
|
||||||
|
|
||||||
// custom columns
|
// custom columns
|
||||||
cc, err := buildCustomColumns(ctx)
|
cc, err := buildCustomColumns(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -39,6 +38,7 @@ type HistoryEntry struct {
|
|||||||
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
|
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
|
||||||
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex,index:end_time_index"`
|
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex,index:end_time_index"`
|
||||||
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
|
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
|
||||||
|
EntryId string `json:"entry_id" gorm:"uniqueIndex:compositeindex,uniqueIndex:entry_id_index"`
|
||||||
CustomColumns CustomColumns `json:"custom_columns"`
|
CustomColumns CustomColumns `json:"custom_columns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ func EncryptHistoryEntry(userSecret string, entry HistoryEntry) (shared.EncHisto
|
|||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
UserId: UserId(userSecret),
|
UserId: UserId(userSecret),
|
||||||
Date: entry.EndTime,
|
Date: entry.EndTime,
|
||||||
EncryptedId: uuid.Must(uuid.NewRandom()).String(),
|
EncryptedId: entry.EntryId,
|
||||||
ReadCount: 0,
|
ReadCount: 0,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -103,6 +103,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
|
|||||||
db.AutoMigrate(&data.HistoryEntry{})
|
db.AutoMigrate(&data.HistoryEntry{})
|
||||||
db.Exec("PRAGMA journal_mode = WAL")
|
db.Exec("PRAGMA journal_mode = WAL")
|
||||||
db.Exec("CREATE INDEX IF NOT EXISTS end_time_index ON history_entries(end_time)")
|
db.Exec("CREATE INDEX IF NOT EXISTS end_time_index ON history_entries(end_time)")
|
||||||
|
db.Exec("CREATE INDEX IF NOT EXISTS entry_id_index ON history_entries(entry_id)")
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,6 +303,7 @@ func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error
|
|||||||
StartTime: time.Now().UTC(),
|
StartTime: time.Now().UTC(),
|
||||||
EndTime: time.Now().UTC(),
|
EndTime: time.Now().UTC(),
|
||||||
DeviceId: config.DeviceId,
|
DeviceId: config.DeviceId,
|
||||||
|
EntryId: uuid.Must(uuid.NewRandom()).String(),
|
||||||
}
|
}
|
||||||
err = ReliableDbCreate(db, entry)
|
err = ReliableDbCreate(db, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -326,6 +327,7 @@ func MakeFakeHistoryEntry(command string) data.HistoryEntry {
|
|||||||
StartTime: time.Unix(fakeHistoryTimestamp, 0).UTC(),
|
StartTime: time.Unix(fakeHistoryTimestamp, 0).UTC(),
|
||||||
EndTime: time.Unix(fakeHistoryTimestamp+3, 0).UTC(),
|
EndTime: time.Unix(fakeHistoryTimestamp+3, 0).UTC(),
|
||||||
DeviceId: "fake_device_id",
|
DeviceId: "fake_device_id",
|
||||||
|
EntryId: uuid.Must(uuid.NewRandom()).String(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user