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

chore(supabase): enable realtime for the file_objects table #985

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Update the file_objects table to add an updated_at column
ALTER TABLE file_objects ADD COLUMN updated_at timestamp DEFAULT timezone('utc', now()) NOT NULL;

-- Create a trigger to automatically update the updated_at column
CREATE TRIGGER update_file_objects_modtime
BEFORE UPDATE ON file_objects
FOR EACH ROW
EXECUTE FUNCTION update_modified_column();

-- Enable Supabase realtime for the file_objects table
alter publication supabase_realtime
add table file_objects;
76 changes: 76 additions & 0 deletions tests/e2e/test_supabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,79 @@ def run_postgres_db_changes():
assert True
except Exception:
assert False


def test_supabase_realtime_file_objects():
class TestCompleteException(Exception):
pass

def timeout_handler():
print("Test timed out after 10 seconds")
_thread.interrupt_main()

async def create_file_object():
client: AsyncClient = await acreate_client(
supabase_key=ANON_KEY,
supabase_url="https://supabase-kong.uds.dev",
)
await client.auth.set_session(access_token=access_token, refresh_token="dummy")

empty_file_object = FileObject(
id="",
bytes=0,
created_at=0,
filename="test_file.txt",
object="file",
purpose="assistants",
status="uploaded",
status_details=None,
)

file_object = await CRUDFileObject(client).create(object_=empty_file_object)
assert file_object is not None, "Failed to create file object"

def file_objects_callback(payload):
expected_record = {
"object": "file",
"status": "uploaded",
"filename": "test_file.txt",
}

all_records_match = all(
payload.get("record", {}).get(key) == value
for key, value in expected_record.items()
)
event_information_match = (
payload.get("table") == "file_objects" and payload.get("type") == "INSERT"
)

if event_information_match and all_records_match:
raise TestCompleteException("Test completed successfully")

def run_create_file_object():
asyncio.run(create_file_object())

timeout_timer = None
try:
random_name = str(uuid.uuid4())
access_token = create_test_user(email=f"{random_name}@fake.com")

threading.Timer(5.0, run_create_file_object).start()

timeout_timer = threading.Timer(10.0, timeout_handler)
timeout_timer.start()

URL = f"wss://supabase-kong.uds.dev/realtime/v1/websocket?apikey={SERVICE_KEY}&vsn=1.0.0"
s = Socket(URL)
s.connect()

channel = s.set_channel("realtime:public:file_objects")
channel.join().on("*", file_objects_callback)

s.listen()
except TestCompleteException:
if timeout_timer is not None:
timeout_timer.cancel()
assert True
except Exception:
assert False