Example #1
0
        private IMigrationBatch FetchMigrationsTo(ComposablePartCatalog catalog, long timestamp)
        {
            // import all migrations
            DateTime start = DateTime.Now;
            var      timestampProviders = InitializeTimestampProviders(catalog, _options.ModuleSelector);
            IEnumerable <ImportedMigration> availableMigrations = ImportAllMigrations(catalog, timestampProviders);

            Log.Verbose(LogCategory.Performance, "Importing migrations took {0}s", (DateTime.Now - start).TotalSeconds);

            // initialize command execution/scripting dispatching
            ISqlDispatcher dispatcher = new SqlDispatcher(_options.ScriptingOptions, _provider, _providerMetadata);

            // initialize versioning component and get executed migrations
            IVersioning versioning         = InitializeVersioning(catalog, dispatcher);
            var         executedMigrations = new List <IMigrationMetadata>(versioning.ExecutedMigrations);

            // create migration batch
            var migrationSelector = new MigrationSelector(availableMigrations, executedMigrations);
            IEnumerable <ApplicableMigration> applicableMigrations;
            IEnumerable <IMigrationMetadata>  unidentifiedMigrations;

            migrationSelector.GetMigrationsTo(timestamp, _options.ModuleSelector, out applicableMigrations, out unidentifiedMigrations);
            return(new MigrationBatch(
// ReSharper disable RedundantEnumerableCastCall
                       applicableMigrations.Select(m => new MigrationStep(m.Implementation, m.Metadata, _connectionInfo, _provider, _providerMetadata, _dbConnectionFactory, dispatcher)).Cast <IMigrationStep>(),
// ReSharper restore RedundantEnumerableCastCall
                       unidentifiedMigrations,
                       versioning,
                       _options));
        }
