Example #1
0
        public MigratorConsole(params string[] args)
        {
            consoleAnnouncer.Header();

            try
            {
                var optionSet = new OptionSet
                {
                    {
                        "assembly=|a=|target=",
                        "REQUIRED. The assembly containing the migrations you want to execute.",
                        v => { TargetAssembly = v; }
                    },
                    {
                        "provider=|dbType=|db=",
                        string.Format("REQUIRED. The kind of database you are migrating against. Available choices are: {0}.",
                                      new MigrationProcessorFactoryProvider().ListAvailableProcessorTypes()),
                        v => { ProcessorType = v; }
                    },
                    {
                        "connectionString=|connection=|conn=|c=",
                        "The name of the connection string (falls back to machine name) or the connection string itself to the server and database you want to execute your migrations against."
                        ,
                        v => { Connection = v; }
                    },
                    {
                        "connectionStringConfigPath=|configPath=",
                        string.Format("The path of the machine.config where the connection string named by connectionString" +
                                      " is found. If not specified, it defaults to the machine.config used by the currently running CLR version")
                        ,
                        v => { ConnectionStringConfigPath = v; }
                    },
                    {
                        "namespace=|ns=",
                        "The namespace contains the migrations you want to run. Default is all migrations found within the Target Assembly will be run."
                        ,
                        v => { Namespace = v; }
                    },
                    {
                        "nested",
                        "Whether migrations in nested namespaces should be included. Used in conjunction with the namespace option."
                        ,
                        v => { NestedNamespaces = true; }
                    },
                    {
                        "output|out|o",
                        "Output generated SQL to a file. Default is no output. Use outputFilename to control the filename, otherwise [assemblyname].sql is the default."
                        ,
                        v => { Output = true; }
                    },
                    {
                        "outputFilename=|outfile=|of=",
                        "The name of the file to output the generated SQL to. The output option must be included for output to be saved to the file."
                        ,
                        v => { OutputFilename = v; }
                    },
                    {
                        "preview|p",
                        "Only output the SQL generated by the migration - do not execute it. Default is false.",
                        v => { PreviewOnly = true; }
                    },
                    {
                        "steps=",
                        "The number of versions to rollback if the task is 'rollback'. Default is 1.",
                        v => { Steps = int.Parse(v); }
                    },
                    {
                        "task=|t=",
                        "The task you want FluentMigrator to perform. Available choices are: migrate:up, migrate (same as migrate:up), migrate:down, rollback, rollback:toversion, rollback:all, validateversionorder, listmigrations. Default is 'migrate'."
                        ,
                        v => { Task = v; }
                    },
                    {
                        "version=",
                        "The specific version to migrate. Default is 0, which will run all migrations.",
                        v => { Version = long.Parse(v); }
                    },
                    {
                        "verbose=",
                        "Show the SQL statements generated and execution time in the console. Default is false.",
                        v => { Verbose = true; }
                    },
                    {
                        "workingdirectory=|wd=",
                        "The directory to load SQL scripts specified by migrations from.",
                        v => { WorkingDirectory = v; }
                    },
                    {
                        "profile=",
                        "The profile to run after executing migrations.",
                        v => { Profile = v; }
                    },
                    {
                        "context=",
                        "Set ApplicationContext to the given string.",
                        v => { ApplicationContext = v; }
                    },
                    {
                        "timeout=",
                        "Overrides the default SqlCommand timeout of 30 seconds.",
                        v => { Timeout = int.Parse(v); }
                    },
                    {
                        "tag=",
                        "Filters the migrations to be run by tag.",
                        v => { Tags.Add(v); }
                    },
                    {
                        "help|h|?",
                        "Displays this help menu.",
                        v => { ShowHelp = true; }
                    }
                };

                try
                {
                    optionSet.Parse(args);
                }
                catch (OptionException e)
                {
                    consoleAnnouncer.Error(e.Message);
                    consoleAnnouncer.Say("Try 'migrate --help' for more information.");
                    return;
                }

                if (string.IsNullOrEmpty(Task))
                {
                    Task = "migrate";
                }

                if (string.IsNullOrEmpty(ProcessorType) ||
                    string.IsNullOrEmpty(TargetAssembly))
                {
                    DisplayHelp(optionSet);
                    Environment.ExitCode = 1;
                    return;
                }

                if (ShowHelp)
                {
                    DisplayHelp(optionSet);
                    return;
                }

                if (Output)
                {
                    if (string.IsNullOrEmpty(OutputFilename))
                    {
                        OutputFilename = TargetAssembly + ".sql";
                    }

                    ExecuteMigrations(OutputFilename);
                }
                else
                {
                    ExecuteMigrations();
                }
            }
            catch (Exception ex)
            {
                consoleAnnouncer.Error(ex.ToString());
                Environment.ExitCode = 1;
            }

            System.Console.ResetColor();
        }
        public int Run(params string[] args)
        {
            consoleAnnouncer.Header();

            try
            {
                var optionSet = new OptionSet
                {
                    {
                        "assembly=|a=|target=",
                        "REQUIRED. The assembly containing the migrations you want to execute.",
                        v => { TargetAssembly = v; }
                    },
                    {
                        "provider=|dbType=|db=",
                        string.Format("REQUIRED. The kind of database you are migrating against. Available choices are: {0}.",
                                      new MigrationProcessorFactoryProvider().ListAvailableProcessorTypes()),
                        v => { ProcessorType = v; }
                    },
                    {
                        "connectionString=|connection=|conn=|c=",
                        "The name of the connection string (falls back to machine name) or the connection string itself to the server and database you want to execute your migrations against."
                        ,
                        v => { Connection = v; }
                    },
                    {
                        "connectionStringConfigPath=|configPath=",
                        string.Format("The path of the machine.config where the connection string named by connectionString" +
                                      " is found. If not specified, it defaults to the machine.config used by the currently running CLR version")
                        ,
                        v => { ConnectionStringConfigPath = v; }
                    },
                    {
                        "namespace=|ns=",
                        "The namespace contains the migrations you want to run. Default is all migrations found within the Target Assembly will be run."
                        ,
                        v => { Namespace = v; }
                    },
                    {
                        "nested",
                        "Whether migrations in nested namespaces should be included. Used in conjunction with the namespace option."
                        ,
                        v => { NestedNamespaces = v != null; }
                    },
                    {
                        "output|out|o",
                        "Output generated SQL to a file. Default is no output. Use outputFilename to control the filename, otherwise [assemblyname].sql is the default."
                        ,
                        v => { Output = v != null; }
                    },
                    {
                        "outputFilename=|outfile=|of=",
                        "The name of the file to output the generated SQL to. The output option must be included for output to be saved to the file."
                        ,
                        v => { OutputFilename = v; }
                    },
                    {
                        "preview|p",
                        "Only output the SQL generated by the migration - do not execute it. Default is false.",
                        v => { PreviewOnly = v != null; }
                    },
                    {
                        "steps=",
                        "The number of versions to rollback if the task is 'rollback'. Default is 1.",
                        v => { Steps = int.Parse(v); }
                    },
                    {
                        "task=|t=",
                        "The task you want FluentMigrator to perform. Available choices are: migrate:up, migrate (same as migrate:up), migrate:down, rollback, rollback:toversion, rollback:all, validateversionorder, listmigrations. Default is 'migrate'."
                        ,
                        v => { Task = v; }
                    },
                    {
                        "version=",
                        "The specific version to migrate. Default is 0, which will run all migrations.",
                        v => { Version = long.Parse(v); }
                    },
                    {
                        "startVersion=",
                        "The specific version to start migrating from. Only used when NoConnection is true. Default is 0",
                        v => { StartVersion = long.Parse(v); }
                    },
                    {
                        "noConnection",
                        "Indicates that migrations will be generated without consulting a target database. Should only be used when generating an output file.",
                        v => { NoConnection = v != null; }
                    },
                    {
                        "verbose=",
                        "Show the SQL statements generated and execution time in the console. Default is false.",
                        v => { Verbose = v != null; }
                    },
                    {
                        "stopOnError=",
                        "Pauses migration execution until the user input if any error occured. Default is false.",
                        v => { StopOnError = v != null; }
                    },
                    {
                        "workingdirectory=|wd=",
                        "The directory to load SQL scripts specified by migrations from.",
                        v => { WorkingDirectory = v; }
                    },
                    {
                        "profile=",
                        "The profile to run after executing migrations.",
                        v => { Profile = v; }
                    },
                    {
                        "context=",
                        "Set ApplicationContext to the given string.",
                        v => { ApplicationContext = v; }
                    },
                    {
                        "timeout=",
                        "Overrides the default SqlCommand timeout of 30 seconds.",
                        v => { Timeout = int.Parse(v); }
                    },
                    {
                        "tag=",
                        "Filters the migrations to be run by tag.",
                        v => { Tags.Add(v); }
                    },
                    {
                        "providerswitches=",
                        "Provider specific switches",
                        v => { ProviderSwitches = v; }
                    },
                    {
                        "help|h|?",
                        "Displays this help menu.",
                        v => { ShowHelp = true; }
                    },
                    {
                        "transaction-per-session|tps",
                        "Overrides the transaction behavior of migrations, so that all migrations to be executed will run in one transaction.",
                        v => { TransactionPerSession = v != null; }
                    },
                    {
                        "allow-breaking-changes|abc",
                        "Allows execution of migrations marked as breaking changes.",
                        v => { AllowBreakingChange = v != null; }
                    },
                };

                try
                {
                    optionSet.Parse(args);
                }
                catch (OptionException e)
                {
                    consoleAnnouncer.Error(e);
                    consoleAnnouncer.Say("Try 'migrate --help' for more information.");
                    return(2);
                }

                if (string.IsNullOrEmpty(Task))
                {
                    Task = "migrate";
                }

                if (!ValidateArguments(optionSet))
                {
                    return(1);
                }

                if (ShowHelp)
                {
                    DisplayHelp(optionSet);
                    return(0);
                }

                if (Output)
                {
                    return(ExecuteMigrations(OutputFilename));
                }

                return(ExecuteMigrations());
            }
            catch (MissingMigrationsException ex)
            {
                consoleAnnouncer.Error(ex);
                return(6);
            }
            catch (RunnerException ex)
            {
                consoleAnnouncer.Error(ex);
                return(5);
            }
            catch (FluentMigratorException ex)
            {
                consoleAnnouncer.Error(ex);
                return(4);
            }
            catch (Exception ex)
            {
                consoleAnnouncer.Error(ex);
                return(3);
            }
        }