Corrections to stmt ordering for using diff schema
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / database / LoadSchema.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.database;
22
23 import java.io.*;
24 import java.sql.*;
25
26 import org.apache.log4j.Logger;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
32
33 public class LoadSchema {
34         private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoadSchema.class);
35         private static final EELFLogger appLogger = EELFManager.getInstance().getApplicationLogger();
36         private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
37         private static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
38         private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
39         private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
40         
41         static int getVer(Statement s) throws SQLException {
42                 ResultSet rs = null;
43                 try {
44                         rs = s.executeQuery("SELECT version FROM dmaapbc_sch_ver");
45                         rs.next();
46                         return(rs.getInt(1));
47                 } finally {
48                         if (rs != null) {
49                                 rs.close();
50                         }
51                 }
52         }
53         static void upgrade() throws SQLException {
54                 ConnectionFactory cf = ConnectionFactory.getDefaultInstance();
55                 Connection c = null;
56                 Statement stmt = null;
57                 InputStream is = null;
58                 try { 
59                         c = cf.get(true);
60                         stmt = c.createStatement();
61                         
62                         // this sets the PG search_path to a consistent schema, otherwise sometimes
63                         // we get public, and sometimes we get dmaap_admin
64                         String cmd = String.format( "SET search_path to %s;", cf.getSchema());
65                         try {
66                                 stmt.execute(cmd);
67                                 logger.info("SCHEMA: " + cmd);
68                         } catch (SQLException sqle) {
69                                         throw sqle;
70                         }
71                         
72                         
73                         // determine if an upgrade is needed
74                         int newver = -1;
75                         try {
76                                 newver = getVer(stmt);
77                         } catch (Exception e) {}
78                         logger.info("Database schema currently at version " + newver++);
79                         
80
81
82                         while ((is = LoadSchema.class.getClassLoader().getResourceAsStream("schema_" + newver + ".sql")) != null) {
83                                 logger.info("Upgrading database schema to version " + newver);
84                                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
85                                 String s;
86                                 String sofar = null;
87                                 
88
89                                 
90                                 while ((s = br.readLine()) != null) {
91                                         logger.info("SCHEMA: " + s);
92                                         s = s.trim();
93                                         if (s.length() == 0 || s.startsWith("--")) {
94                                                 continue;
95                                         }
96                                         if (sofar == null) {
97                                                 sofar = s;
98                                         } else {
99                                                 sofar = sofar + " " + s;
100                                         }
101                                         if (s.endsWith(";")) {
102                                                 sofar = sofar.substring(0, sofar.length() - 1);
103                                                 boolean ignore = false;
104                                                 if (sofar.startsWith("@")) {
105                                                         ignore = true;
106                                                         sofar = sofar.substring(1).trim();
107                                                 }
108                                                 try {
109                                                         stmt.execute(sofar);
110                                                 } catch (SQLException sqle) {
111                                                         if (!ignore) {
112                                                                 throw sqle;
113                                                         }
114                                                 }
115                                                 sofar = null;
116                                         }
117                                 }
118                                 is.close();
119                                 is = null;
120                                 if (getVer(stmt) != newver) {
121                                         throw new SQLException("Schema version not properly updated to " + newver + " by upgrade script");
122                                 }
123                                 logger.info("Upgrade to database schema version " + newver + " successful");
124                                 newver++;
125                         }
126                 } catch (IOException ioe) {
127                         throw new SQLException(ioe);
128                 } finally {
129                         if (stmt != null) { try { stmt.close(); } catch (Exception e) {}}
130                         if (c != null) { try { c.close(); } catch (Exception e) {}}
131                 }
132         }
133         public static void main(String[] args) throws Exception {
134                 upgrade();
135         }
136 }