Example #1
0
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            var context = new Context();
            if (!InitialiseContext(context, args))
            {
                return;
            }
            var upgrader = new Upgrader(context, new LogToUpgradeLogFile(Log));

            WhenDebugging_AssertThatScriptsExistInFolders(context);

            if (!UserHasConfirmedExecution(context))
            {
                Console.WriteLine("Aborting create database and run upgrade scripts process.");
                return;
            }
            try
            {
                Console.WriteLine("Creating database if non-existent...");
                upgrader.CreateDatabaseIfNonExistent();

                if (upgrader.IsUpgradeRequired())
                {
                    Console.WriteLine("Upgrading Database...");
                    var result = upgrader.PerformUpgrade();

                    if (!result.Successful)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(result.Error);
                        Console.ResetColor();
                        return;
                    }

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Success! - Upgrade completed");
                    Console.ResetColor();

                    PrintScriptExecutionList(result.Scripts, context.TargetDatabaseName);
                }
                else
                {
                    Console.WriteLine("Upgrade is not required!");
                }
                PromptToTerminateProcess();
            }
            catch (Exception e)
            {
                const string message = "Unhandled exception thrown during upgrade process.";
                Log.Error(message, e);
                Console.Error.WriteLine("{0}: {1}", message, e);
            }
        }
Example #2
0
 private static bool InitialiseContext(Context context, string[] args)
 {
     var validCommandLineArguments = ReadCommandLineArguments(args, context);
     if (!validCommandLineArguments)
     {
         ShowUsageInformation();
         return false;
     }
     ReadAppSettings(context);
     return true;
 }
Example #3
0
 static UpgradeEngine CreateUpgradeEngine(Context context, IUpgradeLog upgradeLog)
 {
     var connectionString = context.ConnectionString;
     var scriptsPath = context.ScriptsPath;
     var auxScriptPath = context.AuxiliaryScriptsPath;
     var variables = context.Variables;
     return DeployChanges
         .To
         .SqlDatabase(connectionString)
         .WithScriptsFromFileSystem(scriptsPath)
         .WithScriptsFromFileSystem(auxScriptPath)
         .LogTo(upgradeLog)
         .WithVariables(variables)
         .Build();
 }
Example #4
0
 public Upgrader(Context context, IUpgradeLog upgradeLog)
 {
     _context = context;
     _upgradeEngine = CreateUpgradeEngine(_context, upgradeLog);
 }
Example #5
0
 private static void WhenDebugging_AssertThatScriptsExistInFolders(Context context)
 {
     foreach (var path in new[] { context.ScriptsPath, context.AuxiliaryScriptsPath })
     {
         Console.WriteLine("Looking for scripts in {0}", path);
         Debug.Assert(Directory.Exists(path), "directory non existant: " + path);
         Debug.Assert(Directory.EnumerateFiles(path).Any(), "no scripts to apply in:" + path);
         Console.WriteLine("...Okay, scripts exist in that directory.");
     }
 }
Example #6
0
 static bool UserHasConfirmedExecution(Context settings)
 {
     if (!_isInteractive)
     {
         return true;
     }
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.WriteLine("Connection string to Target database: {0}", settings.ConnectionString);
     Console.WriteLine("Target Database name: {0}", settings.TargetDatabaseName);
     Console.WriteLine("CRM Database name is: {0}", settings.CrmDatabaseName);
     PrintLine();
     Console.WriteLine("The following variable substitutions (read from {0}) will be applied:", Path.GetFileName(settings.ConfigFile));
     Console.WriteLine(AsString(settings.Variables));
     PrintLine();
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine("Proceed with creating database and running scripts? (Y/N)");
     Console.ResetColor();
     return (Console.ReadLine() ?? "").ToUpperInvariant().Trim().StartsWith("Y");
 }
Example #7
0
        private static bool ReadCommandLineArguments(string[] args, Context context)
        {
            var collection = new CommandArgumentCollection(args);
            _isInteractive = collection.HasArgument("interactive");

            var ca = collection.GetFirstArgument("cs");
            if (ca == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You must provide an argument /cs for the connection string");
                Console.ResetColor();
                return false;
            }

            SqlConnectionStringBuilder connectionStringBuilder;
            try
            {
                var connectionStringValue = ca.Value.Trim();
                connectionStringBuilder = new SqlConnectionStringBuilder(connectionStringValue);
            }
            catch (ArgumentException argEx)
            {
                Log.Error(argEx);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(
            @"Invalid connection string argument for /cs
            Argument must have the following format: Server=_Instance Name_;Database=_Catalog Name_;[Trusted_connection=True;Pooling=False]");
                Console.ResetColor();
                return false;
            }

            var targetDatabaseName = connectionStringBuilder.InitialCatalog;
            var dataSource = connectionStringBuilder.DataSource;
            var connectionString = String.Format(ConnectionStringFormat,
                dataSource, targetDatabaseName);

            var crmArg = collection.GetFirstArgument("crmname");
            if (crmArg == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You must provide an argument /crmname for the crm database name");
                Console.ResetColor();
                return false;
            }

            context.CrmDatabaseName = crmArg.Value.Trim();
            context.TargetDatabaseName = targetDatabaseName;
            context.ConnectionString = connectionString;
            context.DataSource = dataSource;
            return true;
        }
Example #8
0
 private static void ReadAppSettings(Context context)
 {
     context.ConfigFile = Path.Combine(Directory.GetCurrentDirectory(), AppConfig.Get<string>("SubstituteVariableFilePath"));
     context.ScriptsPath = Path.Combine(Directory.GetCurrentDirectory(), AppConfig.Get<string>("ScriptsPath"));
     context.AuxiliaryScriptsPath = Path.Combine(Directory.GetCurrentDirectory(), AppConfig.Get<string>("AuxiliaryScriptsPath"));
     context.ReadSubstitionVariablesFromConfigFile();
 }