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;
}
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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations