Esempio n. 1
0
        private static int GenrateDatabaseScript(Options options, StringBuilder sbBuilder)
        {
            try
            {
                var scriptor =
                    DbConfigurationManagerHelper.CreateDbMigrationScriptingDecorator(
                        options.TargetDataBaseConnectioString, options.DllPath, options.ConfigurationClassName);
                var sourceMigration = string.Empty;
                var targetMigration = string.Empty;

                if (options.TargetMigration != null)
                {
                    if (options.TargetMigration.Equals("null", StringComparison.InvariantCultureIgnoreCase) ||
                        string.IsNullOrEmpty(options.TargetMigration))
                    {
                        targetMigration = null;
                    }
                    else
                    {
                        targetMigration = options.TargetMigration;
                    }
                }
                if (options.SourceMigration != null)
                {
                    if (options.SourceMigration.Equals("null", StringComparison.InvariantCultureIgnoreCase) ||
                        string.IsNullOrEmpty(options.SourceMigration))
                    {
                        sourceMigration = null;
                    }
                    else
                    {
                        sourceMigration = options.SourceMigration;
                    }
                }
                string script = scriptor.ScriptUpdate(sourceMigration: sourceMigration, targetMigration: targetMigration);
                if (script.Length > 0)
                {
                    sbBuilder.AppendLine("Creating script from migration saving the file at." + FilePath);
                    Console.WriteLine("Creating script from migration saving the file at." + FilePath);
                    Console.WriteLine(script);
                }
                sbBuilder.AppendLine(
                    "--------------------------Script Generated ---------------------------------------------");
                sbBuilder.AppendLine(script);
                sbBuilder.AppendLine(
                    "--------------------------Script End ---------------------------------------------");
            }
            catch (Exception t)
            {
                System.IO.File.WriteAllText(FilePath, sbBuilder.ToString());
                return(-1);
            }
            return(0);
        }
Esempio n. 2
0
        //public const string FullPath = DirPath + "\\" + FilePath;
        static int Main(string[] args)
        {
            var stringBulider = new StringBuilder();

            try
            {
                var lastDbMigration = string.Empty;

                stringBulider.AppendLine(
                    "-----------------------------------------------------------------------------------------------------------------------------");
                stringBulider.AppendLine(
                    "========================================================== DataBase Deployment ===============================================");
                var options = new Options();
                if (CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    // Values are available here
                    if (options.Verbose)
                    {
                        if (ParseCommandLineParameters(options, stringBulider).IsSuccess)
                        {
                            var migrator = DbConfigurationManagerHelper.CreateDbMigration(options.TargetDataBaseConnectioString, options.DllPath, options.ConfigurationClassName);
                            if (migrator != null)
                            {
                                lastDbMigration = migrator.GetDatabaseMigrations().Take(1).FirstOrDefault();
                                stringBulider.AppendLine("Last Migration Applied To Database:" + lastDbMigration);

                                stringBulider.AppendLine("Migration which are pending on current database");

                                foreach (string migration in migrator.GetPendingMigrations())
                                {
                                    stringBulider.AppendLine(migration);
                                    Console.WriteLine(migration);
                                }
                                GenrateDatabaseScript(options, stringBulider);
                                UpdateDatabase(lastDbMigration, options, migrator, stringBulider);
                            }
                        }
                    }
                }
            }
            catch (Exception t)
            {
                stringBulider.AppendLine("Error in Update DataBase." + t.InnerException + " Stackrace:" + t.StackTrace);
                System.IO.File.WriteAllText(FilePath, stringBulider.ToString());
                return(-1);
            }


            System.IO.File.WriteAllText(FilePath, stringBulider.ToString());

            return(0);
        }