Exemplo n.º 1
0
 public static void ShowSettingsLink()
 {
     Program.WriteLine("", "To learn how to work with settings, visit: ");
     Program.WriteLine(ConsoleColor.Cyan, " https://github.com/vb-consulting/PgRoutiner/wiki/1.-WORKING-WITH-SETTINGS", "");
 }
Exemplo n.º 2
0
        public static void ShowUpdatedSettings()
        {
            bool settingsWritten = false;

            void WriteSetting(string name, object value, System.Type type)
            {
                if (!settingsWritten)
                {
                    Program.WriteLine("");
                    Program.WriteLine("Using settings: ");
                }
                if (value == null)
                {
                    Program.Write(ConsoleColor.Yellow, $" {name.ToKebabCase()} = ");
                    Program.WriteLine(ConsoleColor.Cyan, "null");
                }
                else if (type == typeof(bool) && (bool)value)
                {
                    Program.WriteLine(ConsoleColor.Yellow, $" {name.ToKebabCase()}");
                }
                else
                {
                    Program.Write(ConsoleColor.Yellow, $" {name.ToKebabCase()} = ");
                    if (type == typeof(IList <string>))
                    {
                        Program.WriteLine(ConsoleColor.Cyan, $"{string.Join(", ", (value as IList<string>).ToArray())}");
                    }
                    else
                    if (type == typeof(HashSet <string>))
                    {
                        Program.WriteLine(ConsoleColor.Cyan, $"{string.Join(", ", (value as HashSet<string>).ToArray())}");
                    }
                    else
                    {
                        Program.WriteLine(ConsoleColor.Cyan, $"{value}");
                    }
                }
                settingsWritten = true;
            }

            var defaultValue = new Settings();

            foreach (var prop in Value.GetType().GetProperties())
            {
                var name = prop.Name;
                if (prop.PropertyType == typeof(bool))
                {
                    var v1 = (bool)prop.GetValue(Value);
                    var v2 = (bool)prop.GetValue(defaultValue);
                    if (v1 == v2)
                    {
                        continue;
                    }
                    WriteSetting(prop.Name, v1, prop.PropertyType);
                }
                if (prop.PropertyType == typeof(string))
                {
                    var s1 = (string)prop.GetValue(Value);
                    var s2 = (string)prop.GetValue(defaultValue);
                    if (string.Equals(s1, s2))
                    {
                        continue;
                    }
                    WriteSetting(prop.Name, s1, prop.PropertyType);
                }
                if (prop.PropertyType == typeof(IList <string>))
                {
                    var l1 = (IList <string>)prop.GetValue(Value);
                    var l2 = (IList <string>)prop.GetValue(defaultValue);
                    if (l1.SequenceEqual(l2))
                    {
                        continue;
                    }
                    WriteSetting(prop.Name, l1, prop.PropertyType);
                }
                if (prop.PropertyType == typeof(HashSet <string>))
                {
                    var l1 = (HashSet <string>)prop.GetValue(Value);
                    var l2 = (HashSet <string>)prop.GetValue(defaultValue);
                    if (l1.SequenceEqual(l2))
                    {
                        continue;
                    }
                    WriteSetting(prop.Name, l1, prop.PropertyType);
                }
            }
        }
Exemplo n.º 3
0
        public IEnumerable <(string name, string content, PgType type, string schema)> GetDatabaseObjects()
        {
            var args = string.Concat(
                baseArg,
                " --schema-only",
                settings.DbObjectsOwners ? "" : " --no-owner",
                settings.DbObjectsPrivileges ? "" : " --no-acl",
                settings.DbObjectsDropIfExists ? " --clean --if-exists" : "");

            foreach (var table in Connection.GetTables(settings))
            {
                var    name    = table.GetFileName();
                string content = null;
                try
                {
#pragma warning disable CS8509
                    content = table.Type switch
#pragma warning restore CS8509
                    {
                        PgType.Table => GetTableContent(table, args),
                        PgType.View => GetViewContent(table, args)
                    };
                }
                catch (Exception e)
                {
                    Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                    continue;
                }

                yield return(name, content, table.Type, table.Schema);
            }

            foreach (var seq in Connection.GetSequences(settings))
            {
                var    name = seq.GetFileName();
                string content;
                try
                {
                    content = GetSeqContent(seq, args);
                }
                catch (Exception e)
                {
                    Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                    continue;
                }
                yield return(name, content, seq.Type, seq.Schema);
            }

            List <string> lines = null;
            List <PgItem> types = new();
            try
            {
                lines = GetDumpLines(args, excludeTables, out types);
            }
            catch (Exception e)
            {
                Program.WriteLine(ConsoleColor.Red, $"Could not create pg_dump for functions and procedures", $"ERROR: {e.Message}");
            }

            if (lines != null)
            {
                foreach (var routine in Connection.GetRoutines(settings))
                {
                    var    name = routine.GetFileName();
                    string content;
                    try
                    {
                        content = new RoutineDumpTransformer(routine, lines)
                                  .BuildLines(dbObjectsCreateOrReplace: settings.DbObjectsCreateOrReplace)
                                  .ToString();
                    }
                    catch (Exception e)
                    {
                        Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                        continue;
                    }
                    yield return(name, content, routine.Type, routine.Schema);
                }

                foreach (var domain in Connection.GetDomains(settings))
                {
                    var    name = domain.GetFileName();
                    string content;
                    try
                    {
                        content = new DomainDumpTransformer(domain, lines)
                                  .BuildLines()
                                  .ToString();
                    }
                    catch (Exception e)
                    {
                        Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                        continue;
                    }
                    yield return(name, content, domain.Type, domain.Schema);
                }

                foreach (var type in Connection.FilterTypes(types, settings))
                {
                    var    name = type.GetFileName();
                    string content;
                    try
                    {
                        content = new TypeDumpTransformer(type, lines)
                                  .BuildLines()
                                  .ToString();
                    }
                    catch (Exception e)
                    {
                        Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                        continue;
                    }
                    yield return(name, content, type.Type, type.Schema);
                }

                foreach (var schema in Connection.GetSchemas(settings))
                {
                    if (string.Equals(schema, "public"))
                    {
                        continue;
                    }
                    var    name = $"{schema}.sql";
                    string content;
                    try
                    {
                        content = new SchemaDumpTransformer(schema, lines)
                                  .BuildLines()
                                  .ToString();
                    }
                    catch (Exception e)
                    {
                        Program.WriteLine(ConsoleColor.Red, $"Could not write dump file {name}", $"ERROR: {e.Message}");
                        continue;
                    }
                    yield return(name, content, PgType.Schema, null);
                }
            }
        }