All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.precondition.core.ColumnExistsPrecondition Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.precondition.core;

import liquibase.changelog.DatabaseChangeLog;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.exception.*;
import liquibase.precondition.Precondition;
import liquibase.snapshot.DatabaseSnapshotGeneratorFactory;
import liquibase.util.StringUtils;

public class ColumnExistsPrecondition implements Precondition {
    private String schemaName;
    private String tableName;
    private String columnName;

    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = StringUtils.trimToNull(schemaName);
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public Warnings warn(Database database) {
        return new Warnings();
    }

    public ValidationErrors validate(Database database) {
        return new ValidationErrors();
    }
    
    public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
        try {
            if (DatabaseSnapshotGeneratorFactory.getInstance().getGenerator(database).getColumn(getSchemaName(), getTableName(), getColumnName(), database) == null) {
                throw new PreconditionFailedException("Column '"+database.escapeColumnName(getSchemaName(), getTableName(), getColumnName())+"' does not exist", changeLog, this);
            }
        } catch (DatabaseException e) {
            throw new PreconditionErrorException(e, changeLog, this);
        }
    }

    public String getName() {
        return "columnExists";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy