Release patch 2.0.4
[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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 public class LoadSchema {
30         private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoadSchema.class);
31         
32         static int getVer(Statement s) throws SQLException {
33                 ResultSet rs = null;
34                 try {
35                         rs = s.executeQuery("SELECT version FROM dmaapbc_sch_ver");
36                         rs.next();
37                         return(rs.getInt(1));
38                 } finally {
39                         if (rs != null) {
40                                 rs.close();
41                         }
42                 }
43         }
44         static void upgrade() throws SQLException {
45                 ConnectionFactory cf = ConnectionFactory.getDefaultInstance();
46                 InputStream is = null;
47                 try(
48                                 Connection c = cf.get(true);
49                                 Statement stmt = c.createStatement();
50                 ) {
51
52                         
53                         // this sets the PG search_path to a consistent schema, otherwise sometimes
54                         // we get public, and sometimes we get dmaap_admin
55                         String cmd = String.format( "SET search_path to %s;", cf.getSchema());
56                         try {
57                                 stmt.execute(cmd);
58                                 logger.info("SCHEMA: " + cmd);
59                         } catch (SQLException sqle) {
60                                 logger.error("Error", sqle);
61                                         throw sqle;
62                         }
63                         
64                         
65                         // determine if an upgrade is needed
66                         int newver = -1;
67                         try {
68                                 newver = getVer(stmt);
69                         } catch (Exception e) {
70                                 logger.error("Error", e);
71                         }
72                         logger.info("Database schema currently at version " + newver++);
73                         
74
75
76                         while ((is = LoadSchema.class.getClassLoader().getResourceAsStream("schema_" + newver + ".sql")) != null) {
77                                 logger.info("Upgrading database schema to version " + newver);
78                                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
79                                 String s;
80                                 String sofar = null;
81                                 
82
83                                 
84                                 while ((s = br.readLine()) != null) {
85                                         logger.info("SCHEMA: " + s);
86                                         s = s.trim();
87                                         if (s.length() == 0 || s.startsWith("--")) {
88                                                 continue;
89                                         }
90                                         if (sofar == null) {
91                                                 sofar = s;
92                                         } else {
93                                                 sofar = sofar + " " + s;
94                                         }
95                                         if (s.endsWith(";")) {
96                                                 sofar = sofar.substring(0, sofar.length() - 1);
97                                                 boolean ignore = false;
98                                                 if (sofar.startsWith("@")) {
99                                                         ignore = true;
100                                                         sofar = sofar.substring(1).trim();
101                                                 }
102                                                 try {
103                                                         stmt.execute(sofar);
104                                                 } catch (SQLException sqle) {
105                                                         if (!ignore) {
106                                                                 throw sqle;
107                                                         }
108                                                 }
109                                                 sofar = null;
110                                         }
111                                 }
112                                 is.close();
113                                 is = null;
114                                 if (getVer(stmt) != newver) {
115                                         throw new SQLException("Schema version not properly updated to " + newver + " by upgrade script");
116                                 }
117                                 logger.info("Upgrade to database schema version " + newver + " successful");
118                                 newver++;
119                         }
120                 } catch (IOException ioe) {
121                         throw new SQLException(ioe);
122                 }
123         }
124         public static void main(String[] args) throws Exception {
125                 upgrade();
126         }
127 }