Skip to content

Commit

Permalink
add migration utility
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenerik committed Jul 28, 2024
1 parent 1344972 commit d6bdeef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
ListenAddress string
DatabaseFile string
Debug bool
MigrateToTeam string
}

func (c *Config) Load() error {
Expand Down Expand Up @@ -43,5 +44,7 @@ func (c *Config) Load() error {
c.Debug = true
}

c.MigrateToTeam = os.Getenv("MIGRATE_TEAM")

return nil
}
35 changes: 34 additions & 1 deletion database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,38 @@ func OpenDatabase(filename string) error {

return nil
})
return err

if err != nil {
return err
}

if App.config.MigrateToTeam != "" {
return migrateDatabase()
}

return nil
}

func migrateDatabase() error {
return App.db.Update(func(tx *bolt.Tx) error {
questions := tx.Bucket([]byte("questions"))

return questions.ForEach(func(k, v []byte) error {
var question Question
err := json.Unmarshal(v, &question)
if err != nil {
return err
}

if question.TeamID == "" {
question.TeamID = App.config.MigrateToTeam
}

data, err := json.Marshal(question)
if err != nil {
return err
}
return questions.Put(k, data)
})
})
}

0 comments on commit d6bdeef

Please sign in to comment.