Skip to content

Instantly share code, notes, and snippets.

@bvenkatr
Created July 24, 2014 06:29
Show Gist options
  • Save bvenkatr/d946f8bb3e55147c0184 to your computer and use it in GitHub Desktop.
Save bvenkatr/d946f8bb3e55147c0184 to your computer and use it in GitHub Desktop.
// this script will copy all AQL user functions from the _system database of an ArangoDB server
// into all other databases of the server. Database names and authentication credentials must
// be specified in the script in the "databases" variable
//
// invoke the script from ArangoShell like this:
// require("internal").load("copy-aqlfunctions.js");
(function () {
var db = require("org/arangodb").db;
var print = require("internal").print;
var aqlfunctions = require("org/arangodb/aql/functions");
// databases that we care about. _system database must be first!!!
// if you don't want to list all databases here, use db._listDatabases()
// but this will only work if authentication is either turned off or all
// databases have the same user and passwd credentials
var databases = [
{ name: "_system", user: "root", passwd: "" },
{ name: "test1", user: "root", passwd: "" },
{ name: "test2", user: "root", passwd: "" }
];
var functions;
// get _system database
var system = databases.shift();
// extract all aql functions from _system database
try {
arango.reconnect(arango.getEndpoint(), system.name, system.user, system.passwd);
db._useDatabase(system.name);
functions = db._aqlfunctions.toArray();
}
catch (err1) {
throw "cannot fetch aql functions from _system database: " + String(err1);
}
try {
databases.forEach(function (d) {
arango.reconnect(arango.getEndpoint(), d.name, d.user, d.passwd);
db._useDatabase(d.name);
// unregister all existing aql functions in the target database
// comment the following forEach if you don't want this!
print("- unregistering existing aql functions");
aqlfunctions.toArray().forEach(function(func) {
aqlfunctions.unregister(func.name);
});
// re-register all aql functions that we found in system database
// in the target database
print("- copying existing aql functions into database: " + functions.map(function (func) { return func.name; }).join(", "));
functions.forEach(function(func) {
aqlfunctions.register(func.name, func.code);
});
print();
});
}
catch (err2) {
throw "error occurred when copying functions: " + String(err2);
}
// finally re-connect to _system database
arango.reconnect(arango.getEndpoint(), system.name, system.user, system.passwd);
db._useDatabase(system.name);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment