2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.database;
26 import org.apache.log4j.Logger;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
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();
41 static int getVer(Statement s) throws SQLException {
44 rs = s.executeQuery("SELECT version FROM dmaapbc_sch_ver");
53 static void upgrade() throws SQLException {
54 ConnectionFactory cf = ConnectionFactory.getDefaultInstance();
56 Statement stmt = null;
57 InputStream is = null;
60 stmt = c.createStatement();
63 newver = getVer(stmt);
64 } catch (Exception e) {}
65 logger.info("Database schema currently at version " + newver++);
67 while ((is = LoadSchema.class.getClassLoader().getResourceAsStream("schema_" + newver + ".sql")) != null) {
68 logger.info("Upgrading database schema to version " + newver);
69 BufferedReader br = new BufferedReader(new InputStreamReader(is));
75 sofar = String.format( "SET search_path to %s;", cf.getSchema());
77 while ((s = br.readLine()) != null) {
78 logger.info("SCHEMA: " + s);
80 if (s.length() == 0 || s.startsWith("--")) {
86 sofar = sofar + " " + s;
88 if (s.endsWith(";")) {
89 sofar = sofar.substring(0, sofar.length() - 1);
90 boolean ignore = false;
91 if (sofar.startsWith("@")) {
93 sofar = sofar.substring(1).trim();
97 } catch (SQLException sqle) {
107 if (getVer(stmt) != newver) {
108 throw new SQLException("Schema version not properly updated to " + newver + " by upgrade script");
110 logger.info("Upgrade to database schema version " + newver + " successful");
113 } catch (IOException ioe) {
114 throw new SQLException(ioe);
116 if (stmt != null) { try { stmt.close(); } catch (Exception e) {}}
117 if (c != null) { try { c.close(); } catch (Exception e) {}}
120 public static void main(String[] args) throws Exception {