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:
David Dworken 2023-09-22 13:16:24 -07:00
parent 2a5a6d65c4
commit a5f11af150
No known key found for this signature in database
6 changed files with 22 additions and 2 deletions

View File

@ -171,9 +171,21 @@ func install(secretKey string, offline bool) error {
// No config, so set up a new installation
return lib.Setup(secretKey, offline)
}
err = handleDbUpgrades(hctx.MakeContext())
if err != nil {
return err
}
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
// 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.

View File

@ -18,6 +18,7 @@ import (
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/ddworken/hishtory/shared"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
@ -281,6 +282,9 @@ func buildPreArgsHistoryEntry(ctx context.Context) (*data.HistoryEntry, error) {
config := hctx.GetConf(ctx)
entry.DeviceId = config.DeviceId
// entry ID
entry.EntryId = uuid.Must(uuid.NewRandom()).String()
// custom columns
cc, err := buildCustomColumns(ctx)
if err != nil {

View File

@ -15,7 +15,6 @@ import (
"time"
"github.com/ddworken/hishtory/shared"
"github.com/google/uuid"
)
const (
@ -39,6 +38,7 @@ type HistoryEntry struct {
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex,index:end_time_index"`
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"`
}
@ -136,7 +136,7 @@ func EncryptHistoryEntry(userSecret string, entry HistoryEntry) (shared.EncHisto
Nonce: nonce,
UserId: UserId(userSecret),
Date: entry.EndTime,
EncryptedId: uuid.Must(uuid.NewRandom()).String(),
EncryptedId: entry.EntryId,
ReadCount: 0,
}, nil
}

View File

@ -103,6 +103,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
db.AutoMigrate(&data.HistoryEntry{})
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 entry_id_index ON history_entries(entry_id)")
return db, nil
}

View File

@ -303,6 +303,7 @@ func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error
StartTime: time.Now().UTC(),
EndTime: time.Now().UTC(),
DeviceId: config.DeviceId,
EntryId: uuid.Must(uuid.NewRandom()).String(),
}
err = ReliableDbCreate(db, entry)
if err != nil {

View File

@ -17,6 +17,7 @@ import (
"github.com/ddworken/hishtory/client/data"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
)
const (
@ -326,6 +327,7 @@ func MakeFakeHistoryEntry(command string) data.HistoryEntry {
StartTime: time.Unix(fakeHistoryTimestamp, 0).UTC(),
EndTime: time.Unix(fakeHistoryTimestamp+3, 0).UTC(),
DeviceId: "fake_device_id",
EntryId: uuid.Must(uuid.NewRandom()).String(),
}
}