///<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.ForDefaultDatabase() 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;

            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);
            }));
        }
        ///<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.ForDefaultDatabase() 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;

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

                var req = serverClient.CreateRequest("PUT", "/admin/databases/" + Uri.EscapeDataString(name));
                req.Write(doc.ToString(Formatting.Indented));
                return req.ExecuteRequestAsync();
            })
                   .Unwrap()
                   .ContinueWith(x =>
            {
                if (ignoreFailures == false)
                {
                    x.Wait();                             // will throw on error
                }
                var observedException = x.Exception;
                GC.KeepAlive(observedException);
            }));
        }
Exemple #3
0
        ///<summary>
        /// Ensures that the database exists, creating it if needed
        ///</summary>
        public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false)
        {
            self = self.ForDefaultDatabase();
            var doc   = MultiDatabase.CreateDatabaseDocument(name);
            var docId = "Raven/Databases/" + name;

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

                return (Task)self.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);
            }));
        }
        public static Task CreateDatabaseAsync(this IAsyncDatabaseCommands self, DatabaseDocument databaseDocument, bool ignoreFailures = false)
        {
            var serverClient = self.ForDefaultDatabase() 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("PUT", "/admin/databases/" + Uri.EscapeDataString(databaseDocument.Id));

            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)
        {
            self = self.ForDefaultDatabase();
            AssertValidName(name);
            var doc = RavenJObject.FromObject(new DatabaseDocument
            {
                Settings =
                {
                    { "Raven/DataDir", Path.Combine("~", Path.Combine("Tenants", name)) }
                }
            });

            doc.Remove("Id");
            var docId = "Raven/Databases/" + name;

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

                return (Task)self.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);
            }));
        }