Ejemplo n.º 1
0
        public NopConventionSet(INopDataProvider dataProvider)
        {
            if (dataProvider is null)
            {
                throw new ArgumentNullException(nameof(dataProvider));
            }

            var defaultConventionSet = new DefaultConventionSet();

            ForeignKeyConventions = new List <IForeignKeyConvention>()
            {
                new NopForeignKeyConvention(dataProvider),
                defaultConventionSet.SchemaConvention,
            };

            IndexConventions = new List <IIndexConvention>()
            {
                new NopIndexConvention(dataProvider),
                defaultConventionSet.SchemaConvention
            };

            ColumnsConventions = new List <IColumnsConvention>()
            {
                new NopColumnsConvention(),
                new DefaultPrimaryKeyNameConvention()
            };

            ConstraintConventions = defaultConventionSet.ConstraintConventions;

            SequenceConventions = defaultConventionSet.SequenceConventions;
            AutoNameConventions = defaultConventionSet.AutoNameConventions;
            SchemaConvention    = defaultConventionSet.SchemaConvention;
            RootPathConvention  = defaultConventionSet.RootPathConvention;
        }
        private void RunMigrations(string databaseKey)
        {
            var cs = SqlConnections.TryGetConnectionString(databaseKey);

            if (cs == null)
            {
                throw new ArgumentOutOfRangeException(nameof(databaseKey));
            }


            string serverType = cs.Dialect.ServerType;

            bool isOracle   = serverType.StartsWith("Oracle", StringComparison.OrdinalIgnoreCase);
            bool isFirebird = serverType.StartsWith("Firebird", StringComparison.OrdinalIgnoreCase);

            // safety check to ensure that we are not modifying an arbitrary database.
            // remove these lines if you want Serene1 migrations to run on your DB.
            if (!isOracle && cs.ConnectionString.IndexOf(typeof(DataMigrations).Namespace +
                                                         @"_" + databaseKey + "_v1", StringComparison.OrdinalIgnoreCase) < 0)
            {
                SkippedMigrations = true;
                return;
            }

            string databaseType = isOracle ? "OracleManaged" : serverType;

            var conventionSet = new DefaultConventionSet(defaultSchemaName: null,
                                                         Path.GetDirectoryName(typeof(DataMigrations).Assembly.Location));

            var serviceProvider = new ServiceCollection()
                                  .AddLogging(lb => lb.AddFluentMigratorConsole())
                                  .AddFluentMigratorCore()
                                  .AddSingleton <IConventionSet>(conventionSet)
                                  .Configure <TypeFilterOptions>(options =>
            {
                options.Namespace = "Serene1.Migrations." + databaseKey + "DB";
            })
                                  .Configure <ProcessorOptions>(options =>
            {
                options.Timeout = TimeSpan.FromSeconds(90);
            })
                                  .ConfigureRunner(builder =>
            {
                if (databaseType == OracleDialect.Instance.ServerType)
                {
                    builder.AddOracleManaged();
                }
                else if (databaseType == SqliteDialect.Instance.ServerType)
                {
                    builder.AddSQLite();
                }
                else if (databaseType == FirebirdDialect.Instance.ServerType)
                {
                    builder.AddFirebird();
                }
                else if (databaseType == MySqlDialect.Instance.ServerType)
                {
                    builder.AddMySql5();
                }
                else if (databaseType == PostgresDialect.Instance.ServerType)
                {
                    builder.AddPostgres();
                }
                else
                {
                    builder.AddSqlServer();
                }

                builder.WithGlobalConnectionString(cs.ConnectionString);
                builder.WithMigrationsIn(typeof(DataMigrations).Assembly);
            })
                                  .BuildServiceProvider();

            var culture = CultureInfo.CurrentCulture;

            try
            {
                if (isFirebird)
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                }

                using var scope = serviceProvider.CreateScope();
                var runner = scope.ServiceProvider.GetRequiredService <IMigrationRunner>();
                runner.MigrateUp();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Error executing migration!", ex);
            }
            finally
            {
                if (isFirebird)
                {
                    Thread.CurrentThread.CurrentCulture = culture;
                }
            }
        }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        private int ExecuteMigrations()
        {
            var conventionSet = new DefaultConventionSet(defaultSchemaName: null, WorkingDirectory);

            var services = CreateCoreServices()
                           .Configure <FluentMigratorLoggerOptions>(
                opt =>
            {
                opt.ShowElapsedTime = Verbose;
                opt.ShowSql         = Verbose;
            })
                           .AddSingleton <IConventionSet>(conventionSet)
                           .Configure <SelectingProcessorAccessorOptions>(opt => opt.ProcessorId = ProcessorType)
                           .Configure <AssemblySourceOptions>(opt => opt.AssemblyNames           = new[] { TargetAssembly })
#pragma warning disable 612
                           .Configure <AppConfigConnectionStringAccessorOptions>(
                opt => opt.ConnectionStringConfigPath = ConnectionStringConfigPath)
#pragma warning restore 612
                           .Configure <TypeFilterOptions>(
                opt =>
            {
                opt.Namespace        = Namespace;
                opt.NestedNamespaces = NestedNamespaces;
            })
                           .Configure <RunnerOptions>(
                opt =>
            {
                opt.Task         = Task;
                opt.Version      = Version;
                opt.StartVersion = StartVersion;
                opt.NoConnection = NoConnection;
                opt.Steps        = Steps;
                opt.Profile      = Profile;
                opt.Tags         = Tags.ToArray();
#pragma warning disable 612
                opt.ApplicationContext = ApplicationContext;
#pragma warning restore 612
                opt.TransactionPerSession = TransactionPerSession;
                opt.AllowBreakingChange   = AllowBreakingChange;
            })
                           .Configure <ProcessorOptions>(
                opt =>
            {
                opt.ConnectionString = Connection;
                opt.PreviewOnly      = PreviewOnly;
                opt.ProviderSwitches = ProviderSwitches;
                opt.Timeout          = Timeout == null ? null : (TimeSpan?)TimeSpan.FromSeconds(Timeout.Value);
            });

            if (StopOnError)
            {
                services
                .AddSingleton <ILoggerProvider, StopOnErrorLoggerProvider>();
            }
            else
            {
                services
                .AddSingleton <ILoggerProvider, FluentMigratorConsoleLoggerProvider>();
            }

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

            using (var serviceProvider = services.BuildServiceProvider(validateScopes: false))
            {
                var executor = serviceProvider.GetRequiredService <TaskExecutor>();
                executor.Execute();
            }

            return(0);
        }