Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script to migrate schemas between registries #84

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions scripts/migrate-schemas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

API_TOKEN="YOUR_API_TOKEN"

SOURCE_REGISTRY="https://schemas.holaplex.tools"
DEST_REGISTRY="https://schemas.holaplex.com"


# Define the list of schemas to be migrated
SCHEMAS=("google%2Fprotobuf%2Ftimestamp.proto" "credential" "credits" "credits_mpsc" "customer" "nfts" "organization" "solana_nfts" "timestamp" "treasury" "webhook" "analytics" "polygon_nfts")

fetch_schema() {
local schema=$1
local version=$2

echo "Fetching version $version of schema $schema from source registry" >&2

local schema_data
schema_data=$(curl -s "$SOURCE_REGISTRY/subjects/$schema/versions/$version")

if ! echo "$schema_data" | jq -e . > /dev/null 2>&1; then
echo "Error: Fetched data is not valid JSON." >&2
echo "Fetched Data: $schema_data" >&2
exit 1
fi

echo "$schema_data"
}

schema_version_exists() {
local schema=$1
local version=$2

# Try to fetch the schema version from the given registry
response=$(curl -s "$DEST_REGISTRY/subjects/$schema/versions/$version")

# If the schema version exists, we'd get a JSON response with a non-null "id" field
if echo "$response" | jq -e '.id' > /dev/null 2>&1; then
return 0
else
return 1 # doesn't exist
fi
}

publish_schema() {
local schema=$1
local version=$2
local data=$3

local schemaType
local schemaContent
local references

schemaType=$(echo "$data" | jq -r '.schemaType')
schemaContent=$(echo "$data" | jq '.schema')
references=$(echo "$data" | jq '.references')

# Construct the payload based on the presence of references
if [[ "$references" != "null" ]]; then
schema_payload="{\"schemaType\": \"$schemaType\", \"schema\": $schemaContent, \"references\": $references}"
else
schema_payload="{\"schemaType\": \"$schemaType\", \"schema\": $schemaContent}"
fi

if [[ -z "$schema_payload" ]]; then
echo "Error reconstructing the payload."
exit 1
fi

echo "Payload: $schema_payload"

response=$(curl -s -X POST "$DEST_REGISTRY/subjects/$schema/versions" \
-H "Authorization: $API_TOKEN" \
-H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d "$schema_payload")

# Check if the POST request was successful
if [[ $(echo "$response" | jq -r '.id') == "null" ]]; then
echo "Error publishing version $version of schema $schema to destination."
echo "Response: $response"
exit 1
fi

echo "Successfully migrated version $version of schema $schema"
}

for schema in "${SCHEMAS[@]}"; do
versions=$(curl -s "$SOURCE_REGISTRY/subjects/$schema/versions" | jq '.[]')
for version in $versions; do
if schema_version_exists "$schema" "$version"; then
echo "Schema $schema version $version already exists in destination registry. Skipping."
continue
fi

schema_data=$(fetch_schema "$schema" "$version")
echo "Printing schema data"
echo "$schema_data" | jq
publish_schema "$schema" "$version" "$schema_data"
done
done

echo "Migration completed successfully!"