public void GetAllRegisteredMigrationVersions()
        {
            IMigrationVersionInfoManager migrationVersionInfoManager =
                new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");

            ExecuteInTransaction(delegate(IDbTransaction dbTransaction)
                {
                    Execute(dbTransaction, "create table SchemaInfo (Version bigint);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (1);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (2);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (4);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (7);");

                    IList<long> registeredMigrationVersions = 
                        migrationVersionInfoManager.GetRegisteredMigrationVersions(dbTransaction);
                    
                    Assert.AreEqual(4, registeredMigrationVersions.Count);
                    Assert.AreEqual(1, registeredMigrationVersions[0]);
                    Assert.AreEqual(2, registeredMigrationVersions[1]);
                    Assert.AreEqual(4, registeredMigrationVersions[2]);
                    Assert.AreEqual(7, registeredMigrationVersions[3]);

                    Execute(dbTransaction, "drop table SchemaInfo;");
                    
                    return null;
                });
        }
        public void GetCurrentMigrationVersionWithMissingTable()
        {
            IMigrationVersionInfoManager migrationVersionInfoManager =
                new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");

            long currentMigrationVersion = 
                MigrationVersionInfoManagerUtil.GetCurrentMigrationVersion(migrationVersionInfoManager, dbPlatform, connectionString);
            Assert.AreEqual(0, currentMigrationVersion);
        }
        public void GetRegisteredMigrationVersionsWithMissingTable()
        {
            IMigrationVersionInfoManager migrationVersionInfoManager =
                new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");

            IList<long> registeredMigrationVersions = 
                MigrationVersionInfoManagerUtil.GetRegisteredMigrationVersions(migrationVersionInfoManager, dbPlatform, connectionString);

            Assert.AreEqual(0, registeredMigrationVersions.Count);
        }
        public void TestFixtureSetUp()
        {
            dbPlatform = new SqlCePlatform();
            connectionString = ConfigurationManager.AppSettings["connectionString"];

            migrationVersionInfoManager = new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            migrationService = new MigrationService(
                dbPlatform,
                migrationVersionInfoManager,
                new DbMigrationScriptExecutive(new DbCommandExecutionStrategy()),
                new FileSystemNativeSqlResourceProvider(
                Path.Combine(
                    GetAssemblyLocation(Assembly.GetExecutingAssembly()), "Resources")));
        }
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            //
            // Load IDbPlatforms
            Type sourcePlatformType = Type.GetType(SourceDbPlatformType);
            Type targetPlatformType = String.IsNullOrEmpty(TargetDbPlatformType) ?
                sourcePlatformType :
                Type.GetType(TargetDbPlatformType);

            IDbPlatform sourceDbPlatform = (IDbPlatform)Activator.CreateInstance(sourcePlatformType, null);
            IDbPlatform targetDbPlatform = (IDbPlatform)Activator.CreateInstance(targetPlatformType, null);

            IMigrationVersionInfoManager sourceDbMigrationVersionInfoManager =
                new DbMigrationVersionInfoManager(sourceDbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            IMigrationVersionInfoManager targetDbMigrationVersionInfoManager =
                new DbMigrationVersionInfoManager(targetDbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            
            //
            // Source database version
            long sourceMigrationVersion = MigrationVersionInfoManagerUtil.GetCurrentMigrationVersion(
                sourceDbMigrationVersionInfoManager, sourceDbPlatform, SourceConnectionString);
            long targetMigrationVersion = MigrationVersionInfoManagerUtil.GetCurrentMigrationVersion(
                targetDbMigrationVersionInfoManager, targetDbPlatform, TargetConnectionString);

            if(targetMigrationVersion > sourceMigrationVersion && !AllowDowngrade)
            {
                if(BuildEngine != null)
                    Log.LogError("Could not downgrade from version {0} to version {1}. Review your migration definition or " +
                        "set 'AllowDowngrade' property to 'true'.", sourceMigrationVersion, targetMigrationVersion);
                return false;
            } // if

            IMigrationService migrationService =
                new MigrationService(targetDbPlatform, targetDbMigrationVersionInfoManager,
                    new DbMigrationScriptExecutive(new DbCommandExecutionStrategy()),
                    new FileSystemNativeSqlResourceProvider(Directory.GetCurrentDirectory()));
            migrationService.Migrated += delegate(object sender, MigrationEventArgs args)
                {
                    if(BuildEngine != null)
                        Log.LogMessage(MessageImportance.Normal, "Migrated to version {0}", args.Version);
                };

            using(StreamReader streamReader = new StreamReader(MigrationDefinitionPath))
                migrationService.Migrate(TargetConnectionString, sourceMigrationVersion, streamReader);

            return true;
        }
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            //
            // Load DbPlatform
            Type platformType = Type.GetType(DbPlatformType);
            IDbPlatform dbPlatform = (IDbPlatform)Activator.CreateInstance(platformType, null);

            DbMigrationVersionInfoManager migrationVersionInfoManager = 
                new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");
            long currentMigrationVersion = 
                MigrationVersionInfoManagerUtil.GetCurrentMigrationVersion(migrationVersionInfoManager, dbPlatform, ConnectionString);
            if(targetVersion.HasValue && targetVersion.Value < currentMigrationVersion && !AllowDowngrade)
            {
                if(BuildEngine != null)
                    Log.LogError("Could not downgrade from version {0} to version {1}. Review your migration definition or " +
                        "set 'AllowDowngrade' property to 'true'.", currentMigrationVersion, targetVersion.Value);
                return false;
            } // if

            IMigrationService migrationService = new MigrationService(dbPlatform,
                migrationVersionInfoManager,
                new DbMigrationScriptExecutive(new DbCommandExecutionStrategy()),
                new FileSystemNativeSqlResourceProvider(Directory.GetCurrentDirectory()));
            migrationService.Migrated += delegate(object sender, MigrationEventArgs args)
                {
                    if(BuildEngine != null)
                        Log.LogMessage(MessageImportance.Normal, "Migrated to version {0}", args.Version);
                };
            
            using(StreamReader streamReader = new StreamReader(MigrationDefinitionPath))
                migrationService.Migrate(ConnectionString, TargetVersion, streamReader);
            
            return true;
        }
        public void RegisterMigrationVersion()
        {
            IMigrationVersionInfoManager migrationVersionInfoManager =
                new DbMigrationVersionInfoManager(dbPlatform, new DbCommandExecutionStrategy(), "SchemaInfo");

            ExecuteInTransaction(delegate(IDbTransaction dbTransaction)
                {
                    Execute(dbTransaction, "create table SchemaInfo (Version bigint);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (1);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (2);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (4);");
                    Execute(dbTransaction, "insert into SchemaInfo (Version) values (7);");

                    migrationVersionInfoManager.RegisterMigrationVersion(dbTransaction, MigrationMode.Upgrade, 8);
                    Assert.AreEqual(8, migrationVersionInfoManager.GetCurrentMigrationVersion(dbTransaction));

                    migrationVersionInfoManager.RegisterMigrationVersion(dbTransaction, MigrationMode.Upgrade, 9);
                    Assert.AreEqual(9, migrationVersionInfoManager.GetCurrentMigrationVersion(dbTransaction));

                    migrationVersionInfoManager.RegisterMigrationVersion(dbTransaction, MigrationMode.Downgrade, 5);
                    Assert.AreEqual(9, migrationVersionInfoManager.GetCurrentMigrationVersion(dbTransaction));

                    migrationVersionInfoManager.RegisterMigrationVersion(dbTransaction, MigrationMode.Downgrade, 9);
                    Assert.AreEqual(8, migrationVersionInfoManager.GetCurrentMigrationVersion(dbTransaction));

                    migrationVersionInfoManager.RegisterMigrationVersion(dbTransaction, MigrationMode.Upgrade, 3);
                    Assert.AreEqual(8, migrationVersionInfoManager.GetCurrentMigrationVersion(dbTransaction));

                    Execute(dbTransaction, "drop table SchemaInfo;");
                    
                    return null;
                });
        }