Created
February 9, 2016 09:35
-
-
Save vmihailenco/2ab6aaa5b8143c4b1b67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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