Ejemplo n.º 1
0
        /// <summary>
        /// Drops the specified dbs
        /// </summary>
        /// <param name="dbNames"></param>
        protected virtual void DropDb(params string[] dbNames)
        {
            //wipe out the db name for the connection as need to connect to the service db!
            var curentDbName = Dsc.DbName;

            Dsc.DbName = string.Empty;

            try
            {
                foreach (var dbName in dbNames)
                {
                    ConsoleEx.Write($"Dropping db: {dbName}... ", ConsoleColor.DarkYellow);

                    //first cut the connection to the db if any
                    using (var conn = new NpgsqlConnection(Dsc.GetConnectionString(serviceDatabase: true)))
                    {
                        conn.Open();

                        var cmd = new NpgsqlCommand();
                        cmd.Connection  = conn;
                        cmd.CommandText =
                            $"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '{dbName}';";

                        cmd.ExecuteNonQuery();

                        conn.CloseConnection();
                    }

                    //it should be possible to drop the db now
                    using (var conn = new NpgsqlConnection(Dsc.GetConnectionString(serviceDatabase: true)))
                    {
                        conn.Open();

                        var cmd = new NpgsqlCommand();
                        cmd.Connection  = conn;
                        cmd.CommandText = $"DROP DATABASE IF EXISTS {dbName}";

                        cmd.ExecuteNonQuery();

                        conn.CloseConnection();
                    }

                    ConsoleEx.Write("Done!" + Environment.NewLine, ConsoleColor.DarkGreen);
                }

                Console.WriteLine();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                //restore previously set dbname
                Dsc.DbName = curentDbName;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Drops specified schemas
        /// </summary>
        /// <param name="schemaNames">k,v: schemaname,dbname</param>
        protected virtual void DropSchema(Dictionary <string, string> schemaNames)
        {
            //cache whatever is the current db name, as it will be restored later; in order to drop schema, need to connect to a particular db!
            var curentDbName = Dsc.DbName;

            try
            {
                foreach (var schema in schemaNames)
                {
                    Dsc.DbName = schema.Value;

                    ConsoleEx.Write($"Dropping schema: {schema.Key}... ", ConsoleColor.DarkYellow);

                    //FIXME - need to check if db exists, oherwise will fail...

                    using (var conn = new NpgsqlConnection(Dsc.GetConnectionString()))
                    {
                        conn.Open();

                        var cmd = new NpgsqlCommand();
                        cmd.Connection  = conn;
                        cmd.CommandText = $"DROP SCHEMA IF EXISTS {schema.Key} CASCADE;";

                        cmd.ExecuteNonQuery();

                        conn.CloseConnection();
                    }

                    ConsoleEx.Write("Done!" + Environment.NewLine, ConsoleColor.DarkGreen);
                    ConsoleEx.WriteLine("INFO:", ConsoleColor.DarkMagenta);
                    ConsoleEx.WriteLine("With EF Core dropping schema is not enough! You will reset the migrations too. They should be located in the public schema.", ConsoleColor.DarkMagenta);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                //restore previously set dbname
                Dsc.DbName = curentDbName;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Drops an organization by id
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="clean"></param>
        /// <returns>Whether or not an org drop procedure has been performed</returns>
        protected async Task <bool> DropOrganizationAsync(Guid orgId, bool clean)
        {
            var output = false;

            //remote mode
            if (RemoteMode)
            {
                var org = await GetOrgRemoteAsync(orgId);

                if (org != null)
                {
                    Console.WriteLine();
                    if (clean &&
                        !PromptUser(
                            "A database previously registered to the specified user already exists and you are about to delete it. Proceed?"))
                    {
                        return(false);
                    }

                    //org exists, so need to drop it with a remote API...
                    ConsoleEx.Write($"Dropping organisation '{org.DisplayName}' ({org.Slug}) with its db... ", ConsoleColor.DarkYellow);
                    await DropOrgRemoteAsync(orgId, clean);

                    ConsoleEx.Write("Done!" + Environment.NewLine, ConsoleColor.DarkGreen);

                    //assume it's been dropped. otherwise api will throw (at least it should...)
                    output = true;
                }
            }
            //and local
            else
            {
                //if an organisation for a specified user exists, destroy it
                using (var dbCtx = GetMapHiveDbContext())
                {
                    var org = await dbCtx.Organizations.Where(x => x.Uuid == orgId).FirstOrDefaultAsync();

                    if (org != null)
                    {
                        //to drop db need to connect to a proper db (org can be somewhere out there...), so need to save the current dsc
                        var dsc = Dsc.Clone();
                        org.LoadDatabases(dbCtx);

                        Console.WriteLine();
                        if (clean &&
                            !PromptUser(
                                "A database previously registered to the specified user already exists and you are about to delete it. Proceed?"))
                        {
                            return(false);
                        }

                        //clean the org rec in the metadata
                        ConsoleEx.Write($"Dropping organisation '{org.DisplayName}' ({org.Slug}) with its db... ", ConsoleColor.DarkYellow);

                        await org.DestroyAsync(dbCtx, clean);

                        ConsoleEx.Write("Done!" + Environment.NewLine, ConsoleColor.DarkGreen);
                    }
                }

                output = true;
            }

            Console.WriteLine();
            return(output);
        }