Methods to create mutli tenants databases
Esempio n. 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);
            }

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

            serverClient.ForceReadFromMaster();

            var get = await serverClient.GetAsync(docId);

            if (get != null)
            {
                return;
            }

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

            req.Write(doc.ToString(Formatting.Indented));
            try
            {
                await req.ExecuteRequestAsync();
            }
            catch (Exception)
            {
                if (ignoreFailures)
                {
                    return;
                }
            }
            await new RavenDocumentsByEntityName().ExecuteAsync(serverClient.ForDatabase(name), new DocumentConvention());
        }
        ///<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());
        }
Esempio n. 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)
        {
            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);
            }));
        }