示例#1
0
        public static MigratorOptions CreateListMigrations(ListMigrations cmd)
        {
            var result = new MigratorOptions("listmigrations")
                         .Init(cmd);

            return(result);
        }
示例#2
0
        private IRunnerContext CreateRunnerContext(IServiceProvider serviceProvider, MigratorOptions options)
        {
            var announcer = serviceProvider.GetRequiredService <IAnnouncer>();

            return(new RunnerContext(announcer)
            {
                Task = options.Task,
                Connection = options.ConnectionString,
                ConnectionStringConfigPath = options.ConnectionStringConfigPath,
                Database = options.ProcessorType,
                ProviderSwitches = options.ProcessorSwitches,
                NoConnection = options.NoConnection || string.IsNullOrEmpty(options.ConnectionString),
                Version = options.TargetVersion ?? 0,
                Targets = options.TargetAssemblies.ToArray(),
                Steps = options.Steps ?? 1,
                Namespace = options.Namespace,
                NestedNamespaces = options.NestedNamespaces,
                StartVersion = options.StartVersion ?? 0,
                WorkingDirectory = options.WorkingDirectory,
                Tags = options.Tags.ToList(),
                PreviewOnly = options.Preview,
                Profile = options.Profile,
                ApplicationContext = options.Context,
                Timeout = options.Timeout,
                TransactionPerSession = options.TransactionMode == TransactionMode.Session,
            });
        }
        public static IServiceProvider BuildServiceProvider(MigratorOptions options, IConsole console)
        {
            var serviceCollection = new ServiceCollection();
            var serviceProvider   = ConfigureServices(serviceCollection, options, console);

            Configure(serviceProvider.GetRequiredService <ILoggerFactory>());
            return(serviceProvider);
        }
示例#4
0
        public static MigratorOptions CreateRollbackAll(RollbackAll cmd)
        {
            var result = new MigratorOptions("rollback:all")
                         .Init(cmd.Parent);

            result.TransactionMode = cmd.Parent.TransactionMode;
            return(result);
        }
示例#5
0
        private IAnnouncer CreateAnnouncer(IServiceProvider serviceProvider, MigratorOptions options)
        {
            var loggingAnnouncer  = serviceProvider.GetRequiredService <LoggingAnnouncer>();
            var consoleAnnouncer  = serviceProvider.GetRequiredService <ParserConsoleAnnouncer>();
            var lateInitAnnouncer = serviceProvider.GetRequiredService <LateInitAnnouncer>();

            return(new CompositeAnnouncer(loggingAnnouncer, consoleAnnouncer, lateInitAnnouncer));
        }
示例#6
0
        public static MigratorOptions CreateRollbackTo(RollbackTo cmd)
        {
            var result = new MigratorOptions("rollback:toversion")
                         .Init(cmd.Parent);

            result.TargetVersion   = cmd.Version;
            result.TransactionMode = cmd.Parent.TransactionMode;
            return(result);
        }
示例#7
0
        public static MigratorOptions CreateRollbackBy(Rollback cmd, int?steps)
        {
            var result = new MigratorOptions("rollback")
                         .Init(cmd);

            result.Steps           = steps;
            result.TransactionMode = cmd.TransactionMode;
            return(result);
        }
示例#8
0
        public static MigratorOptions CreateMigrateDown(MigrateDown cmd)
        {
            var result = new MigratorOptions("migrate:down")
                         .Init(cmd.Parent);

            result.TargetVersion   = cmd.TargetVersion;
            result.TransactionMode = cmd.Parent.TransactionMode;
            return(result);
        }
示例#9
0
        public static MigratorOptions CreateMigrateUp(Migrate cmd, long?targetVersion = null)
        {
            var result = new MigratorOptions("migrate:up")
                         .Init(cmd);

            result.TargetVersion   = targetVersion;
            result.TransactionMode = cmd.TransactionMode;
            return(result);
        }
