ViewExists precondition is using a complete snapshot

Hi all,

ViewExists precondition is using a complete database snapshot to determine if a view exists.
Couldn’t we use the same mechanism than the TableExists precondition with an hasView function similar to hasTable function ?

Best regards.

We can, it just didn’t make it into the current release yet.  Hopefully for 2.1.

Nathan

May I provide an untested patch ?

Index: liquibase-core/src/main/java/liquibase/snapshot/DatabaseSnapshotGenerator.java

— liquibase-core/src/main/java/liquibase/snapshot/DatabaseSnapshotGenerator.java (revision 1981)
+++ liquibase-core/src/main/java/liquibase/snapshot/DatabaseSnapshotGenerator.java (working copy)
@@ -48,4 +48,6 @@
    boolean hasDatabaseChangeLogLockTable(Database database);

    public boolean hasTable(String schemaName, String tableName, Database database);

+   

+    public boolean hasView(String schemaName, String viewName, Database database);

}

Index: liquibase-core/src/main/java/liquibase/snapshot/jvm/JdbcDatabaseSnapshotGenerator.java

— liquibase-core/src/main/java/liquibase/snapshot/jvm/JdbcDatabaseSnapshotGenerator.java (revision 1981)
+++ liquibase-core/src/main/java/liquibase/snapshot/jvm/JdbcDatabaseSnapshotGenerator.java (working copy)
@@ -63,6 +63,21 @@
            throw new UnexpectedLiquibaseException(e);

        }

    }

+   

+    public boolean hasView(String schemaName, String viewName, Database database) {

+        try {

+            ResultSet rs = getMetaData(database).getTables(database.convertRequestedSchemaToCatalog(schemaName), database.convertRequestedSchemaToSchema(schemaName), convertTableNameToDatabaseTableName(viewName), new String[]{“VIEW”});

+            try {

+                return rs.next();

+            } finally {

+                try {

+                    rs.close();

+                } catch (SQLException ignore) { }

+            }

+        } catch (Exception e) {

+            throw new UnexpectedLiquibaseException(e);

+        }

+    }

    public Table getTable(String schemaName, String tableName, Database database) throws DatabaseException {

        ResultSet rs = null;

Index: liquibase-core/src/main/java/liquibase/precondition/core/ViewExistsPrecondition.java

— liquibase-core/src/main/java/liquibase/precondition/core/ViewExistsPrecondition.java (revision 1981)
+++ liquibase-core/src/main/java/liquibase/precondition/core/ViewExistsPrecondition.java (working copy)
@@ -38,15 +38,15 @@
    }

    public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {

-        DatabaseSnapshot snapshot;

-        try {

-            snapshot = DatabaseSnapshotGeneratorFactory.getInstance().createSnapshot(database, getSchemaName(), null);

-        } catch (DatabaseException e) {

+    try {

+            if (!DatabaseSnapshotGeneratorFactory.getInstance().getGenerator(database).hasView(getSchemaName(), getViewName(), database)) {

+                throw new PreconditionFailedException(“View “+database.escapeTableName(getSchemaName(), getViewName())+” does not exist”, changeLog, this);

+            }

+        } catch (PreconditionFailedException e) {

+            throw e;

+        } catch (Exception e) {

            throw new PreconditionErrorException(e, changeLog, this);

        }

-        if (snapshot.getView(getViewName()) == null) {

-            throw new PreconditionFailedException(“View “+database.escapeStringForDatabase(getViewName())+” does not exist”, changeLog, this);

-        }

    }

    public String getName() {

The patch got mangled in the post body.  Could you email it to me?  nathan@liquibase.org

Nathan