Skip to content

Commit 4e8dd4f

Browse files
Merge branch 'openmrs:openmrs-emr3' into openmrs-emr3
2 parents 51be773 + e55f8e9 commit 4e8dd4f

File tree

8 files changed

+192
-5
lines changed

8 files changed

+192
-5
lines changed

pom-step-01.xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</execution>
3838
<execution>
3939
<id>stop-empty-database</id>
40-
<phase>process-resources</phase>
40+
<phase>verify</phase>
4141
<goals>
4242
<goal>stop</goal>
4343
</goals>
@@ -171,6 +171,36 @@
171171
</execution>
172172
</executions>
173173
</plugin>
174+
<!-- Exec: Empty Dump DB to SQL -->
175+
<plugin>
176+
<groupId>org.codehaus.mojo</groupId>
177+
<artifactId>exec-maven-plugin</artifactId>
178+
<executions>
179+
<execution>
180+
<id>dump-empty-database</id>
181+
<phase>package</phase>
182+
<goals>
183+
<goal>java</goal>
184+
</goals>
185+
<configuration>
186+
<mainClass>org.openmrs.standalone.DatabaseDumper</mainClass>
187+
<arguments>
188+
<argument>${project.build.directory}</argument>
189+
<argument>emptydatabase</argument>
190+
<argument>--port=33326</argument>
191+
<argument>--user=openmrs</argument>
192+
<argument>--password=test</argument>
193+
<argument>--skip-lock-tables</argument>
194+
<argument>--skip-extended-insert</argument>
195+
<argument>--ssl=0</argument>
196+
<argument>openmrs</argument>
197+
<argument>--result-file=${project.build.directory}/openmrs-empty-dump.sql</argument>
198+
</arguments>
199+
<classpathScope>compile</classpathScope>
200+
</configuration>
201+
</execution>
202+
</executions>
203+
</plugin>
174204
</plugins>
175205
<pluginManagement>
176206
<plugins>

pom-step-02.xml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,36 @@
180180
</execution>
181181
</executions>
182182
</plugin>
183+
<!-- Exec: Demo Dump DB to SQL -->
184+
<plugin>
185+
<groupId>org.codehaus.mojo</groupId>
186+
<artifactId>exec-maven-plugin</artifactId>
187+
<executions>
188+
<execution>
189+
<id>dump-demo-database</id>
190+
<phase>prepare-package</phase>
191+
<goals>
192+
<goal>java</goal>
193+
</goals>
194+
<configuration>
195+
<mainClass>org.openmrs.standalone.DatabaseDumper</mainClass>
196+
<arguments>
197+
<argument>${project.build.directory}</argument>
198+
<argument>demodatabase</argument>
199+
<argument>--port=33328</argument>
200+
<argument>--user=openmrs</argument>
201+
<argument>--password=test</argument>
202+
<argument>--skip-lock-tables</argument>
203+
<argument>--skip-extended-insert</argument>
204+
<argument>--ssl=0</argument>
205+
<argument>openmrs</argument>
206+
<argument>--result-file=${project.build.directory}/openmrs-demo-dump.sql</argument>
207+
</arguments>
208+
<classpathScope>compile</classpathScope>
209+
</configuration>
210+
</execution>
211+
</executions>
212+
</plugin>
183213
<plugin>
184214
<groupId>org.apache.maven.plugins</groupId>
185215
<artifactId>maven-assembly-plugin</artifactId>
@@ -206,7 +236,7 @@
206236
</execution>
207237
<execution>
208238
<id>zip-demo-database</id>
209-
<phase>prepare-package</phase>
239+
<phase>package</phase>
210240
<goals>
211241
<goal>single</goal>
212242
</goals>
@@ -221,7 +251,7 @@
221251
</execution>
222252
<execution>
223253
<id>zip-empty-database</id>
224-
<phase>prepare-package</phase>
254+
<phase>package</phase>
225255
<goals>
226256
<goal>single</goal>
227257
</goals>

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
<version>${mockitoVersion}</version>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<groupId>org.mybatis</groupId>
100+
<artifactId>mybatis</artifactId>
101+
<version>3.5.19</version>
102+
</dependency>
98103
</dependencies>
99104

100105
<build>

src/main/assembly/zip-demo-database.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
<fileSets>
1111
<fileSet>
1212
<outputDirectory>/</outputDirectory>
13-
<directory>${project.build.directory}/demodatabase/data</directory>
13+
<directory>${project.build.directory}</directory>
14+
<includes>
15+
<include>openmrs-demo-dump.sql</include>
16+
</includes>
1417
</fileSet>
1518
</fileSets>
1619
</assembly>

