From c5590cac9588fbdc2a95f4cbd47b8587ba391f32 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 16 Mar 2023 06:41:22 +0000 Subject: [PATCH] dump script for user db --- cmd/dumps/main.go | 109 +++++++++++++++++++++++++++++++++++++++++++++- towndb/towndb.go | 47 +++++++++++++++----- 2 files changed, 142 insertions(+), 14 deletions(-) diff --git a/cmd/dumps/main.go b/cmd/dumps/main.go index dd9ce64..fc83e2a 100644 --- a/cmd/dumps/main.go +++ b/cmd/dumps/main.go @@ -9,11 +9,14 @@ import ( "git.tilde.town/tildetown/town/models" "git.tilde.town/tildetown/town/signup" + "git.tilde.town/tildetown/town/stats" + "git.tilde.town/tildetown/town/towndb" "github.com/AlecAivazis/survey/v2" ) -// TODO ok so right now this just does one hyper specific thing, but I want to -// add more similar scripts here. this might warrant the introduction of cobra. +// this is basically a pile of scripts. no hope is to be found here. this is +// dirty, one off code stored in case any useful patterns are worth extracting +// or for copypasta fodder. func confirmContinue(msg string) { var serr error @@ -41,6 +44,108 @@ type jsonSignup struct { } func main() { + db, err := towndb.ConnectDB() + if err != nil { + panic(err) + } + + lol, err := os.ReadFile("/town/var/users.json") + if err != nil { + panic(err) + } + + td, err := stats.Stats() + if err != nil { + panic(err) + } + + lines := strings.Split(string(lol), "\n") + errs := []error{} + signups := make([]jsonSignup, len(lines)) + for i, l := range lines { + l = strings.TrimSpace(l) + if l == "" { + continue + } + s := jsonSignup{} + err := json.Unmarshal([]byte(l), &s) + if err != nil { + fmt.Printf("%s %s", l, err.Error()) + errs = append(errs, err) + } else { + signups[i] = s + } + } + if len(errs) > 0 { + confirmContinue(fmt.Sprintf("%d errors found deserializing; continue?", len(errs))) + } + + ttbirth, err := time.Parse("2006-01-02 3:04pm", "2014-10-11 11:49pm") + if err != nil { + panic(err) + } + + me := towndb.TownUser{ + Created: ttbirth, + Username: "vilmibm", + Emails: []string{"vilmibm@protonmail.com"}, + State: towndb.StateActive, + IsAdmin: true, + } + err = me.Insert(db) + if err != nil { + panic(err) + } + + notFound := []jsonSignup{} + found := []jsonSignup{} + for _, su := range signups { + fl := len(found) + for _, u := range td.Users { + if su.Username == u.Username { + found = append(found, su) + break + } + } + if len(found) == fl { + notFound = append(notFound, su) + } + } + if len(notFound) > 0 { + confirmContinue(fmt.Sprintf("%d of those were not found. continue?", len(notFound))) + } + + for _, su := range found { + var emails []string + if su.Email != "" { + emails = []string{su.Email} + } + + u := towndb.TownUser{ + Created: time.Unix(int64(su.Created), 0), + Emails: emails, + Username: su.Username, + State: towndb.StateActive, + } + if err = u.Insert(db); err != nil { + confirmContinue(fmt.Sprintf("%#v led to error %s; continue?", u, err.Error())) + } + + if su.Notes != "" { + note := towndb.AdminNote{ + Created: time.Time{}, + AuthorID: me.ID, + Content: su.Notes, + UserID: u.ID, + } + if err = note.Insert(db); err != nil { + confirmContinue(fmt.Sprintf("%#v led to error %s; continue?", note, err.Error())) + } + } + } +} + +func importSignups() { lol, err := os.ReadFile("/town/var/signups.json") if err != nil { panic(err) diff --git a/towndb/towndb.go b/towndb/towndb.go index e77e5b7..52d073a 100644 --- a/towndb/towndb.go +++ b/towndb/towndb.go @@ -27,34 +27,57 @@ type AdminNote struct { } func (n *AdminNote) Insert(db *sql.DB) error { - n.Created = time.Now() - stmt, err := db.Prepare(` - INSERT INTO notes (created, authorid, content, userid) - VALUES ( - ?, ?, ?, ? - )`) + var ( + err error + stmt *sql.Stmt + result sql.Result + liid int64 + ) + + tx, err := db.Begin() if err != nil { return err } - result, err := stmt.Exec( + defer func() { + if err != nil { + tx.Rollback() + } + }() + stmt, err = tx.Prepare(` + INSERT INTO notes (created, author, content) + VALUES (?, ?, ?)`) + if err != nil { + return err + } + + result, err = stmt.Exec( n.Created.Unix(), n.AuthorID, - n.Content, - n.UserID) + n.Content) if err != nil { return err } - defer stmt.Close() - liid, err := result.LastInsertId() + liid, err = result.LastInsertId() if err != nil { return err } n.ID = liid - return nil + stmt, err = tx.Prepare(` + INSERT INTO user_notes (noteid, userid) VALUES (?, ?)`) + if err != nil { + return err + } + + _, err = stmt.Exec(n.ID, n.UserID) + if err != nil { + return err + } + + return tx.Commit() } type TownUser struct {