Example #2
0
        /// <summary>
        /// Executes the migration step and updates the versioning information in one transaction.
        /// </summary>
        public void Execute(IVersioning versioning)
        {
            if (versioning == null) throw new ArgumentNullException("versioning");

            DateTime start = DateTime.Now;

            using (IDbConnection connection = _connectionFactory.OpenConnection(_connectionInfo))
            {
                Debug.Assert(connection.State == ConnectionState.Open);

                using (IDbTransaction transaction = _connectionInfo.SupportsTransactions ? connection.BeginTransaction() : null)
                {
                    IDbCommandExecutor executor;
                    using ((executor = _sqlDispatcher.CreateExecutor(string.Format(CultureInfo.InvariantCulture, "Migration.{0}.{1}", _metadata.ModuleName, _metadata.Timestamp))) as IDisposable)
                    {
                        Execute(connection, transaction, _metadata.Direction, executor);

                        // update versioning
                        versioning.Update(_metadata, connection, transaction, executor);
                    }

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
            }

            Log.Verbose(LogCategory.Performance, "Migration of module '{0}' to {1}{2} took {3}s",
                _metadata.ModuleName,
                _metadata.Timestamp,
                !string.IsNullOrEmpty(_metadata.Tag) ? string.Format(CultureInfo.CurrentCulture, " '{0}'", _metadata.Tag) : string.Empty,
                (DateTime.Now - start).TotalSeconds);
        }
Example #3
0
        private IMigrationBatch FetchMigrationsTo(ContainerConfiguration containerConfiguration, long timestamp)
        {
            IVersioning versioning = InitializeVersioning(containerConfiguration);
            IDictionary <string, IMigrationTimestampProvider> timestampProviders = InitializeTimestampProviders(containerConfiguration);
            var importer      = new MigrationImporter(containerConfiguration, timestampProviders);
            var batchPreparer = new MigrationBatchPreparer(importer, versioning, Configuration);

            return(batchPreparer.Prepare(timestamp, _options));
        }
Example #4
0
 public MigrationBatch(
     IEnumerable<IMigrationStep> upMigrations,
     IEnumerable<IMigrationStep> downMigrations,
     IVersioning versioning,
     MigrationOptions options)
 {
     _upMigrations = upMigrations;
     _downMigrations = downMigrations;
     _versioning = versioning;
     _options = options;
 }
Example #5
0
 public MigrationBatch(
     IEnumerable <IMigrationStep> migrations,
     IEnumerable <IMigrationMetadata> unidentifiedMigrations,
     IVersioning versioning,
     IRuntimeConfiguration configuration)
 {
     _migrations             = migrations.ToList();
     _steps                  = new ReadOnlyCollection <IMigrationStepMetadata>(_migrations.Select(s => s.Metadata).ToList());
     _unidentifiedMigrations = new ReadOnlyCollection <IMigrationMetadata>(unidentifiedMigrations.ToList());
     _versioning             = versioning;
     _configuration          = configuration;
 }
Example #6
0
 public MigrationBatch(
     IEnumerable<IMigrationStep> migrations,
     IEnumerable<IMigrationMetadata> unidentifiedMigrations,
     IVersioning versioning,
     MigrationOptions options)
 {
     _migrations = migrations;
     _scheduledMigrations = new ReadOnlyCollection<IScheduledMigrationMetadata>(_migrations.Select(s => s.Metadata).ToList());
     _unidentifiedMigrations = new ReadOnlyCollection<IMigrationMetadata>(unidentifiedMigrations.ToList());
     _versioning = versioning;
     _options = options;
 }
Example #7
0
 public MigrationBatch(
     IEnumerable <IMigrationStep> migrations,
     IEnumerable <IMigrationMetadata> unidentifiedMigrations,
     IVersioning versioning,
     MigrationOptions options)
 {
     _migrations             = migrations;
     _scheduledMigrations    = new ReadOnlyCollection <IScheduledMigrationMetadata>(_migrations.Select(s => s.Metadata).ToList());
     _unidentifiedMigrations = new ReadOnlyCollection <IMigrationMetadata>(unidentifiedMigrations.ToList());
     _versioning             = versioning;
     _options = options;
 }
Example #8
0
 public MigrationBatch(
     IEnumerable<IMigrationStep> migrations,
     IEnumerable<IMigrationMetadata> unidentifiedMigrations,
     IVersioning versioning,
     IRuntimeConfiguration configuration)
 {
     _migrations = migrations.ToList();
     _steps = new ReadOnlyCollection<IMigrationStepMetadata>(_migrations.Select(s => s.Metadata).ToList());
     _unidentifiedMigrations = new ReadOnlyCollection<IMigrationMetadata>(unidentifiedMigrations.ToList());
     _versioning = versioning;
     _configuration = configuration;
 }
Example #9
0
        public void Execute(IRuntimeConfiguration configuration, IVersioning versioning)
        {
            if (versioning == null)
            {
                throw new ArgumentNullException("versioning");
            }

            DateTime start = DateTime.Now;

            long   timestamp = GetTimestamp();
            string tag       = GetTag();

            using (IDbConnection connection = configuration.OpenConnection())
            {
                Debug.Assert(connection.State == ConnectionState.Open);

                using (IDbTransaction transaction = configuration.ConnectionInfo.SupportsTransactions ? connection.BeginTransaction() : null)
                {
                    IDbCommandExecutor executor;
                    using ((executor = configuration.SqlDispatcher.CreateExecutor(string.Format(CultureInfo.InvariantCulture, "Migration.{0}.{1}", Metadata.ModuleName, timestamp))) as IDisposable)
                    {
                        try
                        {
                            Execute(configuration.ProviderInfo, connection, transaction, Metadata.Direction, executor);
                        }
                        catch
                        {
                            Log.Error("An non-recoverable error occurred in migration '{0}'{1}{2} while executing {3}.",
                                      timestamp,
                                      Metadata.ModuleName != MigrationExportAttribute.DefaultModuleName ? " in module '" + Metadata.ModuleName + "'" : string.Empty,
                                      tag,
                                      Metadata.Direction);
                            throw;
                        }

                        // update versioning
                        versioning.Update(Metadata, connection, transaction, executor);
                    }

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
            }

            Log.Verbose(LogCategory.Performance, "Migration of module '{0}' to {1}{2} took {3}s",
                        Metadata.ModuleName,
                        timestamp,
                        tag,
                        (DateTime.Now - start).TotalSeconds);
        }
Example #10
0
        /// <summary>
        /// Injects a custom version mechanism.
        /// </summary>
        public void UseCustomVersioning(IVersioning customVersioning)
        {
            if (customVersioning == null)
            {
                throw new ArgumentNullException("customVersioning");
            }
            if (_customBootstrapper != null)
            {
                throw new InvalidOperationException("Either use custom versioning or custom bootstrapping.");
            }

            _customVersioning = customVersioning;
        }
        public static void InterfaceWithInheritanceImplementationForIDisposable()
        {
            using (var managedPtr = new ManagedWrapper())
            {
                //Unmanaged code is passed managedPtr.Ptr
            }

            using (var derived = new DerivedManagedWrapper())
            {
                ExternalInterfaceImplementation eimi = new ExternalInterfaceImplementation();
                //Can NOT call eimi.Version.
                IVersioning iVer = eimi;
                Console.WriteLine(iVer.Version);
            }
        }
Example #12
0
        public void Execute(IRuntimeConfiguration configuration, IVersioning versioning)
        {
            if (versioning == null) throw new ArgumentNullException("versioning");

            DateTime start = DateTime.Now;

            long timestamp = GetTimestamp();
            string tag = GetTag();
            using (IDbConnection connection = configuration.OpenConnection())
            {
                Debug.Assert(connection.State == ConnectionState.Open);

                using (IDbTransaction transaction = configuration.ConnectionInfo.SupportsTransactions ? connection.BeginTransaction() : null)
                {
                    IDbCommandExecutor executor;
                    using ((executor = configuration.SqlDispatcher.CreateExecutor(string.Format(CultureInfo.InvariantCulture, "Migration.{0}.{1}", Metadata.ModuleName, timestamp))) as IDisposable)
                    {
                        try
                        {
                            Execute(configuration.ProviderInfo, connection, transaction, Metadata.Direction, executor);
                        }
                        catch
                        {
                            Log.Error("An non-recoverable error occurred in migration '{0}'{1}{2} while executing {3}.",
                                timestamp,
                                Metadata.ModuleName != MigrationExportAttribute.DefaultModuleName ? " in module '" + Metadata.ModuleName + "'" : string.Empty,
                                tag,
                                Metadata.Direction);
                            throw;
                        }

                        // update versioning
                        versioning.Update(Metadata, connection, transaction, executor);
                    }

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
            }

            Log.Verbose(LogCategory.Performance, "Migration of module '{0}' to {1}{2} took {3}s",
                Metadata.ModuleName,
                timestamp,
                tag,
                (DateTime.Now - start).TotalSeconds);
        }
Example #13
0
        public void VerifyValidationErrorsResultInException()
        {
            IMigrationStep step = MockRepository.GenerateStub <IMigrationStep>();

            step.Expect(s => s.Metadata).Return(new Metadata1());
            IMigrationReport erroneousReport = CreateMigrationReport();

            erroneousReport.Expect(r => r.Error).Return("Some test failure...");
            step.Expect(s => s.Report(null)).IgnoreArguments().Return(erroneousReport);
            IMigrationStep[] steps = new[]
            {
                step,
            };
            IVersioning    versioning = MockRepository.GenerateStub <IVersioning>();
            MigrationBatch batch      = new MigrationBatch(steps, Enumerable.Empty <IMigrationMetadata>(), versioning, new MigrationOptions());

            batch.Execute();
            Assert.IsTrue(batch.IsExecuted);
        }
Example #14
0
        /// <summary>
        /// Executes the migration step and updates the versioning information in one transaction.
        /// </summary>
        public void Execute(IVersioning versioning)
        {
            if (versioning == null)
            {
                throw new ArgumentNullException("versioning");
            }

            DateTime start = DateTime.Now;

            using (IDbConnection connection = _connectionFactory.OpenConnection(_connectionInfo))
            {
                Debug.Assert(connection.State == ConnectionState.Open);

                using (IDbTransaction transaction = _connectionInfo.SupportsTransactions ? connection.BeginTransaction() : null)
                {
                    IDbCommandExecutor executor;
                    using ((executor = _sqlDispatcher.CreateExecutor(string.Format(CultureInfo.InvariantCulture, "Migration.{0}.{1}", _metadata.ModuleName, _metadata.Timestamp))) as IDisposable)
                    {
                        Execute(connection, transaction, _metadata.Direction, executor);

                        // update versioning
                        versioning.Update(_metadata, connection, transaction, executor);
                    }

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
            }

            Log.Verbose(LogCategory.Performance, "Migration of module '{0}' to {1}{2} took {3}s",
                        _metadata.ModuleName,
                        _metadata.Timestamp,
                        !string.IsNullOrEmpty(_metadata.Tag) ? string.Format(CultureInfo.CurrentCulture, " '{0}'", _metadata.Tag) : string.Empty,
                        (DateTime.Now - start).TotalSeconds);
        }
Example #15
0
        public void VerifyStepExecutedAndStepExecutingAreRaised()
        {
            IMigrationStep step     = MockRepository.GenerateStub <IMigrationStep>();
            var            metadata = new Metadata1();

            step.Expect(s => s.Metadata).Return(metadata);
            step.Expect(s => s.Report(null)).IgnoreArguments().Return(CreateMigrationReport());
            IMigrationStep[] steps = new[]
            {
                step,
            };
            IVersioning versioning = MockRepository.GenerateStub <IVersioning>();
            var         batch      = new MigrationBatch(steps, Enumerable.Empty <IMigrationMetadata>(), versioning, new MigrationOptions());

            Assert.AreSame(metadata, batch.ScheduledMigrations[0], "The batch should expose the metadata of the step."); // this is tested to allow for the undocumented feature test below
            int countExecutingEvent = 0;
            int countExecutedEvent  = 0;

            batch.StepExecuting += (sender, args) =>
            {
                // note: the following assertion tests an undocumented feature
                Assert.AreSame(metadata, args.Metadata, "The event should carry the same metadata that is in the ScheduleMigrations collection.");
                countExecutingEvent++;
            };
            batch.StepExecuted += (sender, args) =>
            {
                // note: the following assertion tests an undocumented feature
                Assert.AreSame(metadata, args.Metadata, "The event should carry the same metadata that is in the ScheduleMigrations collection.");
                countExecutedEvent++;
            };

            batch.Execute();

            Assert.IsTrue(batch.IsExecuted);
            Assert.AreEqual(steps.Length, countExecutingEvent);
            Assert.AreEqual(steps.Length, countExecutedEvent);
        }
 public MigrationBatchPreparer(IMigrationImporter importer, IVersioning versioning, IRuntimeConfiguration configuration)
 {
     _importer = importer;
     _versioning = versioning;
     _configuration = configuration;
 }
Example #17
0
 public MigrationBatchPreparer(IMigrationImporter importer, IVersioning versioning, IRuntimeConfiguration configuration)
 {
     _importer      = importer;
     _versioning    = versioning;
     _configuration = configuration;
 }