Class SchemaHistory


  • public abstract class SchemaHistory
    extends Object
    The schema history used to track all applied migrations.
    • Field Detail

      • table

        protected Table table
        The schema history table used by MigrateDB. Non-final due to the table name fallback mechanism.
    • Constructor Detail

      • SchemaHistory

        public SchemaHistory()
    • Method Detail

      • getTable

        public Table getTable()
        Returns:
        The schema history table.
      • withLock

        public abstract <T> T withLock​(Callable<T> callable)
        Acquires an exclusive read-write lock on the schema history table. This lock will be released automatically upon completion.
        Returns:
        The result of the action.
      • exists

        public abstract boolean exists()
        Returns:
        Whether the schema history table exists.
      • create

        public abstract void create​(boolean baseline)
        Creates the schema history. Do nothing if it already exists.
        Parameters:
        baseline - Whether to include the creation of a baseline marker.
      • hasAppliedMigrations

        public final boolean hasAppliedMigrations()
        Checks whether the schema history table contains at least one applied migration, not counting markers for schema creation or baselining.
        Returns:
        true if it does, false if it doesn't.
      • allAppliedMigrations

        public abstract List<AppliedMigration> allAppliedMigrations()
        Returns:
        The list of all migrations applied on the schema in the order they were applied (oldest first). An empty list if no migration has been applied so far.
      • getBaselineMarker

        public final AppliedMigration getBaselineMarker()
        Retrieves the baseline marker from the schema history table.
        Returns:
        The baseline marker or null if none could be found.
      • removeFailedMigrations

        public abstract boolean removeFailedMigrations​(RepairResult repairResult,
                                                       List<MigrationPattern> migrationPatternFilter)

        Repairs the schema history table after a failed migration. This is only necessary for databases without DDL-transaction support.

        On databases with DDL transaction support, a migration failure automatically triggers a rollback of all changes, including the ones in the schema history table.

        Parameters:
        repairResult - The result object containing which failed migrations were removed.
        migrationPatternFilter - The migration patterns to filter by.
      • addSchemasMarker

        public final void addSchemasMarker​(Schema[] schemas)
        Indicates in the schema history table that MigrateDb created these schemas.
        Parameters:
        schemas - The schemas that were created by MigrateDB.
      • hasSchemasMarker

        public final boolean hasSchemasMarker()
        Checks whether the schema history table contains a marker row for schema creation.
        Returns:
        true if it does, false if it doesn't.
      • getSchemasCreatedByMigrateDb

        public List<String> getSchemasCreatedByMigrateDb()
      • update

        public abstract void update​(AppliedMigration appliedMigration,
                                    ResolvedMigration resolvedMigration)
        Updates this applied migration to match this resolved migration.
        Parameters:
        appliedMigration - The applied migration to update.
        resolvedMigration - The resolved migration to source the new values from.
      • delete

        public abstract void delete​(AppliedMigration appliedMigration)
        Update the schema history to mark this migration as DELETED
        Parameters:
        appliedMigration - The applied migration to mark as DELETED
      • clearCache

        public void clearCache()
        Clears the applied migration cache.
      • addAppliedMigration

        public final void addAppliedMigration​(Version version,
                                              String description,
                                              MigrationType type,
                                              String script,
                                              @Nullable Checksum checksum,
                                              int executionTime,
                                              boolean success)
        Records a new applied migration.
        Parameters:
        version - The target version of this migration.
        description - The description of the migration.
        type - The type of migration (BASELINE, SQL, ...)
        script - The name of the script to execute for this migration, relative to its classpath location.
        checksum - The checksum of the migration. (Optional)
        executionTime - The execution time (in millis) of this migration.
        success - Flag indicating whether the migration was successful or not.
      • calculateInstalledRank

        protected int calculateInstalledRank()
        Calculates the installed rank for the new migration to be inserted.
        Returns:
        The installed rank.
      • addAppliedMigration

        public abstract void addAppliedMigration​(int installedRank,
                                                 Version version,
                                                 String description,
                                                 MigrationType type,
                                                 String script,
                                                 @Nullable Checksum checksum,
                                                 int executionTime,
                                                 boolean success)