Skip to content

dolthub/doltgresql#1863: validate database exists before CREATE SCHEMA#2139

Merged
fulghum merged 1 commit intodolthub:mainfrom
codeaucafe:codeaucafe/1863/fix-validate-create-schema-database-context
Dec 22, 2025
Merged

dolthub/doltgresql#1863: validate database exists before CREATE SCHEMA#2139
fulghum merged 1 commit intodolthub:mainfrom
codeaucafe:codeaucafe/1863/fix-validate-create-schema-database-context

Conversation

@codeaucafe
Copy link
Copy Markdown
Contributor

Summary

Adds a ValidateCreateSchema analyzer rule to ensure CREATE SCHEMA fails that appropriately when executed against a non-existent or invalid database context.

  • Adds analyzer rule that checks if the current database has a valid root before allowing CREATE SCHEMA to proceed
  • Ensures PostgreSQL-compliant behavior where schemas must be created within an existing database
  • Adds integration and bats tests

Note

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

  • Integrationt: TestCreateSchemaWithNonExistentDatabase
  • Bats: bats --filter "non-existent database|CREATE SCHEMA works" testing/bats/doltgres.bats
  • Existing schema tests pass: go test ./testing/go/... -run TestSchemas -v

Closes: #1863

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
@codeaucafe codeaucafe changed the title j dolthub/doltgresql#1863: validate database exists before CREATE SCHEMA Dec 22, 2025
@fulghum
Copy link
Copy Markdown
Contributor

fulghum commented Dec 22, 2025

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.

@fulghum fulghum merged commit e0097f8 into dolthub:main Dec 22, 2025
21 checks passed
@codeaucafe
Copy link
Copy Markdown
Contributor Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle PostgreSQL CREATE SCHEMA error when no database is selected

3 participants