Skip to content

Package: KapuaConfigurableServiceSchemaUtils

KapuaConfigurableServiceSchemaUtils

nameinstructionbranchcomplexitylinemethod
createSchemaObjects(String)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
dropSchemaObjects(String)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
scriptSession(String, String)
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.configuration;
14:
15: import java.io.File;
16:
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.commons.jpa.CommonsEntityManagerFactory;
19: import org.eclipse.kapua.commons.jpa.EntityManager;
20: import org.eclipse.kapua.commons.jpa.SimpleSqlScriptExecutor;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: /**
25: * Configurable service database schema utilities.
26: *
27: * @since 1.0
28: */
29: public class KapuaConfigurableServiceSchemaUtils {
30:
31: private static final Logger logger = LoggerFactory.getLogger(KapuaConfigurableServiceSchemaUtils.class);
32:
33: private KapuaConfigurableServiceSchemaUtils() {
34: }
35:
36: public static final String DEFAULT_PATH = "src/main/sql/H2";
37: public static final String DEFAULT_FILTER = "sys_*.sql";
38: public static final String DROP_FILTER = "sys_*_drop.sql";
39:
40: /**
41: * Executes the database scripts in the specified path matching the specified filter
42: *
43: * @param path
44: * @param fileFilter
45: * file names pattern
46: */
47: public static void scriptSession(String path, String fileFilter) {
48: EntityManager em = null;
49: try {
50:
51: logger.info("Running database scripts...");
52:
53: em = CommonsEntityManagerFactory.getEntityManager();
54: em.beginTransaction();
55:
56: SimpleSqlScriptExecutor sqlScriptExecutor = new SimpleSqlScriptExecutor();
57: sqlScriptExecutor.scanScripts(path, fileFilter);
58: sqlScriptExecutor.executeUpdate(em);
59:
60: em.commit();
61:
62: logger.info("...database scripts done!");
63: } catch (KapuaException e) {
64: logger.error("Database scripts failed: {}", e.getMessage());
65:• if (em != null) {
66: em.rollback();
67: }
68: } finally {
69:• if (em != null) {
70: em.close();
71: }
72: }
73:
74: }
75:
76: /**
77: * Executes the create schema files contained in the path
78: *
79: * @param path
80: * @throws KapuaException
81: */
82: public static void createSchemaObjects(String path)
83: throws KapuaException {
84: String pathSep = String.valueOf(File.separatorChar);
85:• String sep = path.endsWith(pathSep) ? "" : pathSep;
86: scriptSession(path + sep + DEFAULT_PATH, DEFAULT_FILTER);
87: }
88:
89: /**
90: * Executes the drop schema files contained in the path
91: *
92: * @param path
93: */
94: public static void dropSchemaObjects(String path) {
95: String pathSep = String.valueOf(File.separatorChar);
96:• String sep = path.endsWith(pathSep) ? "" : pathSep;
97: scriptSession(path + sep + DEFAULT_PATH, DROP_FILTER);
98: }
99: }