How to check if a database exists?

338 views
Skip to first unread message

Tiago Martins de Alexandre

unread,
Mar 21, 2018, 10:05:47 AM3/21/18
to RavenDB - 2nd generation document database
Hi.

In order to make my application running, I need to programmatically start the Raven server and create all needed databases from my shipped application because I have no administrative access to my customer sites.

The second time my application starts, it must recognize that its databases have been created and just use them.

The code that I'm currently using is this one:

var store = new DocumentStore
            {
                Database = myDatabaseName,
                Urls = new[] {"http://localhost:8080"}
            }.Initialize() as DocumentStore;

            var operation = new GetDatabaseNamesOperation(0, 1000); // 1000 is safe enough for me.
            var databaseNames = store.Maintenance.Server.Send(operation);

            if (databaseNames.Any(_ => _ == store.Database)) return store; // database already exists!

            var createDatabaseOperation = new CreateDatabaseOperation(new DatabaseRecord(store.Database));
            store.Maintenance.Server.Send(createDatabaseOperation);

However, I'm asking here to try to find out if there is a more effective way to do this.

Oren Eini (Ayende Rahien)

unread,
Mar 21, 2018, 10:26:45 AM3/21/18
to ravendb
Yes, see how we do that for the tests. 

This will create a process slaved to you, create the relevant db and give you a dedicated document store instance.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

oru malayali

unread,
Mar 21, 2018, 10:26:52 AM3/21/18
to RavenDB - 2nd generation document database
Hi,

this._documentStore.Maintenance.Server.Send(new GetDatabaseRecordOperation(this._database))

That worked for me. Don't know if there is anything simpler. I would like to know as well.

Lokesh.

Tiago Martins de Alexandre

unread,
Mar 22, 2018, 10:16:37 AM3/22/18
to RavenDB - 2nd generation document database
Hi Oren.
I'm not sure if I explained my issue properly, but
in your TestDriver, you create a database that runs in-memory "--RunInMemory=true",

I want to create a permanent database at customer's site.

The second time my application runs, it needs to check if the database exists to avoid getting (DatabaseAlreadyExistsExcetion).

@oru malayali, suggested to use documentStore.Maintenance.Server.Send(new GetDatabaseRecordOperation(databaseName) that seems to solve this issue. Should we keep this as a final solution?

In 2.5 version we were using: 
public bool DatabaseExists(string databaseName) => SafelyExecuteDatabaseCommandsOperation(() =>
{
   var key = "Raven/Databases/" + databaseName;
   return _store.DatabaseCommands.ForSystemDatabase().Head(key) != null;
});



To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Mar 22, 2018, 10:25:50 AM3/22/18
to ravendb
Yes, you would pass --DataDir=PATH instead, and get the same behaviopr
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages