Skip to content

Instantly share code, notes, and snippets.

@vmihailenco
Created February 9, 2016 09:35
Show Gist options
  • Save vmihailenco/2ab6aaa5b8143c4b1b67 to your computer and use it in GitHub Desktop.
Save vmihailenco/2ab6aaa5b8143c4b1b67 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"math/rand"
"gopkg.in/pg.v4"
)
func main() {
db := pg.Connect(&pg.Options{
User: "postgres",
})
qs := []string{
`DROP TABLE IF EXISTS repack_issue_70`,
`CREATE TABLE repack_issue_70 (id serial PRIMARY KEY, hash int)`,
`CREATE UNIQUE INDEX on repack_issue_70 (hash) WHERE hash IS NOT NULL`,
}
for _, q := range qs {
_, err := db.Exec(q)
if err != nil {
log.Fatal(err)
}
}
for i := 0; i < 100; i++ {
go thread(db)
}
log.Print("ready...")
select {}
}
func thread(db *pg.DB) {
for i := uint(0); ; i++ {
hash := rand.Intn(100)
_, err := db.Exec(`
INSERT INTO repack_issue_70 (hash) VALUES (?)
ON CONFLICT DO NOTHING
`, hash)
if err != nil {
log.Fatal(err)
}
if i%3 == 0 {
_, err := db.Exec(`
UPDATE repack_issue_70
SET hash = NULL
WHERE hash = ?
`, hash)
if err != nil {
log.Fatal(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment