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