Inheritance: DotNetMigrations.Core.DatabaseCommandArguments
        public void Constructor_should_default_TargetVersion_to_negative_1()
        {
            //  act
            var args = new MigrateCommandArgs();

            //  assert
            Assert.AreEqual(-1, args.TargetVersion);
        }
Example #2
0
        protected override void Execute(SeedCommandArgs args)
        {
            // migrate the schema first
            _migrateCommand.Log = this.Log;
            var migrateCmdArgs = new MigrateCommandArgs()
            {
                Connection = args.Connection
            };

            _migrateCommand.Run(migrateCmdArgs);

            // run the seed scripts
            _seedCommand.Log = this.Log;
            _seedCommand.Run(args);
        }
        /// <summary>
        /// Executes the Command's logic.
        /// </summary>
        protected override void Execute(DatabaseCommandArguments args)
        {
            long currentVersion  = GetDatabaseVersion();
            long previousVersion = GetPreviousDatabaseVersion(currentVersion);

            if (previousVersion == -1)
            {
                Log.WriteLine("No rollback is necessary. Database schema is already at version 0.");
                return;
            }

            _migrateCommand.Log = Log;

            var migrateCommandArgs = new MigrateCommandArgs();

            migrateCommandArgs.Connection    = args.Connection;
            migrateCommandArgs.TargetVersion = previousVersion;

            _migrateCommand.Run(migrateCommandArgs);
        }
        public void Setup()
        {
            _commandArgs = new MigrateCommandArgs();
            _commandArgs.Connection = TestConnectionString;

            _mockLog = new MockLog1();

            _mockMigrationScripts = new List<IMigrationScriptFile>();

            _mockMigrationDir = new Mock<IMigrationDirectory>();
            _mockMigrationDir.Setup(x => x.GetScripts()).Returns(() => _mockMigrationScripts);

            _migrateCommand = new MigrateCommand(_mockMigrationDir.Object);
            _migrateCommand.Log = _mockLog;

            //  setup the mock migration scripts
            var mockScript1 = new Mock<IMigrationScriptFile>();
            mockScript1.SetupGet(x => x.Version).Returns(1);
            mockScript1.Setup(x => x.Read()).Returns(() => new MigrationScriptContents(
                                                               @"CREATE TABLE [TestTable] (Id INT NOT NULL)
                                                                GO
                                                                INSERT INTO [TestTable] (Id) VALUES (1)",
                                                               @"DROP TABLE [TestTable]"));
            _mockMigrationScripts.Add(mockScript1.Object);

            var mockScript2 = new Mock<IMigrationScriptFile>();
            mockScript2.SetupGet(x => x.Version).Returns(2);
            mockScript2.Setup(x => x.Read()).Returns(() => new MigrationScriptContents(
                                                               "INSERT INTO [TestTable] (Id) VALUES (2)",
                                                               "DELETE FROM [TestTable] WHERE Id = 2"));
            _mockMigrationScripts.Add(mockScript2.Object);

            //  setup a migration script that throws an exception during Setup, but don't add it to the scripts collection
            _mockScriptWithBadSetup = new Mock<IMigrationScriptFile>();
            _mockScriptWithBadSetup.SetupGet(x => x.Version).Returns(3);
            _mockScriptWithBadSetup.SetupGet(x => x.FilePath).Returns("C:\\3_my_bad_script.sql");
            _mockScriptWithBadSetup.Setup(x => x.Read()).Returns(() => new MigrationScriptContents(
                                                               "INSERT INTO [NonExistantTable] (Id) VALUES (1)",
                                                               null));

            //  setup a migration script that throws an exception during Teardown, but don't add it to the scripts collection
            _mockScriptWithBadTeardown = new Mock<IMigrationScriptFile>();
            _mockScriptWithBadTeardown.SetupGet(x => x.Version).Returns(4);
            _mockScriptWithBadTeardown.SetupGet(x => x.FilePath).Returns("C:\\4_my_bad_script.sql");
            _mockScriptWithBadTeardown.Setup(x => x.Read()).Returns(() => new MigrationScriptContents(
                                                               "INSERT INTO [TestTable] (Id) VALUES (4)",
                                                               "DELETE FROM [NonExistantTable] WHERE Id = 4"));
            CreateDatabase();
        }