/// <summary> /// Deletes the record from the migration table. /// </summary> public virtual void Delete() { var migrationRecord = MigrationRecord.Find(this.Uuid); if (migrationRecord == null) { throw new InvalidDataException(string.Format("The migration {0} cannot be deleted from the migration table.", this.Name)); } migrationRecord.Delete(); }
/// <summary> /// Rolls back all found migrations to a specified version. /// </summary> /// <param name="version">The version number up to which to roll back to.</param> /// <param name="hard">If true, ignores thrown errors and powers through the migration reverse.</param> public static void ResetTo(Version version, bool hard = false) { // Get all migrations in reverse var migrations = GetAllMigrationsReverse(); var writtenMigrations = new List <MigrationRecord>(); // Read migrations table to get all var template = new MigrationRecord(); if (DatabaseSession.Instance.Connector.CheckTableExists(template)) { writtenMigrations = DatabaseSession.Instance.Connector.ReadRecords <MigrationRecord>(); } // leave only the migrations which are above the specified version migrations = migrations.Where(m => m.Version > version).OrderByDescending(x => x.FullName).ToList(); Logger.Info($"Resetting to database version {version}..."); // Perform peice by peice rollback foreach (var migration in migrations) { // perform the necessary check whether the migration should be rolled back. Only roll back if it has been applied. if (writtenMigrations.Any(m => m.Uuid == migration.Uuid)) { // perform the migration script try { Logger.Info($"Rolling back migration {migration.Name}..."); migration.Reverse(); migration.Delete(); Logger.Info("Done"); } catch (Exception ex) { if (!hard) { throw new Exception(string.Format("Migration rollback of {1} failed: {0}", ex.Message, migration.Name)); } } } } }
/// <summary> /// Rolls back all found migrations /// </summary> /// <param name="name">The name of the single rollback to perform. Must be the Name parameter of the <see cref="IMigration"/></param> /// <param name="hard">If true, ignores thrown errors and powers through the migration reverse.</param> public static void Reset(string name = null, bool hard = false) { // Get all migrations in reverse var migrations = GetAllMigrationsReverse(); var writtenMigrations = new List <MigrationRecord>(); // Read migrations table to get all var template = new MigrationRecord(); if (DatabaseSession.Instance.Connector.CheckTableExists(template)) { writtenMigrations = DatabaseSession.Instance.Connector.ReadRecords <MigrationRecord>(); } if (!string.IsNullOrEmpty(name)) { migrations = migrations.Where(m => m.Name == name).ToList(); } // Perform peice by peice rollback foreach (var migration in migrations) { // perform the necessary check whether the migration should be rolled back. Only roll back if it has been applied. if (writtenMigrations.Any(m => m.Uuid == migration.Uuid)) { // perform the migration script try { Logger.Info($"Executing reverse on migration {migration.Name}..."); migration.Reverse(); migration.Delete(); Logger.Info("Done"); } catch (Exception ex) { if (!hard) { throw new Exception(string.Format("Migration rollback of {1} failed: {0}", ex.Message, migration.Name)); } } } } }
/// <summary> /// Checks whether the migration should execute. /// </summary> /// <returns>True if the migration should run.</returns> public virtual bool ShouldMigrate() { return(MigrationRecord.Find(this.Uuid) == null); }
/// <summary> /// Saves the migration information to the migration table /// </summary> public virtual void Save() { var migration = new MigrationRecord(this); migration.Save(); }