Created
May 16, 2017 19:11
-
-
Save milesrichardson/b30acef8827c53aae0885186cc6078b3 to your computer and use it in GitHub Desktop.
Example bash file with inline python for deploying schemas from JSON files to parse-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P) | |
SCHEMADIR="$DIR/../src/libcommon/dbschema" | |
TARGET_SERVER="http://localhost:9595/api/parse" | |
APPLICATION_ID="" | |
MASTER_KEY="" | |
main() { | |
if test $# -eq 1 ; then | |
deploy_schema "$1" "$SCHEMADIR"/"$1".json | |
shift | |
else | |
deploy_all_schemas | |
fi | |
} | |
deploy_schema() { | |
local class_name="$1" | |
shift | |
local schema_fn="$1" | |
shift | |
if ! test -f "$schema_fn" ; then | |
echo "Error: Schema not found at $schema_fn" | |
exit 1 | |
fi | |
echo "Update $class_name with schema at: $schema_fn" | |
update_class "$class_name" | |
} | |
deploy_all_schemas() { | |
local schema_basename | |
local class_name | |
for schema_fn in "$SCHEMADIR"/*.json | |
do | |
schema_basename=$(basename "$schema_fn") | |
class_name="${schema_basename%%.*}" | |
update_class "$class_name" | |
done | |
} | |
update_class() { | |
local class_name="$1" | |
shift | |
get_existing_schema() { | |
curl -X GET \ | |
-f -s \ | |
-H "X-Parse-Application-Id: $APPLICATION_ID" \ | |
-H "X-Parse-Master-Key: $MASTER_KEY" \ | |
"$TARGET_SERVER"/schemas/"$class_name" | |
} | |
class_exists_on_target() { | |
get_existing_schema >/dev/null 2>&1 \ | |
&& return 0 || return 1 | |
} | |
get_schema_from_file() { | |
cat "$SCHEMADIR/$class_name".json | |
} | |
create_class_on_target() { | |
curl -X POST \ | |
-H "X-Parse-Application-Id: $APPLICATION_ID" \ | |
-H "X-Parse-Master-Key: $MASTER_KEY" \ | |
"$TARGET_SERVER"/schemas/"$class_name" | |
} | |
get_put_data() { | |
get_existing_schema \ | |
| python -m json.tool \ | |
| python -c 'import sys, json; | |
existing_schema = json.load(sys.stdin); | |
with open("'"$SCHEMADIR/$class_name".json'") as schema_fh: | |
full_schema = json.load(schema_fh) | |
removeFields = [] | |
for field in full_schema["fields"]: | |
if field in existing_schema["fields"].keys(): | |
removeFields.append(field) | |
default_fields = ["ACL", "createdAt", "updatedAt", "objectId"] | |
for field in existing_schema["fields"]: | |
if field in default_fields: | |
continue | |
if field not in full_schema["fields"].keys(): | |
full_schema["fields"][field] = { "__op": "Delete" } | |
for field in removeFields: | |
del full_schema["fields"][field] | |
print json.dumps(full_schema) | |
' \ | |
| python -m json.tool | |
} | |
update_class_on_target() { | |
echo "PUT $TARGET_SERVER/schemas/$class_name" | |
get_put_data | |
curl -s -X PUT \ | |
-H "X-Parse-Application-Id: $APPLICATION_ID" \ | |
-H "X-Parse-Master-Key: $MASTER_KEY" \ | |
-H "Content-Type: application/json" \ | |
"$TARGET_SERVER"/schemas/"$class_name" \ | |
--data @- < <(get_put_data) | |
} | |
if ! class_exists_on_target ; then | |
echo "$class_name does not exist on $TARGET_SERVER, create it" | |
create_class_on_target | |
echo | |
fi | |
echo "Update $class_name on $TARGET_SERVER" | |
update_class_on_target \ | |
&& { echo ; echo "Success" ; } \ | |
|| { echo ; echo "Error, see above" ; } | |
echo | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment