Exemplo n.º 1
0
 internal App(
     RunArguments runArgs,
     DirectoryInfo appDir)
 {
     _runArgs = runArgs;
     _appDir  = appDir;
 }
Exemplo n.º 2
0
        private ConnectionStringSettings ApplyScriptsToDb(
            RunArguments runArgs,
            DirectoryInfo dbDir)
        {
            var scripts = GetExistingScriptFiles(dbDir);

            if (scripts.Count() == 0)
            {
                Console.WriteLine("No previous scripts detected.");
            }

            ConnectionStringSettings conSettings;

            if (runArgs.RunMode == RunMode.Interactive)
            {
                conSettings = LetUserPickDbConnection(dbDir.Name);
            }
            else if (runArgs.RunMode == RunMode.ApplyScriptsToDb)
            {
                conSettings = new ConnectionStringSettings(runArgs.DbKey, runArgs.DbConnectionString, runArgs.DbConnectionProvider);
            }
            else
            {
                throw new InvalidArgumentException("Invalid execution path.");
            }

            ApplyMissingScripts(runArgs, conSettings, scripts);

            return(conSettings);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("DbScriptomate");

            var currentExePath = Assembly.GetExecutingAssembly().Location;
            var appDir         = new DirectoryInfo(Path.GetDirectoryName(currentExePath));
            var runArgs        = new RunArguments(args, appDir);

            if (runArgs.RunMode == RunMode.Help)
            {
                ShowHelp();
                return;
            }
            Console.WriteLine("Run /? for command line help.");

            var app = new App(runArgs, appDir);

            app.Execute();

            if (runArgs.RunMode == RunMode.Interactive)
            {
                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();
            }
        }
Exemplo n.º 4
0
        private void RunInteractively(
            RunArguments runArgs,
            DirectoryInfo dbDir)
        {
            Console.WriteLine("Pick:");
            Console.WriteLine("1) Detect last script number and generate new script template");
            Console.WriteLine("2) Detect missing scripts in DB");
            Console.WriteLine("3) Setup your Database for DbScriptomate");
            var input = Console.ReadKey();

            Console.Clear();
            switch (input.KeyChar)
            {
            case '1':
                GenerateNewScript(runArgs, dbDir);
                break;

            case '2':
                ApplyScriptsToDb(runArgs, dbDir);
                break;

            case '3':
                InitialSetup();
                break;
            }
        }
Exemplo n.º 5
0
        private void GenerateDbObjects(RunArguments runArgs, DirectoryInfo dbDir)
        {
            var startTime = DateTime.Now;

            Console.WriteLine("Getting schema information");

            var dbConnectionString = runArgs.DbConnectionString;

            if (string.IsNullOrWhiteSpace(dbConnectionString))
            {
                dbConnectionString = LetUserPickDbConnection(dbDir.Name).ConnectionString;
            }

            var ignoreValue = System.Configuration.ConfigurationManager.AppSettings.Get("GenerateDbObjectsIgnoreSchemas");

            if (string.IsNullOrWhiteSpace(ignoreValue))
            {
                ignoreValue = string.Empty;
            }
            var ignoreSchemas = ignoreValue
                                .Split(new[] { " ", ",", ";" }, StringSplitOptions.RemoveEmptyEntries)
                                .ToList();

            var globalConnection       = new SqlConnection(dbConnectionString);
            var globalServerConnection = new ServerConnection(globalConnection);
            var globalServer           = new Server(globalServerConnection);
            var globalDb = globalServer.Databases[globalConnection.Database];

            DirectoryInfo rootDir        = dbDir;
            var           targetDir      = rootDir.CreateSubdirectory("DbObjects");
            var           typeDbDobjects = new Dictionary <GenerateDbObjectsType, ICollection <ScriptSchemaObjectBase> >();

            // create folder structure and get dbobjects for each type
            foreach (var e in Enum.GetNames(typeof(GenerateDbObjectsType)))
            {
                var location = e.Replace("_", "\\");
                rootDir.CreateSubdirectory($"DbObjects\\{location}");
                var en        = (GenerateDbObjectsType)Enum.Parse(typeof(GenerateDbObjectsType), e);
                var dbobjects = GetDbObjects(en, globalDb, ignoreSchemas).ToList();
                typeDbDobjects.Add(en, dbobjects);
                Console.WriteLine($"{location} : {dbobjects.Count}");
            }

            foreach (var e in typeDbDobjects)
            {
                var names = e.Value.Select(o => new KeyValuePair <string, string>(o.Schema, o.Name)).ToList();
                GenerateDbObjectsInThreadsAndWait(e.Key, names, dbConnectionString, targetDir, ignoreSchemas);
            }

            var endTime = DateTime.Now;

            Console.WriteLine($"Total time : {endTime.Subtract(startTime).TotalSeconds} seconds");
        }
Exemplo n.º 6
0
        private void GenerateNewScript(
            RunArguments runArgs,
            DirectoryInfo dbDir)
        {
            var response = GetNextSequenceNumber(dbDir.Name, runArgs.UseLocal);

            Console.WriteLine(response.ToString());
            runArgs.ScriptNumber = response.NextSequenceNumber;
            var newScript = CreateNewScriptFile(dbDir, runArgs);

            Console.WriteLine("using {0} next", runArgs.ScriptNumber);
            Console.WriteLine("created file: {0}\\{1}", dbDir.Name, newScript);
            Console.WriteLine();
        }
