-
Notifications
You must be signed in to change notification settings - Fork 347
/
migrateToV3.js
149 lines (139 loc) · 4.67 KB
/
migrateToV3.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var Firebase = require('firebase');
var RSVP = require('rsvp');
var args = process.argv
function usage() {
console.log("USAGE: " + args[0] + " " + args[1] + " [--firebase-secret <firebase-secret>] --in-place <geofire-reference> ");
console.log(" " + args[0] + " " + args[1] + " [--firebase-secret <firebase-secret>] <old-reference> <new-reference>")
console.log("WARNING: --in-place deletes old references");
console.log("The Firebase secret is optional. By passing the secret you can override any security rules present");
process.exit(1);
}
function setWithPromise(ref, value, priority) {
return new RSVP.Promise(function(resolve, reject) {
if (priority) {
ref.setWithPriority(value, priority, function(error) {
if (error) {
reject(error);
} else {
resolve();
}
});
} else {
ref.set(value, function(error) {
if (error) {
reject(error);
} else {
resolve();
}
});
}
});
}
function runMigration(fromFirebase, toFirebase, inPlace) {
console.log("Loading old data...");
fromFirebase.child('i').once('value', function(snapshot) {
console.log('Received old data, processing (this may take a while)...');
var error = false;
var promises = [];
if (snapshot.val() === null) {
console.log("Not a valid GeoFire v2 reference: " + fromFirebase);
process.exit(1);
} else {
snapshot.forEach(function(child) {
var parts = child.name().split(":");
if (parts.length < 2) {
console.log("Error transcribing key " + hashKeyPair + "! Not a valid GeoFire entry!");
error = true;
} else {
var hash = parts[0];
var key = parts.splice(1).join(":");
(function(key, hash) {
var promise = new RSVP.Promise(function(resolve, reject) {
fromFirebase.child('l').child(key).once('value', function(snapshot) {
resolve(snapshot.val());
}, function(error) {
reject(error);
});
}).then(function(value) {
if (value != null) {
var lat = value[0];
var lng = value[1];
if (isNaN(lat) || lat < -90 || lat > 90 || isNaN(lng) || lng < -180 || lng > 180) {
console.log("Error transcribing key " + key + "! Not a valid geolocation: [" + lat + ", " + lng + "]");
error = true;
} else {
return new setWithPromise(toFirebase.child(key), { "g": hash, "l": [lat, lng] }, hash);
}
} else {
console.log("Key was removed from GeoFire while migrating: " + key);
}
});
promises.push(promise);
})(key, hash);
}
});
RSVP.all(promises).then(function(posts) {
if (error) {
console.log("There were errors migrating GeoFire, please check your data and the result manually");
process.exit(1);
} else {
console.log("Migrated " + promises.length + " keys successfully!");
if (inPlace) {
console.log("Deleting old keys");
return RSVP.all([
setWithPromise(fromFirebase.child('l'), null),
setWithPromise(fromFirebase.child('i'), null),
]);
}
}
}).then(function() {
console.log("All done...");
process.exit(0);
}).catch(function(reason) {
console.log("There was an error running the migration: " + reason);
process.exit(1);
});
}
}, function(error) {
console.log("There was an error getting the old GeoFire data: " + error);
});
}
/***** Parse Arguments ******/
var progArgs = args.slice(2);
var secretIndex = progArgs.indexOf("--firebase-secret");
var firebaseSecret;
if (secretIndex !== -1) {
firebaseSecret = progArgs[secretIndex+1];
progArgs.splice(secretIndex, 2);
} else {
firebaseSecret == null;
}
if (progArgs.length !== 2) {
usage();
}
var inPlace;
var fromFirebase;
var toFirebase;
if (progArgs[0] === "--in-place") {
inPlace = true;
fromFirebase = new Firebase(progArgs[1]);
toFirebase = new Firebase(progArgs[1]);
} else {
inPlace = false;
fromFirebase = new Firebase(progArgs[0]);
toFirebase = new Firebase(progArgs[1]);
}
if (firebaseSecret) {
console.log("Authenticating...");
toFirebase.auth(firebaseSecret, function() {
//success
console.log("Authentication successful.");
runMigration(fromFirebase, toFirebase, inPlace);
}, function() {
//failure
console.log("Error authenticating...");
process.exit(1);
});
} else {
runMigration(fromFirebase, toFirebase, inPlace);
}