MigSharp.Process.MigrationBatch.Execute C# (CSharp) Method

Execute() public method

public Execute ( ) : void
return void
        public void Execute()
        {
            if (IsExecuted) throw new InvalidOperationException("Cannot execute the same batch twice.");
            IsExecuted = true;

            DateTime start = DateTime.Now;

            // validate all steps
            string errors;
            string warnings;
            _configuration.Validator.Validate(_migrations, out errors, out warnings);
            if (!string.IsNullOrEmpty(errors))
            {
                throw new InvalidOperationException("Cannot execute the migration(s) as there are validation errors:" + Environment.NewLine + errors);
            }
            if (!string.IsNullOrEmpty(warnings))
            {
                Log.Warning(warnings);
            }

            // execute all steps
            foreach (IMigrationStep step in _migrations)
            {
                ExecuteStep(step);
            }

            Debug.Assert(IsExecuted, "At the end of this method _isExecuted must be true.");

            Log.Info(LogCategory.Performance, "Migration and validation of batch took {0}s",
                (DateTime.Now - start).TotalSeconds);
        }

Usage Example

Example #1
0
        public void VerifyStepExecutedAndStepExecutingAreRaised()
        {
            var metadata = new StepMetadataStub(new MigrationMetadata(1, null, null));
            IMigrationStep step = FakeMigrationStep(metadata);
            IMigrationStep[] steps = { step };
            var batch = new MigrationBatch(steps, Enumerable.Empty<IMigrationMetadata>(), A.Fake<IVersioning>(), A.Fake<IRuntimeConfiguration>());

            Assert.AreSame(metadata, batch.Steps[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);
        }
All Usage Examples Of MigSharp.Process.MigrationBatch::Execute