Merge remote-tracking branch 'mawalu/migrate-registrations-in-batches'

This commit is contained in:
Tulir Asokan 2019-11-10 19:12:57 +02:00
commit a0658a5634

View file

@ -17,19 +17,34 @@ func init() {
if len(registrations) == 0 { if len(registrations) == 0 {
return nil return nil
} }
values := make([]interface{}, 0, len(registrations))
valueStrings := make([]string, 0, len(registrations)) executeBatch := func(tx *sql.Tx, valueStrings []string, values ...interface{}) error {
valueString := strings.Join(valueStrings, ",")
_, err := tx.Exec("INSERT INTO mx_registrations (user_id) VALUES "+valueString, values...)
return err
}
batchSize := 100
values := make([]interface{}, 0, batchSize)
valueStrings := make([]string, 0, batchSize)
i := 1 i := 1
for userID, registered := range registrations { for userID, registered := range registrations {
if i == batchSize {
err := executeBatch(tx, valueStrings, values...)
if err != nil {
return err
}
i = 1
values = make([]interface{}, 0, batchSize)
valueStrings = make([]string, 0, batchSize)
}
if registered { if registered {
values = append(values, userID) values = append(values, userID)
valueStrings = append(valueStrings, fmt.Sprintf("($%d)", i)) valueStrings = append(valueStrings, fmt.Sprintf("($%d)", i))
i++ i++
} }
} }
valueString := strings.Join(valueStrings, ",") return executeBatch(tx, valueStrings, values...)
_, err := tx.Exec("INSERT INTO mx_registrations (user_id) VALUES "+valueString, values...)
return err
} }
migrateMemberships := func(tx *sql.Tx, rooms map[string]map[string]mautrix.Membership) error { migrateMemberships := func(tx *sql.Tx, rooms map[string]map[string]mautrix.Membership) error {