Skip to content

Commit

Permalink
Add workflow to calculate source data schema diff
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Oct 30, 2024
1 parent 56f581c commit 9d5d5d7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/update_source_data_schema_changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Update changelog for Source Data

on:
push:
branches:
- patch/update-source-data-schema-changelog

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Run Bash Script
run: |
cd $GITHUB_WORKSPACE
cp -r schemas/ new_schemas/
git checkout main
cp -r schemas/ old_schemas
output=$(python3 scripts/get_source_data_schema_changelog.py)
echo "$output" > changelog/source_data.md
- name: Commit changes
id: commit_changes
run: |
git add changelog/source_data.md
if git commit -m "Update changelog for Source data"; then
echo "Changes committed."
echo "changes_committed=true" >> $GITHUB_OUTPUT
else
echo "No changes to commit."
echo "changes_committed=false" >> $GITHUB_OUTPUT
fi
- name: Push branch
if: steps.commit_changes.outputs.changes_committed == 'true'
run: |
git push
Empty file added changelog/source_data.md
Empty file.
5 changes: 5 additions & 0 deletions schemas/account_signers_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@
"mode": "NULLABLE",
"name": "ledger_sequence",
"type": "INTEGER"
},
{
"mode": "NULLABLE",
"name": "ledger_sequences",
"type": "INTEGER"
}
]
67 changes: 67 additions & 0 deletions scripts/get_source_data_schema_changelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import json

# Directories containing old and new schemas
old_schemas_dir = "original_schemas"
new_schemas_dir = "new_schemas"

# Check if the directories exist
if not os.path.exists(old_schemas_dir):
print(f"Directory {old_schemas_dir} does not exist.")
exit(1)

if not os.path.exists(new_schemas_dir):
print(f"Directory {new_schemas_dir} does not exist.")
exit(1)

def read_json_file(filepath):
with open(filepath, 'r') as f:
return json.load(f)

new_schemas = [file.replace("_schema.json", "") for file in os.listdir(new_schemas_dir)]
old_schemas = [file.replace("_schema.json", "") for file in os.listdir(old_schemas_dir)]

tables_added = [model for model in new_schemas if model not in old_schemas]
tables_removed = [model for model in old_schemas if model not in new_schemas]
common_tables = [model for model in new_schemas if model in old_schemas]

if tables_added:
print("")
print("## Tables Added:")
print([table for table in tables_added])

if tables_removed:
print("")
print("## Tables Removed:")
print([table for table in tables_removed])

for schema in common_tables:
old_file_path = os.path.join(old_schemas_dir, schema + '_schema.json')
new_file_path = os.path.join(new_schemas_dir, schema + '_schema.json')

new_data = read_json_file(new_file_path)
old_data = read_json_file(old_file_path)

old_dict = {item['name']: item for item in old_data}
new_dict = {item['name']: item for item in new_data}

added = [new_dict[name]['name'] for name in new_dict if name not in old_dict]
deleted = [old_dict[name]['name'] for name in old_dict if name not in new_dict]
type_changed = [
(new_dict[name]['name'], new_dict[name]['type'], old_dict[name]['type']) for name in new_dict
if name in old_dict and new_dict[name]['type'] != old_dict[name]['type']
]

if added or deleted or type_changed:
print("")
print(f"## {schema}")

if added:
print(f'**Added columns:** {[field for field in added]}')

if deleted:
print(f'**Deleted columns:** {[field for field in deleted]}')

if type_changed:
for (field, new_type, old_type) in type_changed:
print(f'Type changed for column {field} from {old_type} to {new_type}')

0 comments on commit 9d5d5d7

Please sign in to comment.