Search in sources :

Example 21 with DatabaseException

use of liquibase.exception.DatabaseException 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 {
            Catalog catalog = new Catalog(catalogName);
            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;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseException(liquibase.exception.DatabaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) Catalog(liquibase.structure.core.Catalog)

Example 22 with DatabaseException

use of liquibase.exception.DatabaseException 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).getMetaData();
        rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
        Table table;
        if (rs.size() > 0) {
            table = readTable(rs.get(0), database);
        } else {
            return null;
        }
        if (table != null && database instanceof MSSQLDatabase) {
            String schemaName;
            Schema tableSchema = table.getSchema();
            if (tableSchema == null) {
                schemaName = database.getDefaultSchemaName();
            } else {
                schemaName = tableSchema.getName();
            }
            String sql;
            if (database.getDatabaseMajorVersion() >= 9) {
                sql = "SELECT" + " CAST(value as varchar(max)) as REMARKS" + " FROM" + " sys.extended_properties" + " WHERE" + " name='MS_Description'" + " AND major_id = OBJECT_ID('" + database.escapeStringForDatabase(database.escapeTableName(null, schemaName, table.getName())) + "')" + " AND" + " minor_id = 0";
            } else {
                sql = "SELECT CAST(value as varchar) as REMARKS FROM dbo.sysproperties WHERE name='MS_Description' AND id = OBJECT_ID('" + database.escapeStringForDatabase(database.escapeTableName(null, schemaName, table.getName())) + "') AND smallid = 0";
            }
            List<String> remarks = ExecutorService.getInstance().getExecutor(snapshot.getDatabase()).queryForList(new RawSqlStatement(sql), String.class);
            if (remarks != null && remarks.size() > 0) {
                table.setRemarks(StringUtils.trimToNull(remarks.iterator().next()));
            }
        }
        return table;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 23 with DatabaseException

use of liquibase.exception.DatabaseException 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).getMetaData().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);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 24 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class UniqueConstraintSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    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 = null;
        try {
            metadata = listConstraints(table, snapshot, schema);
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
        Set<String> seenConstraints = new HashSet<String>();
        for (CachedRow constraint : metadata) {
            UniqueConstraint uq = new UniqueConstraint().setName(cleanNameFromDatabase((String) constraint.get("CONSTRAINT_NAME"), database)).setTable(table);
            if (constraint.containsColumn("INDEX_NAME")) {
                uq.setBackingIndex(new Index((String) constraint.get("INDEX_NAME"), (String) constraint.get("INDEX_CATALOG"), null, table.getName()));
            }
            if ("CLUSTERED".equals(constraint.get("TYPE_DESC"))) {
                uq.setClustered(true);
            }
            if (seenConstraints.add(uq.getName())) {
                table.getUniqueConstraints().add(uq);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(liquibase.database.Database) DatabaseException(liquibase.exception.DatabaseException)

Example 25 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class AddColumnGeneratorSQLite method generateSql.

@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<Sql> sql = new ArrayList<Sql>();
    // define alter table logic
    SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {

        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[] { new ColumnConfig().setName(statement.getColumnName()).setType(statement.getColumnType()).setAutoIncrement(statement.isAutoIncrement()) };
        }

        public boolean copyThisColumn(ColumnConfig column) {
            return !column.getName().equals(statement.getColumnName());
        }

        public boolean createThisColumn(ColumnConfig column) {
            return true;
        }

        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        List<SqlStatement> alterTableStatements = SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
        sql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(alterTableStatements.toArray(new SqlStatement[alterTableStatements.size()]), database)));
    } catch (DatabaseException e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return sql.toArray(new Sql[sql.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)73 SQLException (java.sql.SQLException)25 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 CatalogAndSchema (liquibase.CatalogAndSchema)13 LiquibaseException (liquibase.exception.LiquibaseException)13 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 CachedRow (liquibase.snapshot.CachedRow)10 InvalidExampleException (liquibase.snapshot.InvalidExampleException)10 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)10 Executor (liquibase.executor.Executor)9 DatabaseConnection (liquibase.database.DatabaseConnection)7 OfflineConnection (liquibase.database.OfflineConnection)7 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)6 IOException (java.io.IOException)5