jet icon indicating copy to clipboard operation
jet copied to clipboard

Support for dynamic schema names

Open mickeyreiss opened this issue 4 years ago • 4 comments

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

mickeyreiss avatar May 24 '21 02:05 mickeyreiss

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.

go-jet avatar May 25 '21 16:05 go-jet

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.

mickeyreiss avatar May 25 '21 23:05 mickeyreiss

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?

gkdr avatar Jun 21 '21 16:06 gkdr

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.

go-jet avatar Jun 24 '21 10:06 go-jet

@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.

realbucksavage avatar Nov 30 '22 16:11 realbucksavage

It should now be possible to use table.UseSchema and view.UseSchema since #194 was merged.

@go-jet can this be closed?

realbucksavage avatar Dec 08 '22 19:12 realbucksavage

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.

go-jet avatar Dec 09 '22 16:12 go-jet

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?

georgespalding avatar Mar 23 '23 00:03 georgespalding

Next release should be in April. You can already use this feature. It is merged to master.

go-jet avatar Mar 24 '23 18:03 go-jet

Support added in v2.10.0 release - wiki

go-jet avatar Apr 17 '23 10:04 go-jet