Exemplo n.º 7
0
        private void RunInteractively(
            RunArguments runArgs,
            DirectoryInfo dbDir)
        {
            Console.WriteLine("Pick:");
            Console.WriteLine("1) Detect last script number and generate new script template");
            Console.WriteLine("2) Detect missing scripts in DB");
            Console.WriteLine("3) Setup your Database for DbScriptomate");
            Console.WriteLine("4) Generate Db Objects");
            var input = Console.ReadKey();

            var generateDbObjectsOnNewScript = false;

            bool.TryParse(System.Configuration.ConfigurationManager.AppSettings.Get("GenerateDbObjectsOnNewScript"), out generateDbObjectsOnNewScript);

            var generateDbObjectsAfterApplyScripts = false;

            bool.TryParse(System.Configuration.ConfigurationManager.AppSettings.Get("GenerateDbObjectsAfterApplyScripts"), out generateDbObjectsAfterApplyScripts);

            Console.Clear();
            switch (input.KeyChar)
            {
            case '1':
                GenerateNewScript(runArgs, dbDir);
                break;

            case '2':
                var connection = ApplyScriptsToDb(runArgs, dbDir);
                if (connection != null && generateDbObjectsAfterApplyScripts)
                {
                    Console.WriteLine("Automatically generating db object scripts (GenerateDbObjectsAfterApplyScripts)");
                    Console.WriteLine("Press any key to continue or press 's' to skip");
                    input = Console.ReadKey(true);
                    if (input.Key != ConsoleKey.S)
                    {
                        runArgs.DbConnectionString = connection.ConnectionString;
                        GenerateDbObjects(runArgs, dbDir);
                    }
                }
                break;

            case '3':
                InitialSetup();
                break;

            case '4':
                GenerateDbObjects(runArgs, dbDir);
                break;
            }
        }
Exemplo n.º 8
0
        private string CreateNewScriptFile(
            DirectoryInfo dir,
            RunArguments attributes)
        {
            const string fileNameFormat = "{0:000.0}.{1}.{2}.{3}.sql";
            string       templateFile   = "_NewScriptTemplate.sql";

            templateFile = Path.Combine(dir.FullName, templateFile);
            if (!File.Exists(templateFile))
            {
                throw new FileNotFoundException(templateFile);
            }
            var template = File.ReadAllText(templateFile);
            var contents = string.Format(template, attributes.ScriptNumber, attributes.DdlOrDmlType, attributes.Author, attributes.Description);
            var filename = string.Format(fileNameFormat, attributes.ScriptNumber, attributes.Author, attributes.DdlOrDmlType, attributes.Description);

            File.AppendAllText(Path.Combine(dir.FullName, filename), contents, Encoding.UTF8);
            return(filename);
        }
Exemplo n.º 9
0
        private void ApplyMissingScripts(
            RunArguments runArgs,
            ConnectionStringSettings conSettings,
            IList <FileInfo> scripts)
        {
            var scriptsToRun    = new List <FileInfo>();
            var dbScriptNumbers = GetDbScripts(conSettings);

            Console.WriteLine("{0} scripts logged in dbo.DbScripts", dbScriptNumbers.Count());
            Console.WriteLine("The following are scripts not yet run on the selected DB:");
            foreach (var scriptFile in scripts)
            {
                var scriptNumber = ToDecimal(scriptFile.Name);
                if (!dbScriptNumbers.Contains(scriptNumber))
                {
                    PrintScriptItemInfo(scriptFile);
                    scriptsToRun.Add(scriptFile);
                }
            }
            Console.WriteLine("Missing scripts check completed");
            bool userSelectedToApplyScripts = false;

            if (runArgs.RunMode == RunMode.Interactive)
            {
                Console.WriteLine("Would you like me to run them one at a time? I'll break on any errors?");
                Console.WriteLine("1 - Yes, please run one at a time.");
                Console.WriteLine("2 - No thanks, I'll run them  later myself.");
                int selectedIndex = int.Parse(Console.ReadKey().KeyChar.ToString());
                Console.Clear();

                if (selectedIndex == 1)
                {
                    userSelectedToApplyScripts = true;
                }
            }

            if (runArgs.RunMode == RunMode.Interactive && userSelectedToApplyScripts ||
                runArgs.RunMode == RunMode.ApplyScriptsToDb)
            {
                foreach (var scriptFile in scriptsToRun)
                {
                    Console.WriteLine("Running {0}", scriptFile.Name);
                    string errorMessage = string.Empty;
                    bool   success      = RunDbScript(conSettings, scriptFile, out errorMessage);
                    Console.WriteLine(success ? "Succeeded" : "Failed:");
                    if (!success)
                    {
                        Console.WriteLine("{0}", errorMessage);
                        if (runArgs.RunMode == RunMode.Interactive)
                        {
                            Console.WriteLine("1 - Skip, 2 - Abort?");
                            var innerInput    = Console.ReadKey();
                            int selectedIndex = int.Parse(innerInput.KeyChar.ToString());
                            if (selectedIndex == 2)
                            {
                                return;
                            }
                        }
                        // Not interactive
                        Environment.Exit(2222);
                    }
                }
            }
        }