Migrating the databse
This commit is contained in:
parent
83ca28259a
commit
8a88e8b45a
2 changed files with 68 additions and 22 deletions
|
@ -3,32 +3,27 @@ package krusty;
|
|||
import spark.Request;
|
||||
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;
|
||||
|
||||
// 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;
|
||||
|
||||
public class Database {
|
||||
private static final String jdbcString = "jdbc:sqlite:krusty.sqlite3";
|
||||
|
||||
public void connect() {
|
||||
Connection conn = null;
|
||||
|
||||
try {
|
||||
conn = DriverManager.getConnection(jdbcString);
|
||||
System.out.println("Connection to SQLite has been established.");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement and change output in all methods below!
|
||||
// Here, we use an in-memory database. This string could be changed to
|
||||
// "jdbc:sqlite:<filename>.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 Connection conn = null;
|
||||
|
||||
public String getCustomers(Request req, Response res) {
|
||||
return "{}";
|
||||
|
@ -57,4 +52,52 @@ public class Database {
|
|||
public String createPallet(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
/** Connects to the database using the configured jdbcString. */
|
||||
public void connect() {
|
||||
try {
|
||||
conn = DriverManager.getConnection(jdbcString);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
System.out.println("Connection to SQLite has been established.");
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
while (myReader.hasNextLine()) {
|
||||
String data = myReader.nextLine();
|
||||
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...");
|
||||
}
|
||||
|
||||
// Execute the script
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute(script.toString());
|
||||
conn.commit();
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error executing migration script...");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
System.out.println("Migration script executed successfully...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,9 @@ public class ServerMain {
|
|||
db = new Database();
|
||||
db.connect();
|
||||
|
||||
// Here, we can migrate an arbitrary number of SQL scripts.
|
||||
db.migrateScript("CrustyCookies/Migrations/0010_users.sql");
|
||||
|
||||
port(PORT);
|
||||
|
||||
enableCORS();
|
||||
|
|
Loading…
Reference in a new issue