jet
jet copied to clipboard
Support for dynamic schema names
At the moment, the schema name is baked into the generated code. It would be helpful if the schema name could be globally overridden, so that a compiled program could be configured to hit different databases in different environments without changing code (or adding FromSchema to all queries).
What would be the use case? To set a default schema name during server startup and then during the server lifespan only that schema will be used? If thats the case, there is another workaround. It is possible to override generated sql builder table objects with a different table objects(with different schema name). For instance:
import "github.com/go-jet/jet/v2/examples/quick-start/.gen/jetdb/dvds/table"
func serverStartup(schemaName string){
....
table.Artist = table.Artist.FromSchema(schemaName)
table.Album = table.Album.FromSchema(schemaName)
table.Customer = table.Customer.FromSchema(schemaName)
...
}
Because these table objects are global variables there is no need to change a code or add FromSchema to all queries.
The use case is different schema names in dev, staging, production. Yes, in my case, the schema name is provided to the application at startup via environment variables.
This workaround seems like it will work, albeit still requiring one extra line of code when a new table is added.
funny enough, we've had our own fork with such a functionality hacked into the NewTable() function: it reads an env variable and checks if there is an alias for the current schemaname, see https://github.com/unwired/jet/blob/5426d04726da9c209fce94ce8f925d9591415e4c/mysql/table.go#L88
that was from before the schema alias functionality from the newest release (afaik), so i believe there was no other way at that point in time :slightly_smiling_face: would you consider such a functionality in any form, or is the workaround you provided the "official" way now?
Yes, FromSchema method was added to the latest release. In the feature, new auto generated global function might be added, to automate schema rename of all global table objects.
Something like: table.UseSchema(schemaName) to replace above serverStartup function.
@mickeyreiss @go-jet I can think of a possibility to generate a function with the table SQL files to call UsingSchema on l variables. This method could live in the first generated file.
It should now be possible to use table.UseSchema and view.UseSchema since #194 was merged.
@go-jet can this be closed?
Either is fine. I guess it can be left open for now, until the official release tag. Just in case someone else is in search of this functionality.
I am eager to use the functionality added with https://github.com/go-jet/jet/pull/194 (in the next release?).
Do you have a plan for when the next release will be?
Next release should be in April. You can already use this feature. It is merged to master.
Support added in v2.10.0 release - wiki