001package io.ebean.dbmigration.runner; 002 003import io.ebean.dbmigration.MigrationVersion; 004import org.avaje.classpath.scanner.Resource; 005 006/** 007 * A DB migration resource (DDL script with version). 008 */ 009public 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 private final Resource resource; 026 027 /** 028 * Construct with version and resource. 029 */ 030 public LocalMigrationResource(MigrationVersion version, String location, Resource resource) { 031 this.version = version; 032 this.location = location; 033 this.resource = resource; 034 } 035 036 public String toString() { 037 return version.toString(); 038 } 039 040 /** 041 * Return true if the underlying version is "repeatable". 042 */ 043 public boolean isRepeatable() { 044 return version.isRepeatable(); 045 } 046 047 /** 048 * Return the "key" that identifies the migration. 049 */ 050 public String key() { 051 if (isRepeatable()) { 052 return version.getComment().toLowerCase(); 053 } else { 054 return version.normalised(); 055 } 056 } 057 058 /** 059 * Return the migration comment. 060 */ 061 public String getComment() { 062 String comment = version.getComment(); 063 return (comment == null || comment.isEmpty()) ? "-" : comment; 064 } 065 066 /** 067 * Default ordering by version. 068 */ 069 @Override 070 public int compareTo(LocalMigrationResource o) { 071 return version.compareTo(o.version); 072 } 073 074 /** 075 * Return the underlying migration version. 076 */ 077 public MigrationVersion getVersion() { 078 return version; 079 } 080 081 /** 082 * Return the resource location. 083 */ 084 public String getLocation() { 085 return location; 086 } 087 088 /** 089 * Return the content for the migration apply ddl script. 090 */ 091 public String getContent() { 092 return resource.loadAsString("UTF-8"); 093 } 094 095 /** 096 * Return the type code ("R" or "V") for this migration. 097 */ 098 public String getType() { 099 return isRepeatable() ? REPEAT_TYPE : VERSION_TYPE; 100 } 101}