/// <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; }