Automatically create database if it does not exist

728 views
Skip to first unread message

Dmitiry Nagirnyak

unread,
Oct 26, 2009, 10:39:42 PM10/26/09
to nhu...@googlegroups.com
Hi,
 
Just wondering is it is possible to tell NHibernate to create the database if it doesn't exists.
Obviously, SchemaExport should not do it (and it doesn't).
 
So lookead at Dialect but it doesn't look it has any interface to do that.
 
Currently I workaround it by using my own interface where I create command and run "create database" query.
It works ok, but just want to make sure if this is correct approach not to do the work twice if NH already can do that.
 
Cheers.
Dmitriy.

José F. Romaniello

unread,
Oct 27, 2009, 8:15:56 AM10/27/09
to nhu...@googlegroups.com
The problem is that you need have rights to do that

2009/10/26 Dmitiry Nagirnyak <dna...@gmail.com>

Dmitiry Nagirnyak

unread,
Oct 27, 2009, 8:27:54 AM10/27/09
to nhu...@googlegroups.com
Yes. Of course.
But generally speaking you even need rights to generate schema for existing database.

This is not really related to the actual question.

I manually execute create schema script for MsSql using existing connection string (Trusted Conn.) but changing the database to master. With trusted connection there are plenty of privileges.

Just want to make sure this is correct approach (during unit testing at least).

Cheers.

2009/10/27 José F. Romaniello <joserom...@gmail.com>

José A. Salvador Vanaclocha

unread,
Oct 27, 2009, 8:50:33 AM10/27/09
to nhu...@googlegroups.com
El mar, 27-10-2009 a las 23:27 +1100, Dmitiry Nagirnyak escribió:
Yes. Of course.
But generally speaking you even need rights to generate schema for existing database.

Yes, of course, But you do that through the settings you define at nhibernate configuration which stores your credentials to access to explicit database, not the server database.

This is not really related to the actual question.

I manually execute create schema script for MsSql using existing connection string (Trusted Conn.) but changing the database to master. With trusted connection there are plenty of privileges.

Just want to make sure this is correct approach (during unit testing at least).

I think the best way to achieve this would be through a "mock" embedded database like sqllite, don't you?

José F. Romaniello

unread,
Oct 27, 2009, 9:00:42 AM10/27/09
to nhu...@googlegroups.com
I don't agree with @JoséASalvador (my namesake), test in a real world database.

On the other hand, you are right.. but I used to create the database manually. 
What next? Do you want to write some code to install a unit testing instance of the engine?

Also, if you want to run unit tests in a continuos integration server you will get problems. 
Generally speaking you need less rights to execute DDL inside a DB than to create a new database, much less.

Dmitiry Nagirnyak

unread,
Oct 27, 2009, 8:37:10 PM10/27/09
to nhu...@googlegroups.com
Yes, of course, But you do that through the settings you define at nhibernate configuration which stores your credentials to access to explicit database, not the server database.
And I can use those credentials to create database. And yes, the credentials do need privileges for that. No problem with that.
 
 
I think the best way to achieve this would be through a "mock" embedded database like sqllite, don't you?
I have the integration tests that run against all supported databases. In my case they are MsSql and SqlLite. So I want to execute those tests against a real database (which SqlLite is too but can be thought as a mock DB too).
 

José A. Salvador Vanaclocha

unread,
Oct 28, 2009, 4:31:33 AM10/28/09
to nhu...@googlegroups.com

>I have the integration tests that run against all supported databases. In my case they are MsSql and SqlLite. So I >want to execute those tests against a real database (which SqlLite is too but can be thought as a mock DB too).

If you have all the integration tests running against all supported databases to complete your neededs and at the same time you are telling you want execute those tests against a "real" database, I suposse you need that by real database containing data so... I sugesst you to add the necessary integration tests where these reflect your data schema particularities.

If this is not the case I am not understanding you...

Greats.

Dmitiry Nagirnyak

unread,
Oct 28, 2009, 4:46:02 AM10/28/09
to nhu...@googlegroups.com


2009/10/28 José A. Salvador Vanaclocha <joan...@gmail.com>


>I have the integration tests that run against all supported databases. In my case they are MsSql and SqlLite. So I >want to execute those tests against a real database (which SqlLite is too but can be thought as a mock DB too).

If you have all the integration tests running against all supported databases to complete your neededs and at the same time you are telling you want execute those tests against a "real" database,
Saying "real" I do not mean production. I mean not mocked/faked/stubed etc.
 


If this is not the case I am not understanding you...
Sorry if I haven't been clear enough.
I only want to automatically create a database when my integration tests are going to be executed. Much like SchemaExport. So there is no need to manually create database for those tests.
Currently I do it manually somewhere with SchemaExport for each particular database.
But just wondering if there is a better way of doing it, maybe using NH's Dialect or similar instead of hand crafted SQL.

Cheers.

José A. Salvador Vanaclocha

unread,
Oct 28, 2009, 5:07:40 AM10/28/09
to nhu...@googlegroups.com
So sorry but I don't know any better way...

When I have had to create a database inside a unit test I have done it via a sql server specific sentence. AFAIK does not exist anything like "hql sentence" to create the database through nhibernate.

I put the pattern I follow to create the database via nunit test:

At the setup section of the test:

- Create the database
- Populate the database with the necessary data

At the teardown section of the test:

- Remove the database

Greats.

Dmitiry Nagirnyak

unread,
Oct 29, 2009, 9:30:40 PM10/29/09
to nhu...@googlegroups.com
Yeah. I see what you mean here. And I went the same way.
So I suppose at this stage it should be alright to do it using custom SQL from create database.
 
Thanks a lot for your time spent for this question.
Cheers.
2009/10/28 José A. Salvador Vanaclocha <joan...@gmail.com>
So sorry but I don't know any better way...
Reply all
Reply to author
Forward
0 new messages