コード例 #1
0
ファイル: StatusCommand.cs プロジェクト: PaulStovell/bindable
        protected override void Execute(CommandExecutionContext context)
        {
            if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null)
            {
                ConnectionString = ConnectionStringBuilder.ToString();
            }
            if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null)
            {
                ConnectionString = "Server=(local);Database=BindableCMS;trusted_connection=yes;";
            }

            try
            {
                // Validate the connection string
                SqlDatabaseHelper.ValidateConnectionStringOrThrow(ConnectionString);

                // Ensure the database exists - if not, create it
                UltraConsole.WriteHeading("Database upgrade");
                UltraConsole.WriteParagraph("  Connection string: {0}", ConnectionString);
                UltraConsole.WriteLine("Ensuring database exists...");
                var manager = new ApplicationDatabase(ConnectionString);
                if (!manager.DoesDatabaseExist())
                {
                    UltraConsole.WriteLine("Database does not exist. ");
                    return;
                }
                UltraConsole.WriteLine("Database exists.");

                UltraConsole.WriteHeading("Getting information about current setup...");
                using (UltraConsole.Indent())
                {
                    UltraConsole.WriteColumns("Database version:", manager.GetCurrentVersion().ToString());
                    UltraConsole.WriteColumns("Can be upgraded to:", manager.GetApplicationVersion().ToString());
                }

                // Display the result
                UltraConsole.WriteLine(ConsoleColor.Green, "Success");
            }
            catch (FormatException ex)
            {
                UltraConsole.WriteLine(ConsoleColor.Red, "Connection string was invalid: {0}", ex.Message);
                return;
            }
        }
コード例 #2
0
        protected override void Execute(CommandExecutionContext context)
        {
            if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null)
            {
                ConnectionString = ConnectionStringBuilder.ToString();
            }
            if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null)
            {
                ConnectionString = "Server=(local);Database=BindableCMS;trusted_connection=yes;";
            }

            try
            {
                SqlDatabaseHelper.ValidateConnectionStringOrThrow(ConnectionString);

                UltraConsole.WriteHeading("Database upgrade");
                UltraConsole.WriteParagraph("  Connection string: {0}", ConnectionString);
                UltraConsole.WriteLine("Ensuring database exists...");
                var manager = new ApplicationDatabase(ConnectionString);
                if (!manager.DoesDatabaseExist())
                {
                    UltraConsole.WriteLine("Database does not exist. ");
                    if (!NoCreate)
                    {
                        UltraConsole.WriteLine("Creating database...");
                        manager.CreateDatabase();
                        UltraConsole.WriteLine("Database created");
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    UltraConsole.WriteLine("Database exists.");
                }

                UltraConsole.WriteHeading("Granting access to user '{0}'", UserAccount);
                manager.GrantAccessToLogin(UserAccount);

                UltraConsole.WriteHeading("Getting information about current setup...");
                using (UltraConsole.Indent())
                {
                    UltraConsole.WriteColumns("Current database version:", manager.GetCurrentVersion().ToString());
                    UltraConsole.WriteColumns("Will be upgraded to:", manager.GetApplicationVersion().ToString());
                }
                UltraConsole.WriteHeading("Performing upgrade...");
                var result = manager.PerformUpgrade();

                UltraConsole.WriteParagraph("Upgraded from {0} to {1}", result.OriginalVersion, result.UpgradedVersion);
                if (result.Scripts != null && result.Scripts.Count() > 0)
                {
                    UltraConsole.WriteHeading("The following scripts were executed:");
                    UltraConsole.WriteTable(
                        result.Scripts,
                        table => table.AddColumn("Version", script => script.VersionNumber)
                        );
                }

                if (result.Successful)
                {
                    UltraConsole.WriteLine(ConsoleColor.Green, "Database upgrade was successful.");
                }
                else
                {
                    UltraConsole.WriteLine(ConsoleColor.Red, "Database upgrade failed. Please see the list of scripts that were executed above and the error below.");
                    UltraConsole.WriteLine(result.Error.ToString());
                }
            }
            catch (FormatException ex)
            {
                UltraConsole.WriteLine(ConsoleColor.Red, "Connection string was invalid: {0}", ex.Message);
            }
        }