-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SchemaUtils.createDatabase should work without database connection (+other improvements) #795
Comments
@AdityaAnand1 Thanks for suggesting these improvements. I will create a PR to provide :
Here is an example: fun main() {
//Important is that you connect to the instance, not a specific database initially
Database.connect("jdbc:sqlserver://localhost:1433;integratedSecurity=true", "com.microsoft.sqlserver.jdbc.SQLServerDriver" )
transaction {
SchemaUtils.createDatabase("dbname", autoConnect=true)
}
} => createDatabase will check if the database exists. Then create it if the result is false. Finally, connect to it. @AdityaAnand1 is this exactly what you need ? |
Yes, exactly. As a policy, I always want to default to creating a database on a server if it does not yet exist. This should do it 😁 |
@hichem-fazai , I'm not sure that it's possible to change a scheme within the current transaction. I guess that the more complex approach to work with scheme/databases should be provided. It should cover other cases like #145 , #254. |
@Tapac you can connect to one database and then change to another, but my comment was to give solution when you don't even know if the initial database exists or not.
|
I guess it's not possible to change a scheme(at least for mssql) as it is mentioned here. |
To support schema in a proper way I propose to add an optional field ( object table: Table("table") {
val id = integer("id")
override val schema = "myschema"
} |
@AdityaAnand1 , I just want to say that options like I think that something like a code below looks more predictable (but more challenging in implementation): val newSchema : Schema = SchemaUtils.createSchema("dbname")
transaction.selectSchema(newSchema) or val schemaToConnect = Schema("dbname")
Database.connect("url", schema = schemaToConnect) It will allow improving code where you need to make cross-joins from different schemas or make selects to other schemas from a current connection: tableA.innerJoin(tableB.fromScheme("dbname"))
tableB.fromScheme("dbname").insert {} Functions' names are just examples. |
That's why simple support of schemes in queries should be enough.
Using strings is not a very good idea as it limits you to use extension functions for schemes (if you want to). Also, the field on an original table could not be changed, so I think new class should be introduced.
It just an example and there could be a lot of pitfalls in implementation by I hope you catch the idea. |
Here's the use case:
2.1 If it exists, go ahead and connect to it
2.2 If it does not exists, create it and then connect to it
This would be essential in building a resilient system which would allow spinning up new environments quickly with no manual processes involved. I'm picturing spinning up dev/staging/prod environments with auto-created databases, as well as automatically creating feature-based temporary environments for QA approval testing via CI/CD flows.
So,
SchemaUtils.createDatabase
should work in the same "level" asDatabase.connect()
to allow this. That is, I first want to check for the existence of a database, create it if it doesn't exist, and then connect to it.So, perhaps an alternate version of
createDatabase
that tries to establish a connection to the SQL server all on it's own could be used to accomplish this. Or perhaps extendDatabase.connect()
with a flagcreateDatabaseIfNotExists = true
.Another Util method
SchemaUtils.doesDatabaseExist(): Boolean
would be helpful as well.cc @hichem-fazai
The text was updated successfully, but these errors were encountered: