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