use of liquibase.database.Database in project liquibase by liquibase.
the class SchemaSnapshotGenerator method snapshotObject.
@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
Database database = snapshot.getDatabase();
Schema match = null;
String catalogName = ((Schema) example).getCatalogName();
String schemaName = example.getName();
if (database.supportsSchemas()) {
if (catalogName == null) {
catalogName = database.getDefaultCatalogName();
}
if (schemaName == null) {
schemaName = database.getDefaultSchemaName();
}
} else {
if (database.supportsCatalogs()) {
if ((catalogName == null) && (schemaName != null)) {
catalogName = schemaName;
schemaName = null;
}
} else {
catalogName = null;
schemaName = null;
}
}
example = new Schema(catalogName, schemaName);
// use LEGACY quoting since we're dealing with system objects
ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
try {
if (database.supportsSchemas()) {
for (String tableSchema : getDatabaseSchemaNames(database)) {
CatalogAndSchema schemaFromJdbcInfo = toCatalogAndSchema(tableSchema, database);
Catalog catalog = new Catalog(schemaFromJdbcInfo.getCatalogName());
Schema schema = new Schema(catalog, tableSchema);
if (DatabaseObjectComparatorFactory.getInstance().isSameObject(schema, example, snapshot.getSchemaComparisons(), database)) {
if (match == null) {
match = schema;
} else {
throw new InvalidExampleException("Found multiple catalog/schemas matching " + ((Schema) example).getCatalogName() + "." + example.getName());
}
}
}
} else {
// name as equal to the catalog name.
if (((Schema) example).getCatalog().isDefault()) {
match = new Schema(((Schema) example).getCatalog(), catalogName);
} else {
/* Before we confirm the schema/catalog existence, we must first check if the catalog exists. */
Catalog catalog = ((Schema) example).getCatalog();
String[] dbCatalogNames = getDatabaseCatalogNames(database);
for (String candidateCatalogName : dbCatalogNames) {
if (catalog.equals(new Catalog(candidateCatalogName))) {
match = new Schema(catalog, catalogName);
}
}
}
}
} catch (SQLException e) {
throw new DatabaseException(e);
} finally {
database.setObjectQuotingStrategy(currentStrategy);
}
if ((match != null) && ((match.getName() == null) || match.getName().equalsIgnoreCase(database.getDefaultSchemaName()))) {
match.setDefault(true);
}
return match;
}
use of liquibase.database.Database in project liquibase by liquibase.
the class TableSnapshotGenerator method snapshotObject.
@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
Database database = snapshot.getDatabase();
String objectName = example.getName();
Schema schema = example.getSchema();
List<CachedRow> rs = null;
try {
JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
Table table;
if (!rs.isEmpty()) {
table = readTable(rs.get(0), database);
} else {
return null;
}
return table;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of liquibase.database.Database in project liquibase by liquibase.
the class TableSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
return;
}
if (foundObject instanceof Schema) {
Database database = snapshot.getDatabase();
Schema schema = (Schema) foundObject;
List<CachedRow> tableMetaDataRs = null;
try {
tableMetaDataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache().getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
for (CachedRow row : tableMetaDataRs) {
String tableName = row.getString("TABLE_NAME");
Table tableExample = (Table) new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);
schema.addDatabaseObject(tableExample);
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
use of liquibase.database.Database in project liquibase by liquibase.
the class UniqueConstraintSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
if (!snapshot.getSnapshotControl().shouldInclude(UniqueConstraint.class)) {
return;
}
if (foundObject instanceof Table) {
Table table = (Table) foundObject;
Database database = snapshot.getDatabase();
Schema schema;
schema = table.getSchema();
List<CachedRow> metadata;
try {
metadata = listConstraints(table, snapshot, schema);
} catch (SQLException e) {
throw new DatabaseException(e);
}
Set<String> seenConstraints = new HashSet<>();
for (CachedRow constraint : metadata) {
UniqueConstraint uq = new UniqueConstraint().setName(cleanNameFromDatabase((String) constraint.get("CONSTRAINT_NAME"), database)).setRelation(table);
if (constraint.containsColumn("INDEX_NAME")) {
uq.setBackingIndex(new Index((String) constraint.get("INDEX_NAME"), (String) constraint.get("INDEX_CATALOG"), (String) constraint.get("INDEX_SCHEMA"), table.getName()));
}
if ("CLUSTERED".equals(constraint.get("TYPE_DESC"))) {
uq.setClustered(true);
}
if (seenConstraints.add(uq.getName())) {
table.getUniqueConstraints().add(uq);
}
}
}
}
use of liquibase.database.Database in project liquibase by liquibase.
the class ColumnSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
if (!snapshot.getSnapshotControl().shouldInclude(Column.class)) {
return;
}
if (foundObject instanceof Relation) {
Database database = snapshot.getDatabase();
Relation relation = (Relation) foundObject;
List<CachedRow> allColumnsMetadataRs;
try {
JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
Schema schema;
schema = relation.getSchema();
allColumnsMetadataRs = databaseMetaData.getColumns(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
List<CachedRow> metaDataNotNullConst = databaseMetaData.getNotNullConst(schema.getCatalogName(), schema.getName(), relation.getName());
/*
* Microsoft SQL Server, SAP SQL Anywhere and probably other RDBMS guarantee non-duplicate
* ORDINAL_POSITIONs for the columns of a single table. But they do not guarantee there are no gaps
* in that integers (e.g. if columns have been deleted). So we need to check for that and renumber
* if needed.
*/
TreeMap<Integer, CachedRow> treeSet = new TreeMap<>();
for (CachedRow row : allColumnsMetadataRs) {
treeSet.put(row.getInt("ORDINAL_POSITION"), row);
}
Logger log = Scope.getCurrentScope().getLog(getClass());
// Now we can iterate through the sorted list and repair if needed.
int currentOrdinal = 0;
for (CachedRow row : treeSet.values()) {
currentOrdinal++;
int rsOrdinal = row.getInt("ORDINAL_POSITION");
if (rsOrdinal != currentOrdinal) {
log.fine(String.format("Repairing ORDINAL_POSITION with gaps for table=%s, column name=%s, " + "bad ordinal=%d, new ordinal=%d", relation.getName(), row.getString("COLUMN_NAME"), rsOrdinal, currentOrdinal));
row.set("ORDINAL_POSITION", currentOrdinal);
}
}
// Iterate through all (repaired) rows and add the columns to our result.
for (CachedRow row : allColumnsMetadataRs) {
Column column = readColumn(row, relation, database);
setAutoIncrementDetails(column, database, snapshot);
populateValidateNullableIfNeeded(column, metaDataNotNullConst, database);
column.setAttribute(LIQUIBASE_COMPLETE, !column.isNullable());
relation.getColumns().add(column);
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
Aggregations