dolthub/doltgresql#1863: validate database exists before CREATE SCHEMA#2139
Conversation
Add ValidateCreateSchema analyzer rule that checks if the current database has a valid root before allowing CREATE SCHEMA to proceed. This ensures PostgreSQL-compliant behavior where schemas must be created within an existing database. Adds Go integration test and bats end-to-end tests to verify the following: - Connection to non-existent database fails with error - CREATE SCHEMA succeeds on valid database Refs: dolthub#1863
|
This looks good, thanks for the contribution @codeaucafe! 🙏 Using an analyzer rule for this check is good approach since this is postgres-specific behavior and analyzer rules are our best way to extend each engine with behavior that is specific to either postgres or mysql. In this case, Postgres doesn't seem to ever allow a connection without a selected database (either explicitly specified or implicitly from the connected user name), so should be hard for customers to hit this case. Awesome to have an explicit check for it as a safety measure. |
|
Thank you @fulghum. Btw, yes, you're right, from reading the code I believe the connection-level validation already prevents this in normal usage. I added the analyzer rule mainly as a defensive safety net for any edge cases. Thank you for agreeing, approving, and merging my PR :) |
Summary
Adds a
ValidateCreateSchemaanalyzer rule to ensureCREATE SCHEMAfails that appropriately when executed against a non-existent or invalid database context.CREATE SCHEMAto proceedNote
I wasn't 100% sure if adding an analyzer rule was the right approach here, but it seemed like the cleanest way to validate database context before schema creation. The rule follows the same pattern as existing validation rules like
ValidateCreateTable.The connection-level validation (rejecting connections to non-existent databases) already handles most cases, but this analyzer rule acts as an additional safety net for edge cases where a query might run with an invalid database context (also not sure how realistic this is 😕 ).
Open to feedback on whether there's a better approach!
Testing
bats --filter "non-existent database|CREATE SCHEMA works" testing/bats/doltgres.batsgo test ./testing/go/... -run TestSchemas -vCloses: #1863