Skip to content

Commit

Permalink
MM-58683 Fixing data rention (#204)
Browse files Browse the repository at this point in the history
* Adding ON DELETE CASCADE to RootPostID on LLM_Threads table for data retention

* Update to just remove table intead of migrating to avoid downtime
  • Loading branch information
crspeller authored Jun 14, 2024
1 parent f0ef416 commit 11e90a7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ func (p *Plugin) execBuilder(b builder) (sql.Result, error) {

func (p *Plugin) SetupTables() error {
if _, err := p.db.Exec(`
CREATE TABLE IF NOT EXISTS LLM_Threads (
RootPostID TEXT NOT NULL REFERENCES Posts(ID) PRIMARY KEY,
CREATE TABLE IF NOT EXISTS LLM_PostMeta (
RootPostID TEXT NOT NULL REFERENCES Posts(ID) ON DELETE CASCADE PRIMARY KEY,
Title TEXT NOT NULL
);
`); err != nil {
return fmt.Errorf("can't create feeback table: %w", err)
return fmt.Errorf("can't create llm titles table: %w", err)
}

// This fixes data retention issues when a post is deleted for an older version of the postmeta table.
// Migrate from the old table using `"INSERT INTO LLM_PostMeta(RootPostID, Title) SELECT RootPostID, Title from LLM_Threads"`
if _, err := p.db.Exec(`ALTER TABLE IF EXISTS LLM_Threads DROP CONSTRAINT IF EXISTS llm_threads_rootpostid_fkey;`); err != nil {
return fmt.Errorf("failed to migrate constraint: %w", err)
}

return nil
Expand All @@ -77,7 +83,7 @@ func (p *Plugin) saveTitleAsync(threadID, title string) {
}

func (p *Plugin) saveTitle(threadID, title string) error {
_, err := p.execBuilder(p.builder.Insert("LLM_Threads").
_, err := p.execBuilder(p.builder.Insert("LLM_PostMeta").
Columns("RootPostID", "Title").
Values(threadID, title).
Suffix("ON CONFLICT (RootPostID) DO UPDATE SET Title = ?", title))
Expand Down Expand Up @@ -108,7 +114,7 @@ func (p *Plugin) getAIThreads(dmChannelIDs []string) ([]AIThread, error) {
Where(sq.Eq{"ChannelID": dmChannelIDs}).
Where(sq.Eq{"RootId": ""}).
Where(sq.Eq{"DeleteAt": 0}).
LeftJoin("LLM_Threads as t ON t.RootPostID = p.Id").
LeftJoin("LLM_PostMeta as t ON t.RootPostID = p.Id").
OrderBy("CreateAt DESC").
Limit(60).
Offset(0),
Expand Down

0 comments on commit 11e90a7

Please sign in to comment.