Exemple #1
0
        ///<summary>
        /// Ensures that the database exists, creating it if needed
        ///</summary>
        public static async Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false)
        {
            var serverClient = self.ForSystemDatabase() as AsyncServerClient;

            if (serverClient == null)
            {
                throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self);
            }

            serverClient.ForceReadFromMaster();

            var doc   = MultiDatabase.CreateDatabaseDocument(name);
            var docId = "Raven/Databases/" + name;

            try
            {
                if (await serverClient.GetAsync(docId) != null)
                {
                    return;
                }

                var req = serverClient.CreateRequest("/admin/databases/" + Uri.EscapeDataString(name), "PUT");
                await req.ExecuteWriteAsync(doc.ToString(Formatting.Indented));
            }
            catch (Exception)
            {
                if (ignoreFailures == false)
                {
                    throw;
                }
            }

            try
            {
                await new RavenDocumentsByEntityName().ExecuteAsync(serverClient.ForDatabase(name), new DocumentConvention());
            }
            catch (Exception)
            {
                // we really don't care if this fails, and it might, if the user doesn't have permissions on the new db
            }
        }
Exemple #2
0
        public static Task CreateDatabaseAsync(this IAsyncDatabaseCommands self, DatabaseDocument databaseDocument, bool ignoreFailures = false)
        {
            var serverClient = self.ForSystemDatabase() as AsyncServerClient;

            if (serverClient == null)
            {
                throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self);
            }

            if (databaseDocument.Settings.ContainsKey("Raven/DataDir") == false)
            {
                throw new InvalidOperationException("The Raven/DataDir setting is mandatory");
            }

            var doc = RavenJObject.FromObject(databaseDocument);

            doc.Remove("Id");

            var req = serverClient.CreateRequest("/admin/databases/" + Uri.EscapeDataString(databaseDocument.Id), "PUT");

            return(req.ExecuteWriteAsync(doc.ToString(Formatting.Indented)));
        }
        ///<summary>
        /// Ensures that the database exists, creating it if needed
        ///</summary>
        public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false)
        {
            var serverClient = self.ForSystemDatabase() as AsyncServerClient;

            if (serverClient == null)
            {
                throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self);
            }

            var doc   = MultiDatabase.CreateDatabaseDocument(name);
            var docId = "Raven/Databases/" + name;

            serverClient.ForceReadFromMaster();

            return(serverClient.GetAsync(docId)
                   .ContinueWith(get =>
            {
                if (get.Result != null)
                {
                    return get;
                }

                var req = serverClient.CreateRequest("/admin/databases/" + Uri.EscapeDataString(name), "PUT");
                req.Write(doc.ToString(Formatting.Indented));
                return req.ExecuteRequestAsync();
            })
                   .Unwrap()
                   .ContinueWith(x =>
            {
                if (ignoreFailures == false)
                {
                    x.AssertNotFailed();                                        // will throw on error
                }
                return new RavenDocumentsByEntityName().ExecuteAsync(serverClient.ForDatabase(name), new DocumentConvention());
            }).Unwrap()
                   .ObserveException());
        }
Exemple #4
0
        ///<summary>
        /// Ensures that the database exists, creating it if needed
        ///</summary>
        public static async Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false)
        {
            var serverClient = self.ForSystemDatabase() as AsyncServerClient;

            if (serverClient == null)
            {
                throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self);
            }

            var doc   = MultiDatabase.CreateDatabaseDocument(name);
            var docId = "Raven/Databases/" + name;

            serverClient.ForceReadFromMaster();

            var get = await serverClient.GetAsync(docId).ConfigureAwait(false);

            if (get != null)
            {
                return;
            }

            var req = serverClient.CreateRequest("/admin/databases/" + Uri.EscapeDataString(name), "PUT");

            req.Write(doc.ToString(Formatting.Indented));
            try
            {
                await req.ExecuteRequestAsync().ConfigureAwait(false);
            }
            catch (Exception)
            {
                if (ignoreFailures)
                {
                    return;
                }
            }
            await new RavenDocumentsByEntityName().ExecuteAsync(serverClient.ForDatabase(name), new DocumentConvention()).ConfigureAwait(false);
        }
Exemple #5
0
        ///<summary>
        /// Ensures that the database exists, creating it if needed
        ///</summary>
        public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false)
        {
            var serverClient = self.ForSystemDatabase() as AsyncServerClient;

            if (serverClient == null)
            {
                throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self);
            }

            serverClient.ForceReadFromMaster();

            var doc   = MultiDatabase.CreateDatabaseDocument(name);
            var docId = "Raven/Databases/" + name;

            return(serverClient.GetAsync(docId)
                   .ContinueWith(get =>
            {
                if (get.Result != null)
                {
                    return get;
                }


                return (Task)serverClient.PutAsync(docId, null, doc, new RavenJObject());
            })
                   .Unwrap()
                   .ContinueWith(x =>
            {
                if (ignoreFailures == false)
                {
                    x.Wait();                             // will throw on error
                }
                var observedException = x.Exception;
                GC.KeepAlive(observedException);
            }));
        }