Skip to content

Commit 3517537

Browse files
Create dgraph_schema_converter.sh
Script to convert Schema returned by schema{} query into text format used by alter endpoint.
1 parent ecfcc6a commit 3517537

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Convert Dgraph JSON schema to text schema format
4+
# Usage: ./convert_schema.sh input.json > schema.dql
5+
6+
# JSON format is returned by `schema{}` query.
7+
# curl -X POST -H "Content-Type: application/dql" \
8+
# localhost:8080/query -d $'schema {}' > schema.json
9+
10+
# Text format is used by the alter command to update the schema.
11+
12+
13+
if [ $# -eq 0 ]; then
14+
echo "Usage: $0 <json_file>" >&2
15+
exit 1
16+
fi
17+
18+
json_file="$1"
19+
20+
if [ ! -f "$json_file" ]; then
21+
echo "Error: File '$json_file' not found" >&2
22+
exit 1
23+
fi
24+
25+
# Process predicates (schema)
26+
echo "# Predicates"
27+
jq -r '.data.schema[] |
28+
"<" + .predicate + ">: " + .type + " " +
29+
(if .index then
30+
"@index(" + (.tokenizer | join(",")) + ")"
31+
else "" end) + " " +
32+
(if .reverse then "@reverse" else "" end) + " " +
33+
(if .upsert then "@upsert" else "" end) + " " +
34+
(if .unique then "@unique" else "" end) + " " +
35+
(if .list then "[" + .type + "]" else "" end) + " " +
36+
" ." |
37+
gsub(" +"; " ") |
38+
gsub(" \\."; ".")' "$json_file"
39+
40+
echo ""
41+
42+
# Process types
43+
jq -r '.data.types[] |
44+
"type <" + .name + "> {\n" +
45+
(.fields | map("\t" + .name) | join("\n")) +
46+
"\n}"' "$json_file"

0 commit comments

Comments
 (0)