Ejemplo n.º 1
0
        /// <summary>
        /// Use an arbitrary template to generate an SQL script.
        /// </summary>
        /// <param name="configFile"></param>
        public static void GenerateScript(Configs.InitialConfig config, string templateFileName)

        {
            string templateContents = Generators.GetTemplateContents(templateFileName);

            // get the list of all placeholders in the template
            var matches           = Regex.Matches(templateContents, @"{{\s*([^}]+)\s*}}");
            var matchPlaceholders = new List <string>(); // contains replacement placeholders like {{loggingAzStorageContainerUrl}}

            // process placeholders one by one
            foreach (Match match in matches)
            {
                string placeholder = match.Value;
                if (matchPlaceholders.Contains(placeholder))
                {
                    continue;                       // ignore repeating placeholders
                }
                matchPlaceholders.Add(placeholder); // add it to the list so we don't process it multiple times

                try
                {
                    // replace all instances with the value from config
                    string matchValue = (string)config.GetType().GetField(match.Groups[1].Value).GetValue(config);
                    templateContents = templateContents.Replace(placeholder, matchValue, StringComparison.Ordinal);
                }
                catch (Exception ex)
                {
                    Program.WriteLine();
                    Program.WriteLine($"Variable has no matching config entry: {placeholder}.", ConsoleColor.Red);
                    Program.ExitApp();
                }
            }

            string fileSuffix = string.Format(Program.FileNames.OutputFileNameMaskRunOnMirror, config.mirrorDB, "x", "x");

            string outputFileName = $"{Path.GetFileNameWithoutExtension(templateFileName)}__{fileSuffix}{fileExtSQL}";

            Generators.SaveGeneratedScript(templateContents, outputFileName, 0);
        }
        /// <summary>
        /// Generates *CREATE MASTER KEY* statements
        /// </summary>
        /// <param name="configFile"></param>
        public static void CreateExternalDataSource(Configs.CreateExternalDataSource[] config)
        {
            // load data
            Configs.CreateExternalDataSource sharedConfig = new Configs.CreateExternalDataSource();
            string templateContents = Generators.GetTemplateContents("CreateExternalDataSource.txt");

            // generate output one file at a time
            for (int i = 0; i < config.Length; i++)
            {
                // merge with the previous full version of the config
                sharedConfig = (Configs.CreateExternalDataSource)config[i].Merge(sharedConfig).Clone();

                // there may be 2 loops if it generates scripts for mutual access
                for (int j = 0; j < 2; j++)
                {
                    // interpolate
                    string outputContents = string.Format(templateContents,
                                                          config[i].localDB, config[i].externalDB,
                                                          config[i].serverName, config[i].credential);

                    string outputFileName = $"CreateExternalDataSource__{config[i].localDB}__{config[i].externalDB}__x{fileExtSQL}";

                    Generators.SaveGeneratedScript(outputContents, outputFileName, i);

                    // exit the loop now if the data source is not reciprocal - one way only
                    if (config[i].twoway != "1" || j == 1)
                    {
                        break;
                    }

                    // or swap the DBs if it's two-way generation
                    string ldb = config[i].localDB;
                    config[i].localDB    = config[i].externalDB;
                    config[i].externalDB = ldb;
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Program.WriteLine($"AzurePoolCrossDbGenerator started in {Directory.GetCurrentDirectory()} with params:");

            string command = (args.Length > 0) ? args[0]?.Trim().ToLowerInvariant() : ""; // must be a valid command

            Program.WriteLine($"Command: {command}");

            string paramTemplate = null, paramConfig = null, paramGrepFileName = null, paramTargetDir = null, paramRunOn = null, paramCSLatest = null, paramCSBase = null,
                   paramFileWithListOfItems = null;

            // extract additional params
            for (int i = 1; i < args.Length - 1; i++)
            {
                switch (args[i])
                {
                case "-t":
                {
                    paramTemplate = args[i + 1];
                    Program.WriteLine($"Template: {paramTemplate}");
                    break;
                }

                case "-c":
                {
                    paramConfig = args[i + 1];
                    Program.WriteLine($"Config: {paramConfig}");
                    break;
                }

                case "-g":
                {
                    paramGrepFileName = args[i + 1];
                    Program.WriteLine($"Grep: {paramGrepFileName}");
                    break;
                }

                case "-d":
                {
                    paramTargetDir = args[i + 1];
                    Program.WriteLine($"Target dir: {paramTargetDir}");
                    break;
                }

                case "-o":
                {
                    paramRunOn = args[i + 1];
                    Program.WriteLine($"Run on: {paramRunOn}");
                    break;
                }

                case "-csl":
                {
                    paramCSLatest = args[i + 1];
                    Program.WriteLine($"Connection string, DB to extract from: {paramCSLatest}");
                    break;
                }

                case "-csb":
                {
                    paramCSBase = args[i + 1];
                    Program.WriteLine($"Connection string, DB to compare to: {paramCSBase}");
                    break;
                }

                case "-l":
                {
                    paramFileWithListOfItems = args[i + 1];
                    Program.WriteLine($"File with list of items: {paramFileWithListOfItems}");
                    break;
                }
                }
            }

            // validate the params
            if (string.IsNullOrEmpty(command))
            {
                PrintWelcomeMsg();
            }

            // call the handler for the command
            switch (command)
            {
            case Commands.GenerateBlankConfigFiles:
            {
                Generators.GenerateBlankConfigs();
                break;
            }

            case Commands.GenerateSecondaryConfigFiles:
            {
                Generators.GenerateSecondaryConfigFiles(Configs.InitialConfig.Load(paramConfig));
                break;
            }

            case Commands.GenerateMasterKeys:
            {
                Generators.CreateMasterKey(Configs.CreateMasterKey.Load(paramConfig));
                break;
            }

            case Commands.GenerateExternalDataSources:
            {
                Generators.CreateExternalDataSource(Configs.CreateExternalDataSource.Load(paramConfig));
                break;
            }

            case Commands.ScriptGenerationForTablesAnsSPs:
            {
                Generators.GenerateScript(Configs.AllTables.Load(paramConfig), paramTemplate, paramRunOn);
                break;
            }

            case Commands.ScriptGenerationGeneric:
            {
                Generators.GenerateScript(Configs.InitialConfig.Load(paramConfig), paramTemplate);
                break;
            }

            case Commands.ScriptGenerationExtractFromDb:
            {
                Generators.ExtractScriptsFromDb(paramCSLatest, paramCSBase, paramFileWithListOfItems);
                break;
            }

            case Commands.AltTableColumnTypes:
            {
                Generators.GenerateAltColumnsScript(Configs.AllTables.Load(paramConfig));
                break;
            }

            case Commands.GenerateSqlCmdBatch:
            {
                Generators.GenerateSqlCmdBatch(Configs.InitialConfig.Load(paramConfig), paramTargetDir, paramRunOn);
                break;
            }

            case Commands.ReplaceInSqlFiles:
            {
                Generators.SearchAndReplace(Configs.InitialConfig.Load(paramConfig), paramGrepFileName, paramTemplate);
                break;
            }

            default:
            {
                PrintWelcomeMsg();
                break;
            }
            }

            ExitApp(0);
        }