Search in sources :

Example 6 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class SnapshotGeneratorFactory method has.

public boolean has(DatabaseObject example, Database database) throws DatabaseException, InvalidExampleException {
    List<Class<? extends DatabaseObject>> types = new ArrayList<Class<? extends DatabaseObject>>(getContainerTypes(example.getClass(), database));
    types.add(example.getClass());
    //workaround for common check for databasechangelog/lock table to not snapshot the whole database like we have to in order to handle case issues
    if (example instanceof Table && (example.getName().equals(database.getDatabaseChangeLogTableName()) || example.getName().equals(database.getDatabaseChangeLogLockTableName()))) {
        try {
            ExecutorService.getInstance().getExecutor(database).queryForInt(new RawSqlStatement("select count(*) from " + database.escapeObjectName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), example.getName(), Table.class)));
            return true;
        } catch (DatabaseException e) {
            if (database instanceof PostgresDatabase) {
                //throws "current transaction is aborted" unless we roll back the connection
                database.rollback();
            }
            return false;
        }
    }
    if (createSnapshot(example, database, new SnapshotControl(database, false, types.toArray(new Class[types.size()]))) != null) {
        return true;
    }
    CatalogAndSchema catalogAndSchema;
    if (example.getSchema() == null) {
        catalogAndSchema = database.getDefaultSchema();
    } else {
        catalogAndSchema = example.getSchema().toCatalogAndSchema();
    }
    DatabaseSnapshot snapshot = createSnapshot(catalogAndSchema, database, new SnapshotControl(database, false, example.getClass()));
    for (DatabaseObject obj : snapshot.get(example.getClass())) {
        if (DatabaseObjectComparatorFactory.getInstance().isSameObject(example, obj, null, database)) {
            return true;
        }
    }
    return false;
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) PostgresDatabase(liquibase.database.core.PostgresDatabase) Table(liquibase.structure.core.Table) DatabaseObject(liquibase.structure.DatabaseObject) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseException(liquibase.exception.DatabaseException)

Example 7 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class DataSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Data.class)) {
        return;
    }
    if (foundObject instanceof Table) {
        Table table = (Table) foundObject;
        try {
            Data exampleData = new Data().setTable(table);
            table.setAttribute("data", exampleData);
        } catch (Exception e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : Table(liquibase.structure.core.Table) Data(liquibase.structure.core.Data) DatabaseException(liquibase.exception.DatabaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException)

Example 8 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class ColumnExistsPrecondition method checkUsingSnapshot.

private void checkUsingSnapshot(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    Column example = new Column();
    if (StringUtils.trimToNull(getTableName()) != null) {
        example.setRelation(new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(new Schema(getCatalogName(), getSchemaName())));
    }
    example.setName(database.correctObjectName(getColumnName(), Column.class));
    try {
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            throw new PreconditionFailedException("Column '" + database.escapeColumnName(catalogName, schemaName, getTableName(), getColumnName()) + "' does not exist", changeLog, this);
        }
    } catch (LiquibaseException e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema)

Example 9 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class IndexExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        Schema schema = new Schema(getCatalogName(), getSchemaName());
        Index example = new Index();
        String tableName = StringUtils.trimToNull(getTableName());
        if (tableName != null) {
            example.setTable((Table) new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
        }
        example.setName(database.correctObjectName(getIndexName(), Index.class));
        if (StringUtils.trimToNull(getColumnNames()) != null) {
            for (String column : getColumnNames().split("\\s*,\\s*")) {
                example.addColumn(new Column(database.correctObjectName(column, Column.class)));
            }
        }
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            String name = "";
            if (getIndexName() != null) {
                name += database.escapeObjectName(getIndexName(), Index.class);
            }
            if (tableName != null) {
                name += " on " + database.escapeObjectName(getTableName(), Table.class);
                if (StringUtils.trimToNull(getColumnNames()) != null) {
                    name += " columns " + getColumnNames();
                }
            }
            throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this);
        }
    } catch (Exception e) {
        if (e instanceof PreconditionFailedException) {
            throw (((PreconditionFailedException) e));
        }
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) Index(liquibase.structure.core.Index)

Example 10 with Table

use of liquibase.structure.core.Table in project liquibase by liquibase.

the class PrimaryKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        PrimaryKey example = new PrimaryKey();
        Table table = new Table();
        table.setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (StringUtils.trimToNull(getTableName()) != null) {
            table.setName(getTableName());
        }
        example.setTable(table);
        example.setName(getPrimaryKeyName());
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            if (tableName != null) {
                throw new PreconditionFailedException("Primary Key does not exist on " + database.escapeObjectName(getTableName(), Table.class), changeLog, this);
            } else {
                throw new PreconditionFailedException("Primary Key " + database.escapeObjectName(getPrimaryKeyName(), PrimaryKey.class) + " does not exist", changeLog, this);
            }
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PrimaryKey(liquibase.structure.core.PrimaryKey)

Aggregations

Table (liquibase.structure.core.Table)28 Column (liquibase.structure.core.Column)13 Schema (liquibase.structure.core.Schema)7 DatabaseException (liquibase.exception.DatabaseException)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 SnapshotControl (liquibase.snapshot.SnapshotControl)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)4 ArrayList (java.util.ArrayList)3 Liquibase (liquibase.Liquibase)3 Change (liquibase.change.Change)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)3 ForeignKey (liquibase.structure.core.ForeignKey)3 PrimaryKey (liquibase.structure.core.PrimaryKey)3 Test (org.junit.Test)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Date (java.util.Date)2