use of liquibase.structure.core.Table in project liquibase by liquibase.
the class UniqueConstraintComparator method hash.
@Override
public String[] hash(DatabaseObject databaseObject, Database accordingTo, DatabaseObjectComparatorChain chain) {
List<String> hashes = new ArrayList<String>();
if (databaseObject.getName() != null) {
hashes.add(databaseObject.getName().toLowerCase());
}
Table table = ((UniqueConstraint) databaseObject).getTable();
if (table != null) {
hashes.addAll(Arrays.asList(DatabaseObjectComparatorFactory.getInstance().hash(table, chain.getSchemaComparisons(), accordingTo)));
}
return hashes.toArray(new String[hashes.size()]);
}
use of liquibase.structure.core.Table in project liquibase by liquibase.
the class MissingTableChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Table missingTable = (Table) missingObject;
PrimaryKey primaryKey = missingTable.getPrimaryKey();
// if (control.diffResult.getReferenceSnapshot().getDatabase().isLiquibaseTable(missingTable.getSchema().toCatalogAndSchema(), missingTable.getName())) {
// continue;
// }
CreateTableChange change = createCreateTableChange();
change.setTableName(missingTable.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(missingTable.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(missingTable.getSchema().getName());
}
if (missingTable.getRemarks() != null) {
change.setRemarks(missingTable.getRemarks());
}
for (Column column : missingTable.getColumns()) {
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setName(column.getName());
LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
String typeString = ddt.toString();
if (comparisonDatabase instanceof MSSQLDatabase) {
typeString = comparisonDatabase.unescapeDataTypeString(typeString);
}
columnConfig.setType(typeString);
if (column.isAutoIncrement()) {
columnConfig.setAutoIncrement(true);
}
ConstraintsConfig constraintsConfig = null;
// In MySQL, the primary key must be specified at creation for an autoincrement column
if (column.isAutoIncrement() && primaryKey != null && primaryKey.getColumns().size() == 1 && primaryKey.getColumnNamesAsList().contains(column.getName())) {
if (referenceDatabase instanceof MSSQLDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && !primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else if (referenceDatabase instanceof PostgresDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else {
constraintsConfig = new ConstraintsConfig();
if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
constraintsConfig.setPrimaryKey(true);
constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
// MySQL sets some primary key names as PRIMARY which is invalid
if (comparisonDatabase instanceof MySQLDatabase && "PRIMARY".equals(primaryKey.getName())) {
constraintsConfig.setPrimaryKeyName(null);
} else {
constraintsConfig.setPrimaryKeyName(primaryKey.getName());
}
control.setAlreadyHandledMissing(primaryKey);
control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
} else {
constraintsConfig.setNullable(false);
}
}
} else if (column.isNullable() != null && !column.isNullable()) {
constraintsConfig = new ConstraintsConfig();
constraintsConfig.setNullable(false);
}
if (constraintsConfig != null) {
columnConfig.setConstraints(constraintsConfig);
}
setDefaultValue(columnConfig, column, referenceDatabase);
if (column.getRemarks() != null) {
columnConfig.setRemarks(column.getRemarks());
}
if (column.getAutoIncrementInformation() != null) {
BigInteger startWith = column.getAutoIncrementInformation().getStartWith();
BigInteger incrementBy = column.getAutoIncrementInformation().getIncrementBy();
if (startWith != null && !startWith.equals(BigInteger.ONE)) {
columnConfig.setStartWith(startWith);
}
if (incrementBy != null && !incrementBy.equals(BigInteger.ONE)) {
columnConfig.setIncrementBy(incrementBy);
}
}
change.addColumn(columnConfig);
control.setAlreadyHandledMissing(column);
}
return new Change[] { change };
}
use of liquibase.structure.core.Table in project liquibase by liquibase.
the class StandardLockService method destroy.
@Override
public void destroy() throws DatabaseException {
try {
if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(database.getDatabaseChangeLogLockTableName()).setSchema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName()), database)) {
ExecutorService.getInstance().getExecutor(database).execute(new DropTableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName(), false));
hasDatabaseChangeLogLockTable = null;
}
reset();
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.structure.core.Table in project liquibase by liquibase.
the class ColumnExistsPrecondition method checkFast.
private void checkFast(Database database, DatabaseChangeLog changeLog) throws PreconditionFailedException, PreconditionErrorException {
Statement statement = null;
try {
statement = ((JdbcConnection) database.getConnection()).createStatement();
String schemaName = getSchemaName();
if (schemaName == null) {
schemaName = database.getDefaultSchemaName();
}
String tableName = getTableName();
String columnName = getColumnName();
if (database instanceof PostgresDatabase) {
String sql = "SELECT 1 FROM pg_attribute a WHERE EXISTS (SELECT 1 FROM pg_class JOIN pg_catalog.pg_namespace ns ON ns.oid = pg_class.relnamespace WHERE lower(ns.nspname)='" + schemaName.toLowerCase() + "' AND lower(relname) = lower('" + tableName + "') AND pg_class.oid = a.attrelid) AND lower(a.attname) = lower('" + columnName + "');";
try {
ResultSet rs = statement.executeQuery(sql);
try {
if (rs.next()) {
return;
} else {
// column or table does not exist
throw new PreconditionFailedException(format("Column %s.%s.%s does not exist", schemaName, tableName, columnName), changeLog, this);
}
} finally {
rs.close();
}
} catch (SQLException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
try {
String sql = format("select t.%s from %s.%s t where 0=1", database.escapeColumnNameList(columnName), database.escapeObjectName(schemaName, Schema.class), database.escapeObjectName(tableName, Table.class));
statement.executeQuery(sql).close();
// column exists
return;
} catch (SQLException e) {
// column or table does not exist
throw new PreconditionFailedException(format("Column %s.%s.%s does not exist", schemaName, tableName, columnName), changeLog, this);
}
} catch (DatabaseException e) {
throw new PreconditionErrorException(e, changeLog, this);
} finally {
JdbcUtils.closeStatement(statement);
}
}
use of liquibase.structure.core.Table in project liquibase by liquibase.
the class ForeignKeyExistsPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
try {
ForeignKey example = new ForeignKey();
example.setName(getForeignKeyName());
example.setForeignKeyTable(new Table());
if (StringUtils.trimToNull(getForeignKeyTableName()) != null) {
example.getForeignKeyTable().setName(getForeignKeyTableName());
}
example.getForeignKeyTable().setSchema(new Schema(getCatalogName(), getSchemaName()));
if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
throw new PreconditionFailedException("Foreign Key " + database.escapeIndexName(catalogName, schemaName, foreignKeyName) + " does not exist", changeLog, this);
}
} catch (PreconditionFailedException e) {
throw e;
} catch (Exception e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
Aggregations