Example #1
0
        internal static void LoadFixtures(Task task)
        {
            DBEnvironments environment = task.Environment;
            string dbName = GetDatabaseName(environment);

            if (!IsExists(environment, dbName))
            {
                MessageOut("{0} not found", dbName);
                return;
            }

            // Create Connection String from Configuration File
            string sqlConStr = BuildConnectionString(environment, dbName);
            MessageOut("in {0}", sqlConStr);

            LoadFixtures(sqlConStr, task.FixtureTo);
        }
Example #2
0
        internal static void Migrate(Task task)
        {
            DBEnvironments environment = task.Environment;
            int migrateTo = task.MigrateTo;
            bool aspNET = task.AspNet;

            string dbName = GetDatabaseName(environment);

            if (!IsExists(environment, dbName))
            {
                MessageOut("{0} not found", dbName);
                return;
            }

            // Create Connection String from Configuration File
            string sqlConStr = BuildConnectionString(environment, dbName);
            MessageOut("in {0}", sqlConStr);

            // Update Schema version
            int? latestVersion = GetLatestSchemaVersion(sqlConStr);

            // Grant Permission
            if (!string.IsNullOrEmpty(task.GrantPermission)) GrantPermission(environment, task.GrantPermission);

            // Migrate 000 first
            if (!latestVersion.HasValue && aspNET) InstallMembership(environment);

            // Get all migration files
            var sw = new Stopwatch();
            string[] files = Directory.GetFiles(@"migrate", "*.sql");
            Array.Sort<string>(files);
            foreach (var sqlscript in files)
            {
                // ignore vim backup file
                if (sqlscript.EndsWith("~")) continue;

                string versionName = Path.GetFileNameWithoutExtension(sqlscript);
                int versionNumber = GetVersionNumber(versionName);
                if (latestVersion.HasValue && versionNumber <= latestVersion) continue;
                if (migrateTo != 0 && versionNumber > migrateTo) break;

                using (TransactionScope scope = new TransactionScope())
                {
                    MessageOut(@"Run script ------ {0} --------------------------", versionName);

                    sw.Reset();
                    sw.Start();

                    // Run Sql from file
                    SqlScriptHelper.ExecuteScriptFile(sqlscript, sqlConStr);

                    // Update Schema version
                    UpdateVersionSchema(sqlConStr, versionName);

                    sw.Stop();
                    MessageOut(@"Run script ------ {0} ({1}ms) ------ successfully.", versionName, sw.ElapsedMilliseconds);

                    scope.Complete();
                }
            }

            // Load Fixtures
            if (task.Fixture) LoadFixtures(sqlConStr);
        }
Example #3
0
        internal void Run(string[] args)
        {
            Task task = new Task();

            Parser parser = new Parser(System.Environment.CommandLine, task);

            // Add a switches with lots of aliases for the first name, "help" and "a".
            parser.AddSwitch(new string[] { "help", @"\?" }, "show help");

            // Parse the command line.
            parser.Parse();

            try
            {
                if (!string.IsNullOrEmpty(task.Xml))
                {
                    SqlDBHelper.Logger = new StreamWriter(task.Xml);
                    SqlDBHelper.WriteXmlHeader();
                }

                if (task.CreateDB)
                {
                    SqlDBHelper.CreateDB(task.Environment);
                }
                else if (task.ClearDB)
                {
                    SqlDBHelper.DeleteDB(task.Environment);
                }
                else if (task.Migrate)
                {
                    SqlDBHelper.Migrate(task);
                }
                else if (task.Version)
                {
                    SqlDBHelper.ViewVersion(task.Environment);
                }
                else if (task.Reset)
                {
                    SqlDBHelper.DeleteDB(task.Environment);
                    SqlDBHelper.CreateDB(task.Environment);
                    SqlDBHelper.Migrate(task);
                }
                else if (task.Fixture || !String.IsNullOrEmpty(task.FixtureTo))
                {
                    SqlDBHelper.LoadFixtures(task);
                }
                else if (!string.IsNullOrEmpty(task.GrantPermission))
                {
                    SqlDBHelper.GrantPermission(task.Environment, task.GrantPermission);
                }
                else
                    printUsage(parser);
            }
            catch (System.UnauthorizedAccessException uaex)
            {
                StringBuilder errorSb = new StringBuilder();
                errorSb.AppendLine(uaex.Message);
                errorSb.AppendLine(task.Trace ? uaex.StackTrace : "(See full trace by running ndb.exe with /trace)");
                errorSb.AppendLine("Try: attrib.exe -R migrate");
                errorSb.AppendLine("Try: attrib.exe -R fixtures");

                SqlDBHelper.MessageOut(errorSb.ToString());
            }
            catch (Microsoft.SqlServer.Management.Common.ExecutionFailureException smoEx)
            {
                StringBuilder errorSb = new StringBuilder();
                if (smoEx.InnerException != null)
                {
                    errorSb.AppendLine(smoEx.InnerException.HelpLink + smoEx.InnerException.Message);
                    errorSb.AppendLine(task.Trace ? smoEx.InnerException.StackTrace : "(See full trace by running ndb.exe with /trace)");
                }
                else
                {
                    errorSb.AppendLine(smoEx.Message);
                    errorSb.AppendLine(task.Trace ? smoEx.StackTrace : "(See full trace by running ndb.exe with /trace)");
                }

                SqlDBHelper.MessageOut(errorSb.ToString());
            }
            catch (SqlException dbEx)
            {
                StringBuilder errorSb = new StringBuilder();

                errorSb.AppendLine(dbEx.Message);

                if (task.Trace)
                {
                    foreach (SqlError err in dbEx.Errors)
                    {
                        errorSb.AppendLine(err.ToString());
                    }
                }
                else
                    errorSb.AppendLine("(See full trace by running ndb.exe with /trace)");

                SqlDBHelper.MessageOut(errorSb.ToString());
            }
            catch (Exception generalEx)
            {
                StringBuilder errorSb = new StringBuilder();

                errorSb.AppendLine(generalEx.GetType().ToString() + generalEx.Message);
                errorSb.AppendLine(task.Trace ? generalEx.StackTrace : "(See full trace by running ndb.exe with /trace)");

                SqlDBHelper.MessageOut(errorSb.ToString());
            }
            finally
            {
                if (!string.IsNullOrEmpty(task.Xml))
                {
                    SqlDBHelper.WriteXmlFooter();
                    SqlDBHelper.Logger.Flush();
                    SqlDBHelper.Logger.Close();
                }
            }

            // For error handling, were any switches handled?
            string[] unhandled = parser.UnhandledSwitches;
            if (unhandled != null && unhandled.Length > 0)
            {
                SqlDBHelper.MessageOut("\nThe following switches were not handled.");
                foreach (string s in unhandled)
                    SqlDBHelper.MessageOut(string.Format("  - {0}", s));
            }
        }