From 8d8dfb06401e80c9888fbd2d84618311ba09e8d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Wed, 16 Jan 2019 01:50:01 +0100 Subject: [PATCH] Provision neo4j container with import script --- .dockerignore | 2 + docker-compose.override.yml | 4 +- docker-compose.yml | 3 +- Dockerfile.neo4j => neo4j/Dockerfile | 1 + neo4j/import.cql | 75 ++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) rename Dockerfile.neo4j => neo4j/Dockerfile (88%) create mode 100644 neo4j/import.cql diff --git a/.dockerignore b/.dockerignore index 6b6b2193f..f5a08be39 100644 --- a/.dockerignore +++ b/.dockerignore @@ -15,4 +15,6 @@ kubernetes/ node_modules/ scripts/ dist/ + db-migration-worker/ +neo4j/ diff --git a/docker-compose.override.yml b/docker-compose.override.yml index f2b3fa0fc..c1ac0569d 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -18,6 +18,8 @@ services: - 7474:7474 environment: - NEO4J_apoc_import_file_enabled=true + - "NEO4J_USERNAME=${NEO4J_USERNAME}" + - "NEO4J_PASSWORD=${NEO4J_PASSWORD}" db-migration-worker: build: context: db-migration-worker @@ -34,8 +36,6 @@ services: - "MONGODB_PASSWORD=${MONGODB_PASSWORD}" - "MONGODB_AUTH_DB=${MONGODB_AUTH_DB}" - "MONGODB_DATABASE=${MONGODB_DATABASE}" - - "NEO4J_USERNAME=${NEO4J_USERNAME}" - - "NEO4J_PASSWORD=${NEO4J_PASSWORD}" command: "--smallfiles --logpath=/dev/null" volumes: diff --git a/docker-compose.yml b/docker-compose.yml index 5a7650aa1..df8de6b01 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,8 +24,7 @@ services: neo4j: image: humanconnection/neo4j:latest build: - context: . - dockerfile: Dockerfile.neo4j + context: neo4j networks: - hc-network volumes: diff --git a/Dockerfile.neo4j b/neo4j/Dockerfile similarity index 88% rename from Dockerfile.neo4j rename to neo4j/Dockerfile index cb7fd228f..2ef9443a5 100644 --- a/Dockerfile.neo4j +++ b/neo4j/Dockerfile @@ -1,2 +1,3 @@ FROM neo4j:3.5.0 RUN wget https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/3.5.0.1/apoc-3.5.0.1-all.jar -P plugins/ +COPY import.cql . diff --git a/neo4j/import.cql b/neo4j/import.cql new file mode 100644 index 000000000..521cc1221 --- /dev/null +++ b/neo4j/import.cql @@ -0,0 +1,75 @@ +CALL apoc.load.json('file:/mongo-export/categories.json') YIELD value as category +MERGE(c:Category {id: category._id["$oid"]}) +ON CREATE SET c.name = category.title, + c.slug = category.slug, + c.icon = category.icon + + +CALL apoc.load.json('file:/mongo-export/badges.json') YIELD value as badge +MERGE(b:Badge {id: badge._id["$oid"]}) +ON CREATE SET b.key = badge.key, + b.type = badge.type, + b.icon = badge.image.path, + b.status = badge.status + + +CALL apoc.load.json('file:/mongo-export/users.json') YIELD value as user +MERGE(u:User {id: user._id["$oid"]}) +ON CREATE SET u.name = user.name, + u.slug = user.slug, + u.email = user.email, + u.password = user.password, + u.avatar = user.avatar, + u.coverImg = user.coverImg, + u.wasInvited = user.wasInvited, + u.role = apoc.text.toUpperCase(user.role) +WITH u, user, user.badgeIds AS badgeIds +UNWIND badgeIds AS badgeId +MATCH (b:Badge {id: badgeId}) +MERGE (b)-[:REWARDED]->(u) + + + +CALL apoc.load.json('file:/mongo-export/contributions.json') YIELD value as post +MERGE (p:Post {id: post._id["$oid"]}) +ON CREATE SET p.title = post.title, + p.slug = post.slug, + p.image = post.teaserImg, + p.content = post.content, + p.contentExcerpt = post.contentExcerpt, + p.visibility = apoc.text.toUpperCase(post.visibility), + p.createdAt = datetime(post.createdAt["$date"]), + p.updatedAt = datetime(post.updatedAt["$date"]) +WITH p, post, post.tags AS tags, post.categoryIds as categoryIds +UNWIND tags AS tag +UNWIND categoryIds AS categoryId +MATCH (c:Category {id: categoryId}), + (u:User {id: post.userId}) +MERGE (t:Tag {id: apoc.create.uuid(), name: tag}) +MERGE (p)-[:TAGGED]->(t) +MERGE (u)-[:WROTE]->(p) +MERGE (p)-[:CATEGORIZED]->(c) + + +CALL apoc.load.json('file:/mongo-export/comments.json') YIELD value as comment +MERGE (c:Comment {id: comment._id["$oid"]}) +ON CREATE SET c.content = comment.content, + c.contentExcerpt = comment.contentExcerpt, + c.deleted = comment.deleted +WITH comment +MATCH (p:Post {id: comment.contributionId}), + (u:User {id: comment.userId}) +MERGE (c)-[:COMMENTS]->(p) +MERGE (u)-[:WROTE]->(c) + + +CALL apoc.load.json('file:/mongo-export/follows.json') YIELD value as follow +MATCH (u1:User {id: follow.userId}), + (u2:User {id: follow.foreignId}) +MERGE (u1)-[:FOLLOWS]->(u2) + + +CALL apoc.load.json('file:/mongo-export/shouts.json') YIELD value as shout +MATCH (u:User {id: shout.userId}), + (p:Post {id: shout.foreignId}) +MERGE (u)-[:SHOUTED]->(p)