Skip to content

Instantly share code, notes, and snippets.

@coco98
Last active September 16, 2021 19:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coco98/2a65f9fe8c1c0f5e4bee92d8cb66bc4c to your computer and use it in GitHub Desktop.
Save coco98/2a65f9fe8c1c0f5e4bee92d8cb66bc4c to your computer and use it in GitHub Desktop.
Track tables in python (Hasura)
import requests
#Fetch existing tables
tables = requests.post('http://localhost:8080/v1/query', json={
"type":"select",
"args":{
"table": {"schema": "information_schema", "name": "tables"},
"columns": ["table_name"],
"where": {"table_schema": {"$eq": "public"}}
}
}).json()
#Fetch tracked tables
tracked_tables = requests.post('http://localhost:8080/v1/query', json={
"type":"select",
"args":{
"table": {"schema": "hdb_catalog", "name": "hdb_table"},
"columns": ["table_name"],
"where": {"table_schema": {"$eq": "public"}}
}
}).json()
tracked_table_names = [t['table_name'] for t in tracked_tables]
# Now track these tables one by one
for t in tables:
table_name = t['table_name']
#Skip an already tracked table
if (table_name in tracked_table_names):
print ("Skipping. Table alread tracked: " + table_name)
else:
print(table_name)
response = requests.post('http://localhost:8080/v1/query', json={
"type": "track_table",
"args": {
"name": table_name,
"schema": "public"
}
})
print (response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment