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