dump script for user db

pull/2/head
vilmibm 2023-03-16 06:41:22 +00:00
parent f624483614
commit c5590cac95
2 changed files with 142 additions and 14 deletions

View File

@ -9,11 +9,14 @@ import (
"git.tilde.town/tildetown/town/models" "git.tilde.town/tildetown/town/models"
"git.tilde.town/tildetown/town/signup" "git.tilde.town/tildetown/town/signup"
"git.tilde.town/tildetown/town/stats"
"git.tilde.town/tildetown/town/towndb"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
) )
// TODO ok so right now this just does one hyper specific thing, but I want to // this is basically a pile of scripts. no hope is to be found here. this is
// add more similar scripts here. this might warrant the introduction of cobra. // dirty, one off code stored in case any useful patterns are worth extracting
// or for copypasta fodder.
func confirmContinue(msg string) { func confirmContinue(msg string) {
var serr error var serr error
@ -41,6 +44,108 @@ type jsonSignup struct {
} }
func main() { 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") lol, err := os.ReadFile("/town/var/signups.json")
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -27,34 +27,57 @@ type AdminNote struct {
} }
func (n *AdminNote) Insert(db *sql.DB) error { func (n *AdminNote) Insert(db *sql.DB) error {
n.Created = time.Now() var (
stmt, err := db.Prepare(` err error
INSERT INTO notes (created, authorid, content, userid) stmt *sql.Stmt
VALUES ( result sql.Result
?, ?, ?, ? liid int64
)`) )
tx, err := db.Begin()
if err != nil { if err != nil {
return err 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.Created.Unix(),
n.AuthorID, n.AuthorID,
n.Content, n.Content)
n.UserID)
if err != nil { if err != nil {
return err return err
} }
defer stmt.Close()
liid, err := result.LastInsertId() liid, err = result.LastInsertId()
if err != nil { if err != nil {
return err return err
} }
n.ID = liid 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 { type TownUser struct {