mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-19 19:37:59 +02:00
Create func to automatically create DB indexes rather than just documenting them in a comment that has to be manually executed
This commit is contained in:
parent
51f2c15f35
commit
cc11916f3c
@ -63,6 +63,26 @@ func (db *DB) AddDatabaseTables() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) CreateIndices() error {
|
||||||
|
// Note: If adding a new index here, consider manually running it on the prod DB using CONCURRENTLY to
|
||||||
|
// make server startup non-blocking. The benefit of this function is primarily for other people so they
|
||||||
|
// don't have to manually create these indexes.
|
||||||
|
var indices = []string{
|
||||||
|
`CREATE INDEX IF NOT EXISTS entry_id_idx ON enc_history_entries USING btree(encrypted_id);`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS device_id_idx ON enc_history_entries USING btree(device_id);`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS read_count_idx ON enc_history_entries USING btree(read_count);`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS redact_idx ON enc_history_entries USING btree(user_id, device_id, date);`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS del_user_idx ON deletion_requests USING btree(user_id);`,
|
||||||
|
}
|
||||||
|
for _, index := range indices {
|
||||||
|
r := db.Exec(index)
|
||||||
|
if r.Error != nil {
|
||||||
|
return fmt.Errorf("failed to execute index creation sql=%#v: %w", index, r.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *DB) Close() error {
|
func (db *DB) Close() error {
|
||||||
rawDB, err := db.DB.DB()
|
rawDB, err := db.DB.DB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -95,6 +95,10 @@ func OpenDB() (*database.DB, error) {
|
|||||||
return nil, fmt.Errorf("failed to create underlying DB tables: %w", err)
|
return nil, fmt.Errorf("failed to create underlying DB tables: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
err := db.CreateIndices()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,15 +20,6 @@ type EncHistoryEntry struct {
|
|||||||
ReadCount int `json:"read_count"`
|
ReadCount int `json:"read_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Manually created the indices:
|
|
||||||
CREATE INDEX CONCURRENTLY entry_id_idx ON enc_history_entries USING btree(encrypted_id);
|
|
||||||
CREATE INDEX CONCURRENTLY device_id_idx ON enc_history_entries USING btree(device_id);
|
|
||||||
CREATE INDEX CONCURRENTLY read_count_idx ON enc_history_entries USING btree(read_count);
|
|
||||||
CREATE INDEX CONCURRENTLY redact_idx ON enc_history_entries USING btree(user_id, device_id, date);
|
|
||||||
CREATE INDEX CONCURRENTLY del_user_idx ON deletion_requests USING btree(user_id);
|
|
||||||
*/
|
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
UserId string `json:"user_id"`
|
UserId string `json:"user_id"`
|
||||||
DeviceId string `json:"device_id"`
|
DeviceId string `json:"device_id"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user