public static bool CheckPgDumpVersion(PgDumpBuilder builder)
        {
            var    option      = (typeof(Settings).GetField($"{builder.PgDumpName}Args").GetValue(null) as Arg).Original;
            var    connVersion = builder.Connection.ServerVersion.Split(".").First();
            string fullDumpVersion;
            string dumpVersion;

            try
            {
                fullDumpVersion = builder.GetDumpVersion();
                dumpVersion     = fullDumpVersion.Split(".").First();
            }
            catch (Exception e)
            {
                PgDumpError(connVersion, e, option);
                return(false);
            }

            if (!string.Equals(connVersion, dumpVersion))
            {
                builder.SetPgDumpName(string.Format(Value.PgDumpFallback, connVersion));
                try
                {
                    fullDumpVersion = builder.GetDumpVersion();
                    dumpVersion     = fullDumpVersion.Split(".").First();
                }
                catch (Exception e)
                {
                    PgDumpError(connVersion, e, option);
                    return(false);
                }

                var value = typeof(Settings).GetProperty(builder.PgDumpName).GetValue(Value);
                if (!string.Equals(connVersion, dumpVersion))
                {
                    PgDumpMistmatch(builder.Connection, connVersion, fullDumpVersion, dumpVersion, value, option);
                    return(false);
                }
                Program.WriteLine(ConsoleColor.Yellow, "",
                                  $"WARNING: Using fall-back path for pg_dump: {value}. To remove this warning set the {option} setting to point to this path.");
            }
            return(true);
        }
