This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsdump.go
99 lines (91 loc) · 2.15 KB
/
dsdump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// dsdump.go
//
// Created by Jens-Uwe Mager <jum@anubis.han.de> on 07.01.14.
//
// Simple dump and restore of all records in the appengine datastore.
package dsdump
import (
"appengine"
"appengine/datastore"
"encoding/gob"
"fmt"
"io"
"net/http"
"time"
)
// Set DEBUG to true for lots of debug output
const DEBUG = false
func debug(c appengine.Context, format string, a ...interface{}) {
if DEBUG {
c.Debugf(format, a...)
}
}
func init() {
http.HandleFunc("/admin/dsdump", dsdump)
gob.Register(&time.Time{})
}
type DSRec struct {
Key *datastore.Key
Props datastore.PropertyList
}
func dsdump(w http.ResponseWriter, r *http.Request) {
var err error
c := appengine.NewContext(r)
debug(c, "req url %#v", r.URL)
debug(c, "req header %#v", r.Header)
switch r.Method {
case "GET":
it := datastore.NewQuery("").Run(c)
enc := gob.NewEncoder(w)
w.Header().Set("Content-Type", "application/x-gob")
for {
var rec DSRec
if rec.Key, err = it.Next(&rec.Props); err != nil {
if err != datastore.Done {
c.Errorf("query next: %v", err.Error())
}
break
}
if rec.Key.Kind()[0] == '_' {
// skip over internal datastore management records
continue
}
err = enc.Encode(&rec)
if err != nil {
c.Errorf("encode rec: %v", err.Error())
break
}
}
case "POST":
dec := gob.NewDecoder(r.Body)
num := 0
for {
var rec DSRec
if err = dec.Decode(&rec); err != nil {
if err == io.EOF {
break
}
c.Errorf("decode gob post: %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
debug(c, "req val: %#v", rec)
_, err = datastore.Put(c, rebuildKey(c, rec.Key), &rec.Props)
if err != nil {
c.Errorf("put rec %+v: %v", rec, err.Error())
} else {
num++
}
}
fmt.Fprintf(w, "updated %d records, do not forget to flush memcache\n", num)
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
func rebuildKey(c appengine.Context, key *datastore.Key) *datastore.Key {
if key == nil {
return nil
}
return datastore.NewKey(c, key.Kind(), key.StringID(), key.IntID(), rebuildKey(c, key.Parent()))
}