001package io.ebean.migration.runner;
002
003import io.ebean.migration.MigrationVersion;
004import org.avaje.classpath.scanner.Resource;
005
006/**
007 * A DB migration resource (DDL or Jdbc)
008 */
009public abstract class LocalMigrationResource implements Comparable<LocalMigrationResource> {
010
011  /**
012   * Code for repeatable migrations.
013   */
014  private static final String REPEAT_TYPE = "R";
015
016  /**
017   * Code for version migrations.
018   */
019  private static final String VERSION_TYPE = "V";
020
021  private final MigrationVersion version;
022
023  private final String location;
024
025
026  /**
027   * Construct with version and resource.
028   */
029  public LocalMigrationResource(MigrationVersion version, String location) {
030    this.version = version;
031    this.location = location;
032  }
033
034  public String toString() {
035    return version.toString();
036  }
037
038  /**
039   * Return true if the underlying version is "repeatable".
040   */
041  public boolean isRepeatable() {
042    return version.isRepeatable();
043  }
044
045  /**
046   * Return the "key" that identifies the migration.
047   */
048  public String key() {
049    if (isRepeatable()) {
050      return version.getComment().toLowerCase();
051    } else {
052      return version.normalised();
053    }
054  }
055
056  /**
057   * Return the migration comment.
058   */
059  public String getComment() {
060    String comment = version.getComment();
061    return (comment == null || comment.isEmpty()) ? "-" : comment;
062  }
063
064  /**
065   * Default ordering by version.
066   */
067  @Override
068  public int compareTo(LocalMigrationResource o) {
069    return version.compareTo(o.version);
070  }
071
072  /**
073   * Return the underlying migration version.
074   */
075  public MigrationVersion getVersion() {
076    return version;
077  }
078
079  /**
080   * Return the resource location.
081   */
082  public String getLocation() {
083    return location;
084  }
085
086  /**
087   * Return the type code ("R" or "V") for this migration.
088   */
089  public String getType() {
090    return isRepeatable() ? REPEAT_TYPE : VERSION_TYPE;
091  }
092}