Exemplo n.º 2
0
 private static void BuildObjectDumps(PgDumpBuilder builder)
 {
     if (string.IsNullOrEmpty(Settings.Value.DbObjectsDir))
     {
         return;
     }
Exemplo n.º 3
0
        private static void BuildDiffScript(NpgsqlConnection connection)
        {
            if (!Settings.Value.Diff)
            {
                return;
            }
            using var target = new ConnectionManager(Program.Config, nameof(Settings.DiffTarget), Settings.Value.Connection).ParseConnectionString();
            if (target == null)
            {
                return;
            }

            var sourceBuilder = new PgDumpBuilder(Settings.Value, connection);

            if (!Settings.CheckPgDumpVersion(sourceBuilder))
            {
                return;
            }

            var targetBuilder = new PgDumpBuilder(Settings.Value, target, nameof(Settings.DiffPgDump));

            if (!Settings.CheckPgDumpVersion(targetBuilder))
            {
                return;
            }
            Dump("");
            var targetName = (Settings.Value.DiffTarget ?? $"{target.Host}_{target.Port}_{target.Database}").SanitazePath();

            var now = DateTime.Now;

            string GetFilePattern(int ord)
            {
                var c1 = Settings.Value.DiffFilePattern.Contains("{0");
                var c2 = Settings.Value.DiffFilePattern.Contains("{1");
                var c3 = Settings.Value.DiffFilePattern.Contains("{2");
                var c4 = Settings.Value.DiffFilePattern.Contains("{3");

                if (c1 && c2 && c3 && c4)
                {
                    return(string.Format(Settings.Value.DiffFilePattern, ConnectionName, targetName, ord, now));
                }
                else if (c1 && c2 && c3)
                {
                    return(string.Format(Settings.Value.DiffFilePattern, ConnectionName, targetName, ord));
                }
                else if (c1 && c2)
                {
                    return(string.Format(Settings.Value.DiffFilePattern, ConnectionName, targetName));
                }
                else if (c1)
                {
                    return(string.Format(Settings.Value.DiffFilePattern, ConnectionName));
                }
                return(Settings.Value.DiffFilePattern);
            }

            var title   = string.Format("{0}__diff__{1}", connection.Database, target.Database).SanitazeName();
            var builder = new PgDiffBuilder(Settings.Value, connection, target, sourceBuilder, targetBuilder, title);
            var content = builder.Build((msg, step, total) =>
            {
                DumpFormat(string.Concat("Step ", step.ToString(), " of ", total.ToString(), ": {0}"), msg);
            });

            if (string.IsNullOrEmpty(content))
            {
                Dump($"No diff found between {ConnectionName} and {targetName}...");
                return;
            }
            if (Settings.Value.DiffFilePattern != null && !Settings.Value.Dump)
            {
                var dir = Path.GetFullPath(Path.GetDirectoryName(GetFilePattern(1)));
                if (!Directory.Exists(dir))
                {
                    DumpFormat("Creating dir: {0}", dir);
                    Directory.CreateDirectory(dir);
                }
                int i = 1;
                foreach (var existingFile in Directory.EnumerateFiles(dir))
                {
                    var fileContent = File.ReadAllText(existingFile);
                    if (Equals(content, fileContent))
                    {
                        DumpRelativePath("File with same diff script {0} already exists ...", existingFile);
                        return;
                    }
                    i++;
                }

                var file = GetFilePattern(i);
                DumpRelativePath("Creating new diff file: {0} ...", Path.GetFullPath(file));
                WriteFile(file, content);
            }
            else
            {
                Dump("");
                DumpFormat("Source connection: {0}", ConnectionName);
                DumpFormat("Target connection: {0}", targetName);
                Dump("Diff script:");
                Console.Write(content);
            }
        }
Exemplo n.º 4
0
        public static void Run(NpgsqlConnection connection)
        {
            ConnectionName = (Settings.Value.Connection ?? $"{connection.Host}_{connection.Port}_{connection.Database}").SanitazePath();

            if (Settings.Value.Execute != null)
            {
                DumpTitle("** EXECUTION **");
                ExecuteFromSetting(connection);
                return;
            }

            if (Settings.Value.Psql)
            {
                DumpTitle("** PSQL TERMINAL **");
                new PsqlRunner(Settings.Value, connection).TryRunFromTerminal();
                return;
            }

            if (Settings.Value.CommitMd)
            {
                DumpTitle("** COMMIT MARKDOWN (MD) EDITS **");
                BuildMdDiff(connection);
                return;
            }

            if (Settings.Value.SchemaDumpFile != null)
            {
                SchemaFile = string.Format(Path.GetFullPath(Path.Combine(Program.CurrentDir, Settings.Value.SchemaDumpFile)), ConnectionName);
            }

            if (Settings.Value.DataDumpFile != null)
            {
                DataFile = string.Format(Path.GetFullPath(Path.Combine(Program.CurrentDir, Settings.Value.DataDumpFile)), ConnectionName);
            }

            if (Settings.Value.DbObjects || Settings.Value.SchemaDump || Settings.Value.DataDump)
            {
                var builder = new PgDumpBuilder(Settings.Value, connection);
                if (Settings.CheckPgDumpVersion(builder))
                {
                    if (Settings.Value.SchemaDump)
                    {
                        DumpTitle("** SCHEMA DUMP SCRIPT GENERATION **");
                        BuildDump(
                            dumpFile: Settings.Value.SchemaDumpFile,
                            file: SchemaFile,
                            overwrite: Settings.Value.SchemaDumpOverwrite,
                            askOverwrite: Settings.Value.SchemaDumpAskOverwrite,
                            contentFunc: () => builder.GetSchemaContent());
                    }
                    if (Settings.Value.DataDump)
                    {
                        DumpTitle("** DATA DUMP SCRIPT GENERATION **");
                        BuildDump(
                            dumpFile: Settings.Value.DataDumpFile,
                            file: DataFile,
                            overwrite: Settings.Value.DataDumpOverwrite,
                            askOverwrite: Settings.Value.DataDumpAskOverwrite,
                            contentFunc: () => builder.GetDataContent());
                    }
                    if (Settings.Value.DbObjects)
                    {
                        DumpTitle("** DATA OBJECTS SCRIPTS TREE GENERATION **");
                        BuildObjectDumps(builder);
                    }
                }
            }

            if (Settings.Value.Diff)
            {
                DumpTitle("** DIFF  SCRIPT GENERATION **");
                BuildDiffScript(connection);
            }

            if (Settings.Value.Routines)
            {
                DumpTitle("** ROUTINE SOURCE CODE GENERATION **");
                new CodeRoutinesBuilder(connection, Settings.Value, CodeSettings.ToRoutineSettings(Settings.Value)).Build();
            }

            if (Settings.Value.Crud)
            {
                DumpTitle("** CRUD SOURCE CODE GENERATION **");
                new CodeCrudBuilder(connection, Settings.Value, CodeSettings.ToCrudSettings(Settings.Value)).Build();
            }

            if (Settings.Value.UnitTests)
            {
                DumpTitle("** UNIT TEST PROJECT TEMPLATE CODE GENERATION **");
                BuildUnitTests(connection);
            }

            if (Settings.Value.Markdown)
            {
                DumpTitle("** MARKDOWN (MD) GENERATION **");
                BuilMd(connection);
            }

            DumpTitle("", "", "**** FINISHED ****");
        }