Cleaner migration
This commit is contained in:
parent
ebbd68d6f2
commit
09e733add3
2 changed files with 28 additions and 36 deletions
|
@ -6,16 +6,12 @@ import spark.Response;
|
|||
// Likely dependencies for db operations
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
// Likely dependencies for general operations
|
||||
import java.io.File;
|
||||
import java.util.StringJoiner;
|
||||
import java.io.IOException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
import java.io.FileReader;
|
||||
|
||||
public class Database {
|
||||
// Here, we use an in-memory database. This string could be changed to
|
||||
|
@ -67,37 +63,28 @@ public class Database {
|
|||
// The script location is relative to the gradle
|
||||
// build script ("build.gradle.kts", in this case).
|
||||
/** Reads an sql script into the database */
|
||||
public void migrateScript(String filename) {
|
||||
// Open the file
|
||||
StringBuilder script = new StringBuilder();
|
||||
try {
|
||||
File myObj = new File(filename);
|
||||
Scanner myReader = new Scanner(myObj);
|
||||
public void migrateScript(String filename) throws IOException, SQLException {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
StringBuilder scriptBuilder = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while (myReader.hasNextLine()) {
|
||||
String data = myReader.nextLine();
|
||||
script.append(data);
|
||||
// Read the script file line by line
|
||||
while ((line = reader.readLine()) != null) {
|
||||
scriptBuilder.append(line).append("\n");
|
||||
}
|
||||
myReader.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Migration script not found...");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
System.out.println("Migration script read successfully...");
|
||||
}
|
||||
String script = scriptBuilder.toString().trim();
|
||||
|
||||
// Execute the script
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute(script.toString());
|
||||
conn.commit();
|
||||
conn.setAutoCommit(true);
|
||||
// Execute the script
|
||||
try (Statement statement = conn.createStatement()) {
|
||||
statement.execute(script);
|
||||
}
|
||||
System.out.println(String.format("Executed script %s", filename));
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading script file: " + e.getMessage());
|
||||
throw e;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error executing migration script...");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
System.out.println("Migration script executed successfully...");
|
||||
System.err.println("Error executing script: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,12 @@ public class ServerMain {
|
|||
db.connect();
|
||||
|
||||
// Here, we can migrate an arbitrary number of SQL scripts.
|
||||
db.migrateScript("Migrations/0010-tables.sql");
|
||||
try {
|
||||
db.migrateScript("Migrations/0010-tables.sql");
|
||||
db.migrateScript("Migrations/0020-data.sql");
|
||||
} catch (Exception e) {
|
||||
throw new IOError(e);
|
||||
}
|
||||
|
||||
port(PORT);
|
||||
|
||||
|
|
Loading…
Reference in a new issue