NullPointerException when genereting changelog (generateChangeLog)

Hi,

I have postgres database on My server, I wanted to freeze its state and start using liqui from some point. While trying to generate changelog from current state i receive many strange messages and null pointer exception:

Reading tables for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... Reading foreign keys for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... Reading primary keys for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... Reading columns for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column sequence_name 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column last_value 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column start_value 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column increment_by 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column max_value 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column min_value 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column cache_value 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column log_cnt 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column is_cycled 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view hibernate_sequence for column is_called 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view nlt_client_pk for column id 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view nlt_subscription_pk for column id 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view nlt_synchronization_report_element_pk for column id 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view nlt_synchronization_report_pk for column id 2010-01-07 15:16:38 liquibase.database.structure.SqlDatabaseSnapshot readColumns INFO: Could not find table or view primary_key_id for column id Reading unique constraints for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... Reading indexes for emsuser @ jdbc:postgresql://gucio:5432/emsserver (Default Schema: ems) ... Migration Failed: Unknown Reason.  For more information, use the --logLevel flag) 2010-01-07 15:16:39 liquibase.commandline.Main main SEVERE: Unknown Reason java.lang.NullPointerException         at liquibase.database.structure.SqlDatabaseSnapshot.readIndexes(SqlDatabaseSnapshot.java:505)         at liquibase.database.structure.SqlDatabaseSnapshot.(SqlDatabaseSnapshot.java:99)         at liquibase.database.structure.PostgresDatabaseSnapshot.(PostgresDatabaseSnapshot.java:35)         at liquibase.database.PostgresDatabase.createDatabaseSnapshot(PostgresDatabase.java:447)         at liquibase.diff.Diff.compare(Diff.java:62)         at liquibase.commandline.CommandLineUtils.doGenerateChangeLog(CommandLineUtils.java:135)         at liquibase.commandline.Main.doMigration(Main.java:591)         at liquibase.commandline.Main.main(Main.java:97)

I really need help, please somebody check this one :expressionless:

Sorry for the slow response, I have limited internet access last week and this.  What version of liquibase are you running?

Nathan

1.9.5

But for version 2.0 the exception is similar and also connected with constraints. If you want i cant past it here.

Problem solved!

Liquibase generateChangeLog along with postgres wont work if there are more than two schemas in DB (one of them has to be public). Otherwise liquibase during dump tries to access to constraints that are from other schemas. When he does that, then this constraint do not have table name and this causes NullPointerException.

Thanks for finding the problem.  It should not do that, I’ll create an issue for it, although I will probably not have a chance to look at it until 2.1.  If you get a chance and a have a good database to test against, any patches would be greatly appreciated.

Nathan

I just want to report that I’m getting this same error (NullPointerException in SqlDatabaseSnapshot.readIndexes(), at line 505), when trying to use a “columnExists” preCondition against a multi-schema Postgres database, using Liquibase 1.9.5.

PreConditions were one of the big reasons we upgraded to 1.9.5 - any chance there’s a fix available?

Thanks for your continuing work on Liquibase,

    Andy

Thanks. I’ll take a look at it and get a new 1.9 build out.  It may be another day or two.

Nathan

Hi,

I got NullPointerException during generateChangeLog:

    DEBUG 17.2.10 14:20:liquibase: Reading unique constraints for djaara @ jdbc:postgresql://127.0.0.1/lesk ... INFO 17.2.10 14:20:liquibase: Reading unique constraints for djaara @ jdbc:postgresql://127.0.0.1/lesk ... DEBUG 17.2.10 14:20:liquibase: Reading indexes for djaara @ jdbc:postgresql://127.0.0.1/lesk ... INFO 17.2.10 14:20:liquibase: Reading indexes for djaara @ jdbc:postgresql://127.0.0.1/lesk ... LiquiBase Update Failed: Unknown Reason INFO 17.2.10 14:20:liquibase: Unknown Reason java.lang.NullPointerException at liquibase.snapshot.core.JdbcDatabaseSnapshotGenerator.readIndexes(JdbcDatabaseSnapshotGenerator.java:592) at liquibase.snapshot.core.JdbcDatabaseSnapshotGenerator.createSnapshot(JdbcDatabaseSnapshotGenerator.java:208) at liquibase.snapshot.DatabaseSnapshotGeneratorFactory.createSnapshot(DatabaseSnapshotGeneratorFactory.java:69) at liquibase.diff.Diff.compare(Diff.java:65) at liquibase.integration.commandline.CommandLineUtils.doGenerateChangeLog(CommandLineUtils.java:113) at liquibase.integration.commandline.Main.doMigration(Main.java:605) at liquibase.integration.commandline.Main.main(Main.java:105)

I am using PostgreSQL 8.4.2 with postgresql-8.4-701.jdbc3.jar driver.

I add some debug output to code and found constraint name which case this null pointer exception - uq_dbcl_orderexec. That unique constraint belongs to liquibses table databasechangelog. After constraint removal generateChangeLog works correctly.

DjAARA

Hmm, it seems that liquibase tables are “ignored” during readTables, but postgresql implementation of readUniqueConstraints will find unique keys for liquibase tables and that case problem - liquibase cannot find table for constraint.

Please, apply this patch to PostgresDatabaseSnapshotGenerator.


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

— liquibase-core-jvm/src/main/java/liquibase/snapshot/core/PostgresDatabaseSnapshotGenerator.java (revision 1285)
+++ liquibase-core-jvm/src/main/java/liquibase/snapshot/core/PostgresDatabaseSnapshotGenerator.java (working copy)
@@ -3,6 +3,7 @@
import liquibase.database.Database;
import liquibase.database.JdbcConnection;
import liquibase.database.core.PostgresDatabase;
+import liquibase.database.structure.Table;
import liquibase.database.structure.UniqueConstraint;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
@@ -51,9 +52,15 @@
                String tableName = rs.getString(“relname”);
                UniqueConstraint constraintInformation = new UniqueConstraint();
                constraintInformation.setName(constraintName);
-                constraintInformation.setTable(snapshot.getTable(tableName));
-                getColumnsForUniqueConstraint(database, conrelid, keys, constraintInformation);
-                foundUC.add(constraintInformation);
+                if(!database.isSystemTable(null, schema, tableName)&&!database.isLiquibaseTable(tableName)) {
+                    Table table = snapshot.getTable(tableName);
+                    if (table == null) {
+                        throw new IllegalStateException("Cannot find table for " + tableName);
+                    }

  •                 constraintInformation.setTable(table);
  •                 getColumnsForUniqueConstraint(database, conrelid, keys, constraintInformation);
  •                 foundUC.add(constraintInformation);
    +                }
                }
                snapshot.getUniqueConstraints().addAll(foundUC);
            }

Thanks, I’ll get that applied

It is committed to trunk and the 1.9 branch.  I’ll get a new snapshot build of 1.9 up if anyone wants to try it out.

Nathan

Thanks for acceptation of this patch Nathan.

I uploaded a build with the change if you want to try it out:  http://liquibase.org/liquibase-1.9.6-SNAPSHOT.jar

Nathan