001 package liquibase.precondition.core; 002 003 import liquibase.changelog.DatabaseChangeLog; 004 import liquibase.changelog.ChangeSet; 005 import liquibase.database.Database; 006 import liquibase.exception.PreconditionErrorException; 007 import liquibase.exception.PreconditionFailedException; 008 import liquibase.exception.ValidationErrors; 009 import liquibase.exception.Warnings; 010 import liquibase.precondition.Precondition; 011 import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; 012 import liquibase.util.StringUtils; 013 014 public class TableExistsPrecondition implements Precondition { 015 private String schemaName; 016 private String tableName; 017 018 public String getSchemaName() { 019 return schemaName; 020 } 021 022 public void setSchemaName(String schemaName) { 023 this.schemaName = StringUtils.trimToNull(schemaName); 024 } 025 026 public String getTableName() { 027 return tableName; 028 } 029 030 public void setTableName(String tableName) { 031 this.tableName = tableName; 032 } 033 034 public Warnings warn(Database database) { 035 return new Warnings(); 036 } 037 038 public ValidationErrors validate(Database database) { 039 return new ValidationErrors(); 040 } 041 042 public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) 043 throws PreconditionFailedException, PreconditionErrorException { 044 try { 045 if (!DatabaseSnapshotGeneratorFactory.getInstance().getGenerator(database) 046 .hasTable(getSchemaName(), getTableName(), database)) { 047 throw new PreconditionFailedException("Table " 048 + database.escapeTableName(getSchemaName(), getTableName()) + " does not exist", changeLog, 049 this); 050 } 051 } catch (PreconditionFailedException e) { 052 throw e; 053 } catch (Exception e) { 054 throw new PreconditionErrorException(e, changeLog, this); 055 } 056 } 057 058 public String getName() { 059 return "tableExists"; 060 } 061 }