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 the "key" that identifies the migration. 045 */ 046 public String key() { 047 if (isRepeatable()) { 048 return version.getComment().toLowerCase(); 049 } else { 050 return version.normalised(); 051 } 052 } 053 054 /** 055 * Return the migration comment. 056 */ 057 public String getComment() { 058 String comment = version.getComment(); 059 return (comment == null || comment.isEmpty()) ? "-" : comment; 060 } 061 062 /** 063 * Default ordering by version. 064 */ 065 @Override 066 public int compareTo(LocalMigrationResource o) { 067 return version.compareTo(o.version); 068 } 069 070 /** 071 * Return the underlying migration version. 072 */ 073 public MigrationVersion getVersion() { 074 return version; 075 } 076 077 /** 078 * Return the resource location. 079 */ 080 public String getLocation() { 081 return location; 082 } 083 084 /** 085 * Return the content of the migration. 086 */ 087 public abstract String getContent(); 088 089 /** 090 * Return the type code ("R" or "V") for this migration. 091 */ 092 public String getType() { 093 return type; 094 } 095 096 /** 097 * Set the migration to be an Init migration. 098 */ 099 public void setInitType() { 100 this.type = MigrationVersion.BOOTINIT_TYPE; 101 } 102}