Compare commits

..

No commits in common. "c591c6431bef29541f4f90a060cbaae0ef474c4b" and "ebbd68d6f25613a6c987a6e89105335af46ac9de" have entirely different histories.

4 changed files with 42 additions and 37 deletions

View file

@ -1,4 +0,0 @@
-- Inserts here
INSERT INTO Customers (CustomerID, Name, Address) VALUES
(1, 'John Doe', '123 Main St'),
(2, 'Jane Smith', '456 Elm St');

View file

@ -18,17 +18,18 @@ repositories {
dependencies { dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2") testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
// testImplementation("org.skyscreamer:jsonassert:1.5.0") // For JSON assertions in tests. testImplementation("org.skyscreamer:jsonassert:1.5.0")
// testImplementation("com.mashape.unirest:unirest-java:1.4.9") // For HTTP requests in tests. testImplementation("com.mashape.unirest:unirest-java:1.4.9")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.2") testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.2")
// implementation("com.google.guava:guava:33.1.0-jre") // Currently not used. implementation("com.google.guava:guava:32.1.1-jre")
implementation("com.sparkjava:spark-core:2.9.4") implementation("com.sparkjava:spark-core:2.9.4")
implementation("com.fasterxml.jackson.core:jackson-core:2.17.0") implementation("com.fasterxml.jackson.core:jackson-core:2.17.0")
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.0") implementation("com.fasterxml.jackson.core:jackson-databind:2.17.0")
implementation("org.slf4j:slf4j-simple:2.0.13") implementation("org.slf4j:slf4j-simple:1.7.30")
implementation("org.xerial:sqlite-jdbc:3.45.3.0") implementation("mysql:mysql-connector-java:8.0.19")
implementation("org.xerial:sqlite-jdbc:3.30.1")
} }
// Apply a specific Java toolchain to ease working on different environments. // Apply a specific Java toolchain to ease working on different environments.

View file

@ -6,12 +6,16 @@ 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.FileReader; import java.io.FileNotFoundException;
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
@ -63,28 +67,37 @@ 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) throws IOException, SQLException { public void migrateScript(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { // Open the file
StringBuilder scriptBuilder = new StringBuilder(); StringBuilder script = new StringBuilder();
String line; try {
File myObj = new File(filename);
Scanner myReader = new Scanner(myObj);
// Read the script file line by line while (myReader.hasNextLine()) {
while ((line = reader.readLine()) != null) { String data = myReader.nextLine();
scriptBuilder.append(line).append("\n"); script.append(data);
}
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 // Execute the script
try (Statement statement = conn.createStatement()) { try {
statement.execute(script); conn.setAutoCommit(false);
} Statement stmt = conn.createStatement();
System.out.println(String.format("Executed script %s", filename)); stmt.execute(script.toString());
} catch (IOException e) { conn.commit();
System.err.println("Error reading script file: " + e.getMessage()); conn.setAutoCommit(true);
throw e;
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Error executing script: " + e.getMessage()); System.out.println("Error executing migration script...");
throw e; e.printStackTrace();
} finally {
System.out.println("Migration script executed successfully...");
} }
} }
} }

View file

@ -19,12 +19,7 @@ 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.
try {
db.migrateScript("Migrations/0010-tables.sql"); db.migrateScript("Migrations/0010-tables.sql");
db.migrateScript("Migrations/0020-data.sql");
} catch (Exception e) {
throw new IOError(e);
}
port(PORT); port(PORT);