-
Notifications
You must be signed in to change notification settings - Fork 847
Run client_connected hook for HTTP SQL requests #4563
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
Open
clockwork-labs-bot
wants to merge
8
commits into
master
Choose a base branch
from
bot/sql-connect-hook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−16
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e00232d
Run client_connected hook for HTTP SQL requests
clockwork-labs-bot 5b082e8
Fix: always call identity_disconnected even if authorize_sql fails
clockwork-labs-bot 84b7286
Move connect hook into sql_direct, update pgwire and tests
clockwork-labs-bot 8283f3e
Add smoketests for SQL connect hook
clockwork-labs-bot 6b081b7
[bot/sql-connect-hook]: review
bfops fec28a8
[bot/sql-connect-hook]: fix tests
bfops 60464e4
[bot/sql-connect-hook]: fix tests
bfops 00b3853
Add smoketest: disconnect called even on SQL query error
clockwork-labs-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [package] | ||
| name = "smoketest-module-sql-connect-hook" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| spacetimedb.workspace = true | ||
| log.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use spacetimedb::{log, ReducerContext, Table}; | ||
|
|
||
| #[spacetimedb::table(accessor = person, public)] | ||
| pub struct Person { | ||
| name: String, | ||
| } | ||
|
|
||
| #[spacetimedb::reducer(init)] | ||
| pub fn init(ctx: &ReducerContext) { | ||
| ctx.db.person().insert(Person { | ||
| name: "Alice".to_string(), | ||
| }); | ||
| } | ||
|
|
||
| #[spacetimedb::reducer(client_connected)] | ||
| pub fn connected(ctx: &ReducerContext) { | ||
| log::info!("sql_connect_hook: client_connected caller={}", ctx.sender()); | ||
| } | ||
|
|
||
| #[spacetimedb::reducer(client_disconnected)] | ||
| pub fn disconnected(ctx: &ReducerContext) { | ||
| log::info!("sql_connect_hook: client_disconnected caller={}", ctx.sender()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use spacetimedb_smoketests::Smoketest; | ||
|
|
||
| /// Test that SQL requests are rejected when client_connected returns an error. | ||
| /// | ||
| /// This verifies that the /sql HTTP endpoint now runs the module's | ||
| /// client_connected reducer and rejects the request if it errors. | ||
| /// Without PR #4563, this SQL query would succeed. | ||
| #[test] | ||
| fn test_sql_rejected_when_client_connected_errors() { | ||
| let test = Smoketest::builder() | ||
| .precompiled_module("client-connection-reject") | ||
| .build(); | ||
|
|
||
| // SQL should fail because client_connected returns an error | ||
| let result = test.sql("SELECT * FROM all_u8s"); | ||
| assert!( | ||
| result.is_err(), | ||
| "Expected SQL query to be rejected when client_connected errors, but it succeeded" | ||
| ); | ||
| } | ||
bfops marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Test that SQL requests trigger client_connected and client_disconnected hooks. | ||
| /// | ||
| /// This verifies that the /sql HTTP endpoint calls the module's lifecycle | ||
| /// reducers. Without PR #4563, no connect/disconnect logs would appear. | ||
| #[test] | ||
| fn test_sql_triggers_connect_disconnect_hooks() { | ||
| let test = Smoketest::builder().precompiled_module("sql-connect-hook").build(); | ||
|
|
||
| // Run a SQL query | ||
| test.sql("SELECT * FROM person").unwrap(); | ||
|
|
||
| // Check that both connect and disconnect hooks were called | ||
| let logs = test.logs(100).unwrap(); | ||
| assert!( | ||
| logs.iter().any(|l| l.contains("sql_connect_hook: client_connected")), | ||
| "Expected client_connected log from SQL request, got: {:?}", | ||
| logs | ||
| ); | ||
| assert!( | ||
| logs.iter().any(|l| l.contains("sql_connect_hook: client_disconnected")), | ||
| "Expected client_disconnected log from SQL request, got: {:?}", | ||
| logs | ||
| ); | ||
| } | ||
|
|
||
| /// Test that SQL queries still return data when client_connected accepts. | ||
| /// | ||
| /// Ensures the connect hook doesn't break normal SQL functionality. | ||
| #[test] | ||
| fn test_sql_returns_data_with_connect_hook() { | ||
| let test = Smoketest::builder().precompiled_module("sql-connect-hook").build(); | ||
|
|
||
| test.assert_sql( | ||
| "SELECT * FROM person", | ||
| r#" name | ||
| --------- | ||
| "Alice""#, | ||
| ); | ||
| } | ||
|
|
||
| /// Test that client_disconnected is still called even when the SQL query fails. | ||
| /// | ||
| /// The `authorize_sql` and `exec_sql` errors are captured inside an async block, | ||
| /// so `call_identity_disconnected` runs regardless of query success or failure. | ||
| #[test] | ||
| fn test_sql_disconnect_called_on_query_error() { | ||
| let test = Smoketest::builder().precompiled_module("sql-connect-hook").build(); | ||
|
|
||
| // Run an invalid SQL query — this will fail in exec_sql | ||
| let result = test.sql("SELECT * FROM nonexistent_table"); | ||
| assert!(result.is_err(), "Expected invalid SQL to fail"); | ||
|
|
||
| // Despite the query error, both connect and disconnect should have been called | ||
| let logs = test.logs(100).unwrap(); | ||
| assert!( | ||
| logs.iter().any(|l| l.contains("sql_connect_hook: client_connected")), | ||
| "Expected client_connected even on failed SQL, got: {:?}", | ||
| logs | ||
| ); | ||
| assert!( | ||
| logs.iter().any(|l| l.contains("sql_connect_hook: client_disconnected")), | ||
| "Expected client_disconnected even on failed SQL, got: {:?}", | ||
| logs | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.