src/main/assembly/zip-empty-database.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
<fileSets>
1111
<fileSet>
1212
<outputDirectory>/</outputDirectory>
13-
<directory>${project.build.directory}/emptydatabase/data</directory>
13+
<directory>${project.build.directory}</directory>
14+
<includes>
15+
<include>openmrs-empty-dump.sql</include>
16+
</includes>
1417
</fileSet>
1518
</fileSets>
1619
</assembly>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.standalone;
15+
16+
import java.io.File;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
import static org.openmrs.standalone.OpenmrsUtil.findDumpExecutable;
21+
22+
23+
public class DatabaseDumper {
24+
public static void main(String[] args) throws Exception {
25+
if (args.length < 1) {
26+
System.err.println("Usage: java DatabaseDumper <baseDir> [mariadb-dump args...]");
27+
System.exit(1);
28+
}
29+
30+
String baseDir = args[0];
31+
String dbDir = args[1];
32+
String executable = findDumpExecutable(baseDir, dbDir);
33+
34+
List<String> command = new ArrayList<>();
35+
command.add(executable);
36+
37+
// Add all remaining args starting from args[2], which are the dump flags and db name
38+
for (int i = 2; i < args.length; i++) {
39+
command.add(args[i]);
40+
}
41+
42+
ProcessBuilder pb = new ProcessBuilder(command);
43+
pb.directory(new File("."));
44+
pb.inheritIO();
45+
46+
Process process = pb.start();
47+
int exitCode = process.waitFor();
48+
if (exitCode != 0) {
49+
throw new RuntimeException("Dump command failed with exit code: " + exitCode);
50+
}
51+
52+
System.out.println("✅ Dump completed successfully.");
53+
}
54+
}

src/main/java/org/openmrs/standalone/OpenmrsUtil.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@
1313
*/
1414
package org.openmrs.standalone;
1515

16+
import org.apache.ibatis.jdbc.ScriptRunner;
17+
1618
import java.io.File;
1719
import java.io.FileInputStream;
1820
import java.io.FileNotFoundException;
1921
import java.io.IOException;
2022
import java.io.InputStream;
2123
import java.io.InputStreamReader;
2224
import java.io.UnsupportedEncodingException;
25+
import java.io.Reader;
26+
import java.io.FileReader;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.sql.Connection;
30+
import java.sql.DriverManager;
2331
import java.util.Properties;
2432

2533
public class OpenmrsUtil {
@@ -212,4 +220,41 @@ public static void setDefaultOS(){
212220
public static String getTitle() {
213221
return "OpenMRS " + REFAPP_VERSION + " Standalone";
214222
}
223+
224+
public static void importSqlFile(File sqlFile, String jdbcUrl, String user, String password) {
225+
if (!sqlFile.exists()) {
226+
System.err.println(":x: SQL file not found: " + sqlFile.getAbsolutePath());
227+
return;
228+
}
229+
230+
System.out.println("✅ Preparing to import "+sqlFile+" data");
231+
try (Connection conn = DriverManager.getConnection(jdbcUrl, user, password)) {
232+
233+
System.out.println("📥 Importing SQL from: " + sqlFile.getAbsolutePath());
234+
try (Reader reader = new FileReader(sqlFile)) {
235+
ScriptRunner scriptRunner = new ScriptRunner(conn);
236+
scriptRunner.setLogWriter(null); // Disable logs
237+
scriptRunner.setStopOnError(true);
238+
scriptRunner.runScript(reader);
239+
System.out.println("✅ Successfully imported SQL: " + sqlFile.getAbsolutePath());
240+
}
241+
242+
} catch (Exception e) {
243+
System.err.println("❌ Error importing SQL: " + e.getMessage());
244+
e.printStackTrace();
245+
}
246+
}
247+
248+
public static String findDumpExecutable(String baseDir, String dbDir) {
249+
String os = System.getProperty("os.name").toLowerCase();
250+
boolean isWindows = os.contains("win");
251+
252+
Path mariadbDump = Paths.get(baseDir, dbDir, "bin", isWindows ? "mysqldump.exe" : "mariadb-dump");
253+
254+
if (mariadbDump.toFile().exists()) {
255+
return mariadbDump.toString();
256+
} else {
257+
throw new RuntimeException("❌ Neither mariadb-dump nor mysqldump found in: " + mariadbDump.getParent());
258+
}
259+
}
215260
}

src/main/java/org/openmrs/standalone/StandaloneUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
import ch.vorburger.exec.ManagedProcessException;
4242

43+
import static org.openmrs.standalone.OpenmrsUtil.importSqlFile;
44+
4345
/**
4446
* Utility routines used by the standalone application.
4547
*/
@@ -435,6 +437,7 @@ public static void startupDatabaseToCreateDefaultUser(String mariaDBPort) throws
435437
Properties props = OpenmrsUtil.getRuntimeProperties(getContextName());
436438
String url = props.getProperty("connection.url");
437439
String password = props.getProperty("connection.password");
440+
String username = props.getProperty("connection.username");
438441

439442
System.out.println("Starting MariaDB on port " + mariaDBPort + "...");
440443
MariaDbController.startMariaDB(mariaDBPort, password);
@@ -458,6 +461,20 @@ public static void startupDatabaseToCreateDefaultUser(String mariaDBPort) throws
458461
stmt.executeUpdate(grantCreateUserSQL);
459462

460463
System.out.println("✅ Connection to MariaDB successful.");
464+
465+
// Find sql if exist to preload DB
466+
File dataDir = new File("database/data");
467+
468+
if (dataDir.exists() && dataDir.isDirectory()) {
469+
// Find the first .sql file in the unzipped folder
470+
File[] sqlFiles = dataDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".sql"));
471+
if (sqlFiles != null && sqlFiles.length != 0) {
472+
// Run the first found SQL file
473+
File sqlFile = sqlFiles[0];
474+
importSqlFile(sqlFile, url, username, password);
475+
}
476+
}
477+
461478
} else {
462479
System.err.println("❌ Connection established, but it is not valid.");
463480
}

0 commit comments

Comments
 (0)