示例#10
0
        private IServiceProvider ConfigureServices(IServiceCollection services, MigratorOptions options, IConsole console)
        {
            var mapper = ConfigureMapper();

            services
            .AddLogging()
            .AddOptions()
            .AddSingleton(mapper)
            .AddProcessorFactory <Db2ProcessorFactory>()
            .AddProcessorFactory <DotConnectOracleProcessorFactory>()
            .AddProcessorFactory <FirebirdProcessorFactory>()
            .AddProcessorFactory <MySql4ProcessorFactory>()
            .AddProcessorFactory <MySql5ProcessorFactory>()
            .AddProcessorFactory <OracleManagedProcessorFactory>()
            .AddProcessorFactory <OracleProcessorFactory>()
            .AddProcessorFactory <PostgresProcessorFactory>()
            .AddProcessorFactory <SQLiteProcessorFactory>()
            .AddProcessorFactory <SqlServer2000ProcessorFactory>()
            .AddProcessorFactory <SqlServer2005ProcessorFactory>()
            .AddProcessorFactory <SqlServer2008ProcessorFactory>()
            .AddProcessorFactory <SqlServer2012ProcessorFactory>()
            .AddProcessorFactory <SqlServer2014ProcessorFactory>()
            .AddProcessorFactory <SqlServerProcessorFactory>()
            .AddProcessorFactory <SqlServerCeProcessorFactory>();
            services
            .Configure <MigratorOptions>(mc => mapper.Map(options, mc));
            services
            .Configure <CustomAnnouncerOptions>(
                cao =>
            {
                cao.ShowElapsedTime = options.Verbose;
                cao.ShowSql         = options.Verbose;
            });
            services
            .AddSingleton(console)
            .AddSingleton <LateInitAnnouncer>()
            .AddSingleton <LoggingAnnouncer>()
            .AddSingleton <ParserConsoleAnnouncer>()
            .AddSingleton(sp => CreateAnnouncer(sp, options));
            services
            .AddSingleton(sp => CreateRunnerContext(sp, options));
            services
            .AddTransient <IStopWatch, StopWatch>();
            services
            .AddTransient <TaskExecutor, LateInitTaskExecutor>();
            return(services.BuildServiceProvider());
        }
        private static IServiceProvider ConfigureServices(IServiceCollection services, MigratorOptions options, IConsole console)
        {
            var conventionSet = new DefaultConventionSet(defaultSchemaName: options.SchemaName, options.WorkingDirectory);

            var targetIsSqlServer = !string.IsNullOrEmpty(options.ProcessorType) &&
                                    options.ProcessorType.StartsWith("sqlserver", StringComparison.OrdinalIgnoreCase);

            var mapper = ConfigureMapper();

            services
            .AddLogging(lb => lb.AddFluentMigratorConsole())
            .AddOptions()
            .AddSingleton(mapper);

            if (options.Output)
            {
                services
                .AddSingleton <ILoggerProvider, LogFileFluentMigratorLoggerProvider>()
                .Configure <LogFileFluentMigratorLoggerOptions>(
                    opt =>
                {
                    opt.OutputFileName            = options.OutputFileName;
                    opt.OutputGoBetweenStatements = targetIsSqlServer;
                    opt.ShowSql = true;
                });
            }

            services
            .AddFluentMigratorCore()
            .ConfigureRunner(
                builder => builder
                .AddDb2()
                .AddDb2ISeries()
                .AddDotConnectOracle()
                .AddDotConnectOracle12C()
                .AddFirebird()
                .AddHana()
                .AddMySql4()
                .AddMySql5()
                .AddOracle()
                .AddOracle12C()
                .AddOracleManaged()
                .AddOracle12CManaged()
                .AddPostgres()
                .AddPostgres92()
                .AddPostgres10_0()
                .AddPostgres11_0()
                .AddRedshift()
                .AddSqlAnywhere()
                .AddSQLite()
                .AddSqlServer()
                .AddSqlServer2000()
                .AddSqlServer2005()
                .AddSqlServer2008()
                .AddSqlServer2012()
                .AddSqlServer2014()
                .AddSqlServer2016()
                .AddSqlServerCe());

            services
            .AddSingleton <IConventionSet>(conventionSet)
            .Configure <SelectingProcessorAccessorOptions>(opt => opt.ProcessorId = options.ProcessorType)
            .Configure <AssemblySourceOptions>(opt => opt.AssemblyNames           = options.TargetAssemblies.ToArray())
            .Configure <TypeFilterOptions>(
                opt =>
            {
                opt.Namespace        = options.Namespace;
                opt.NestedNamespaces = options.NestedNamespaces;
            })
            .Configure <RunnerOptions>(
                opt =>
            {
                opt.Task         = options.Task;
                opt.Version      = options.TargetVersion ?? 0;
                opt.StartVersion = options.StartVersion ?? 0;
                opt.NoConnection = options.NoConnection;
                opt.Steps        = options.Steps ?? 1;
                opt.Profile      = options.Profile;
                opt.Tags         = options.Tags.ToArray();
#pragma warning disable 612
                opt.ApplicationContext = options.Context;
#pragma warning restore 612
                opt.TransactionPerSession       = options.TransactionMode == TransactionMode.Session;
                opt.AllowBreakingChange         = options.AllowBreakingChanges;
                opt.IncludeUntaggedMigrations   = options.IncludeUntaggedMigrations;
                opt.IncludeUntaggedMaintenances = options.IncludeUntaggedMaintenances;
            })
            .Configure <ProcessorOptions>(
                opt =>
            {
                opt.ConnectionString = options.ConnectionString;
                opt.PreviewOnly      = options.Preview;
                opt.ProviderSwitches = options.ProcessorSwitches;
                opt.StripComments    = options.StripComments;
                opt.Timeout          = options.Timeout == null ? null : (TimeSpan?)TimeSpan.FromSeconds(options.Timeout.Value);
            });

            services
            .Configure <MigratorOptions>(mc => mapper.Map(options, mc));

            services
            .Configure <FluentMigratorLoggerOptions>(
                opt =>
            {
                opt.ShowElapsedTime = options.Verbose;
                opt.ShowSql         = options.Verbose;
            });

            services
            .AddSingleton(console);

            return(services.BuildServiceProvider());
        }