Merge "DatabaseClass.java: Fixed sonar issues"
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / database / LoadSchema.java
index 6e64c4a..1f1b078 100644 (file)
@@ -23,20 +23,12 @@ package org.onap.dmaap.dbcapi.database;
 import java.io.*;
 import java.sql.*;
 
-import org.apache.log4j.Logger;
-
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-
-import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
+;
 
 public class LoadSchema        {
        private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoadSchema.class);
-       private static final EELFLogger appLogger = EELFManager.getInstance().getApplicationLogger();
-       private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
-       private static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
-       private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
-       private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
        
        static int getVer(Statement s) throws SQLException {
                ResultSet rs = null;
@@ -55,19 +47,41 @@ public class LoadSchema     {
                Connection c = null;
                Statement stmt = null;
                InputStream is = null;
-               try {
+               try { 
                        c = cf.get(true);
                        stmt = c.createStatement();
+                       
+                       // this sets the PG search_path to a consistent schema, otherwise sometimes
+                       // we get public, and sometimes we get dmaap_admin
+                       String cmd = String.format( "SET search_path to %s;", cf.getSchema());
+                       try {
+                               stmt.execute(cmd);
+                               logger.info("SCHEMA: " + cmd);
+                       } catch (SQLException sqle) {
+                               logger.error("Error", sqle);
+                                       throw sqle;
+                       }
+                       
+                       
+                       // determine if an upgrade is needed
                        int newver = -1;
                        try {
                                newver = getVer(stmt);
-                       } catch (Exception e) {}
+                       } catch (Exception e) {
+                               logger.error("Error", e);
+                       }
                        logger.info("Database schema currently at version " + newver++);
+                       
+
+
                        while ((is = LoadSchema.class.getClassLoader().getResourceAsStream("schema_" + newver + ".sql")) != null) {
                                logger.info("Upgrading database schema to version " + newver);
                                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                String s;
                                String sofar = null;
+                               
+
+                               
                                while ((s = br.readLine()) != null) {
                                        logger.info("SCHEMA: " + s);
                                        s = s.trim();
@@ -107,8 +121,20 @@ public class LoadSchema    {
                } catch (IOException ioe) {
                        throw new SQLException(ioe);
                } finally {
-                       if (stmt != null) { try { stmt.close(); } catch (Exception e) {}}
-                       if (c != null) { try { c.close(); } catch (Exception e) {}}
+                       if (stmt != null) { 
+                               try { 
+                                       stmt.close(); 
+                               } catch (Exception e) {
+                                       logger.error("Error", e);
+                               }
+                       }
+                       if (c != null) { 
+                               try { 
+                                       c.close(); 
+                                       } catch (Exception e) {
+                                               logger.error("Error", e);
+                                       }
+                               }
                }
        }
        public static void main(String[] args) throws Exception {