Skip to content

Commit

Permalink
untrack and retrack tables on update
Browse files Browse the repository at this point in the history
  • Loading branch information
francojreyes committed Mar 31, 2024
1 parent cb47dc4 commit 041e9d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 14 additions & 0 deletions app/helpers/hasura.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,17 @@ def track_table(table_name: str):
"type": "bulk",
"args": infer_relationships(table_name)
})


def untrack_table(table_name: str):
send_hasura_api_query({
"type": "pg_untrack_table",
"args": {
"source": "default",
"cascade": True,
"table": {
"schema": "public",
"name": table_name
}
}
})
26 changes: 19 additions & 7 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from enum import Enum
from typing import Any, Literal, Optional

import psycopg2
Expand All @@ -10,7 +11,7 @@
from pydantic import BaseModel, Field

from helpers.auth import validate_api_key
from helpers.hasura import track_table
from helpers.hasura import track_table, untrack_table


class Metadata(BaseModel):
Expand All @@ -24,6 +25,12 @@ class Metadata(BaseModel):
dryrun: bool = Field(False, description='if true, does not commit changes - useful for testing')


class CreateTableResult(Enum):
NONE = 0
UPDATED = 1
CREATED = 2


conn: connection = None
cur: cursor = None

Expand Down Expand Up @@ -53,7 +60,7 @@ class Metadata(BaseModel):
exit(1)


def create_table(metadata: Metadata) -> bool:
def create_table(metadata: Metadata) -> CreateTableResult:
"""
Create table as specified in metadata.
Expand All @@ -79,8 +86,10 @@ def create_table(metadata: Metadata) -> bool:
cmd = r"INSERT INTO Tables(table_name, up, down) VALUES (%s, %s, %s)"
cur.execute(cmd, (metadata.table_name, metadata.sql_up, metadata.sql_down))

return True
return CreateTableResult.CREATED
elif table_sql[0] != metadata.sql_up:
untrack_table(metadata.table_name)

# Re-create
cur.execute(table_sql[1]) # old sql_down
cur.execute(metadata.sql_up)
Expand All @@ -89,9 +98,9 @@ def create_table(metadata: Metadata) -> bool:
cmd = r"UPDATE Tables SET up = %s, down = %s WHERE table_name = %s"
cur.execute(cmd, (metadata.sql_up, metadata.sql_down, metadata.table_name))

return True
return CreateTableResult.UPDATED

return False
return CreateTableResult.NONE


def get_primary_key_columns(table_name: str) -> list[str]:
Expand Down Expand Up @@ -144,7 +153,7 @@ def execute_delete(metadata: Metadata, payload: list[Any]):
@app.post("/insert", dependencies=[Depends(validate_api_key)])
def insert(metadata: Metadata, payload: list[Any]):
try:
created = create_table(metadata)
create_table_result = create_table(metadata)
except (Exception, Error) as error:
err_msg = "Error while creating PostgreSQL table: " + str(error)
print(err_msg)
Expand Down Expand Up @@ -172,7 +181,10 @@ def insert(metadata: Metadata, payload: list[Any]):
conn.commit()

# Run Hasura actions - must be done after transaction committed otherwise Hasura won't see the table
if created:
if create_table_result == CreateTableResult.UPDATED:
untrack_table(metadata.table_name.lower())

if create_table_result == CreateTableResult.UPDATED or create_table_result == CreateTableResult.CREATED:
track_table(metadata.table_name.lower())
else:
conn.rollback()
Expand Down

0 comments on commit 041e9d0

Please sign in to comment.