Repository navigation
feat(codegen): suffix generated query constants with SQL - #4704
Open
robsonpeixoto wants to merge 2 commits into
Open
robsonpeixoto wants to merge 2 commits into
robsonpeixoto wants to merge 2 commits into
Conversation
Name the constant holding each query's text <name>SQL (or <Name>SQL with emit_exported_queries), so it reads as SQL at the call site and no longer collides with an enum or struct of the query's name. The suffix also keeps the name clear of Go keywords, so it no longer needs escaping. The duplicate_go_names/query_constant case expected that collision as an error; it now generates, and pins down that the names stay apart.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The Go generator now ends every query-text constant with
SQL, so a call site shows that the value is the query text.// internal/codegen/golang/result.go if options.EmitExportedQueries { - constantName = sdk.Title(query.Name) + constantName = sdk.Title(query.Name) + "SQL" } else { - constantName = escape(sdk.LowerTitle(query.Name)) + constantName = sdk.LowerTitle(query.Name) + "SQL" }database/sql, pgx v4/v5, batch, prepared statements) because all of them useConstantName.escapeis not needed anymore. No Go keyword ends inSQL, so a query namedSelectnow givesselectSQLand notselect_.emit_exported_queries, the constantFoocollided with an enum or struct namedFoo. The suffix removes that collision. Theduplicate_go_names/query_constantcase expected this collision as an error, so it is now a passing case that keeps the two names separate.internal/endtoend/testdata/, theexamples/module and the samples indocs/howto/are regenerated.docs/reference/changelog.mdis not changed.Evidence
const getAuthor = ..., andduplicate_go_names/query_constantexpectsquery constant name conflicts with enum name: Foo.After: with PostgreSQL and MySQL running,
go test ./...passes in thebaseandmanaged-dbcontexts.go build ./...passes underinternal/endtoend/testdata, andgo vet --tags=examples ./...passes underexamples/.corecontext (SQLC_TEST_CORE=1) fails on the same 293 subtests before and after this change, so nothing new fails there.Merge Danger
Door: two-way
Revert the commit to undo the change. Users get the old names back the next time they run
sqlc generate.Blast Radius: generated-code
Every Go user gets a diff in each
query.sql.gothe next time they runsqlc generate, andsqlc diffin CI fails until they regenerate. Unexported constants are only visible inside the generated package. Withemit_exported_queries: true, the constants are public API, so code in other packages that usesqueries.GetAuthorwill not compile until it changes toqueries.GetAuthorSQL. Mention this in the release notes.