diff --git a/app/Migrations/0020-data.sql b/app/Migrations/0020-data.sql new file mode 100644 index 0000000..2cac31f --- /dev/null +++ b/app/Migrations/0020-data.sql @@ -0,0 +1,4 @@ +-- Inserts here +INSERT INTO Customers (CustomerID, Name, Address) VALUES + (1, 'John Doe', '123 Main St'), + (2, 'Jane Smith', '456 Elm St'); \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0910329..c8a22dc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -18,18 +18,17 @@ repositories { dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.10.2") - testImplementation("org.skyscreamer:jsonassert:1.5.0") - testImplementation("com.mashape.unirest:unirest-java:1.4.9") + // testImplementation("org.skyscreamer:jsonassert:1.5.0") // For JSON assertions in tests. + // testImplementation("com.mashape.unirest:unirest-java:1.4.9") // For HTTP requests in tests. testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.2") - implementation("com.google.guava:guava:32.1.1-jre") + // implementation("com.google.guava:guava:33.1.0-jre") // Currently not used. implementation("com.sparkjava:spark-core:2.9.4") implementation("com.fasterxml.jackson.core:jackson-core:2.17.0") implementation("com.fasterxml.jackson.core:jackson-databind:2.17.0") - implementation("org.slf4j:slf4j-simple:1.7.30") - implementation("mysql:mysql-connector-java:8.0.19") - implementation("org.xerial:sqlite-jdbc:3.30.1") + implementation("org.slf4j:slf4j-simple:2.0.13") + implementation("org.xerial:sqlite-jdbc:3.45.3.0") } // Apply a specific Java toolchain to ease working on different environments. diff --git a/app/src/main/java/krusty/Database.java b/app/src/main/java/krusty/Database.java index 53d2cd3..e673436 100644 --- a/app/src/main/java/krusty/Database.java +++ b/app/src/main/java/krusty/Database.java @@ -6,23 +6,19 @@ 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 // "jdbc:sqlite:.sqlite3" to use a file-based database instead. // Nore that ":memory:" is an **SQLite specific** magic string that tells the // 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; 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). /** 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; } } } diff --git a/app/src/main/java/krusty/ServerMain.java b/app/src/main/java/krusty/ServerMain.java index b028cf8..d753727 100644 --- a/app/src/main/java/krusty/ServerMain.java +++ b/app/src/main/java/krusty/ServerMain.java @@ -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);