private void migrateDatabase() {
final GraphDatabaseService graphDb = getService(NodeService.class).getGraphDb();
final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
final NodeFactory nodeFactory = new NodeFactory(superUserContext);
final RelationshipFactory relFactory = new RelationshipFactory(superUserContext);
final App app = StructrApp.getInstance();
final StringProperty uuidProperty = new StringProperty("uuid");
final int txLimit = 2000;
boolean hasChanges = true;
int actualNodeCount = 0;
int actualRelCount = 0;
logger.log(Level.INFO, "Migration of ID properties from uuid to id requested.");
while (hasChanges) {
hasChanges = false;
try (final Tx tx = app.tx(false, false)) {
// iterate over all nodes,
final Iterator<Node> allNodes = GlobalGraphOperations.at(graphDb).getAllNodes().iterator();
while (allNodes.hasNext()) {
final Node node = allNodes.next();
// do migration of our own ID properties (and only our own!)
if (node.hasProperty("uuid") && node.getProperty("uuid") instanceof String && !node.hasProperty("id")) {
try {
final NodeInterface nodeInterface = nodeFactory.instantiate(node);
final String uuid = nodeInterface.getProperty(uuidProperty);
if (uuid != null) {
nodeInterface.setProperty(GraphObject.id, uuid);
nodeInterface.removeProperty(uuidProperty);
actualNodeCount++;
hasChanges = true;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
// break out of loop to allow transaction to commit
if (hasChanges && (actualNodeCount % txLimit) == 0) {
break;
}
}
tx.success();
} catch (Throwable t) {
t.printStackTrace();
}
logger.log(Level.INFO, "Migrated {0} nodes so far.", actualNodeCount);
}
logger.log(Level.INFO, "Migrated {0} nodes to new ID property.", actualNodeCount);
// iterate over all relationships
hasChanges = true;
while (hasChanges) {
hasChanges = false;
try (final Tx tx = app.tx(false, false)) {
final Iterator<Relationship> allRels = GlobalGraphOperations.at(graphDb).getAllRelationships().iterator();
while (allRels.hasNext()) {
final Relationship rel = allRels.next();
// do migration of our own ID properties (and only our own!)
if (rel.hasProperty("uuid") && rel.getProperty("uuid") instanceof String && !rel.hasProperty("id")) {
try {
final RelationshipInterface relInterface = relFactory.instantiate(rel);
final String uuid = relInterface.getProperty(uuidProperty);
if (uuid != null) {
relInterface.setProperty(GraphObject.id, uuid);
relInterface.removeProperty(uuidProperty);