X-Git-Url: https://gitweb.ps.run/l21s-case-study/blobdiff_plain/b8a155727f6ac18b0afed3e1ffb0177b924b4581..7f2fdc9c08a64e53cbedcb3168384df35bf14533:/src/DbMigration.java diff --git a/src/DbMigration.java b/src/DbMigration.java index bcf22d9..2675c3f 100644 --- a/src/DbMigration.java +++ b/src/DbMigration.java @@ -4,36 +4,39 @@ import java.sql.*; import java.util.*; class DbMigration { - // constants + // constants {{{ static final boolean CONFIG_PRINT_DB = false; static final String CSV_SEPARATOR = ","; - static final String DB_URL = "jdbc:postgresql:users"; + static final String DB_URL = "jdbc:postgresql:migration"; static final String SQL_GET_USERS = "SELECT * FROM users;"; static final String SQL_UPSERT_USER = """ - INSERT INTO users VALUES (?, ?, ?, ?, ?) - ON CONFLICT (ID) - DO UPDATE SET - Mail = EXCLUDED.Mail, - CanRead = EXCLUDED.CanRead, - CanWrite = EXCLUDED.CanWrite, - CanDelete = EXCLUDED.CanDelete - ; + INSERT INTO users VALUES (?, ?, ?, ?, ?) + ON CONFLICT (USER_ID) + DO UPDATE SET + MAIL = EXCLUDED.MAIL, + CAN_READ = EXCLUDED.CAN_READ, + CAN_WRITE = EXCLUDED.CAN_WRITE, + CAN_DELETE = EXCLUDED.CAN_DELETE + ; """; - // user + // }}} + + // user {{{ + public record Role(boolean read, boolean write, boolean delete) { + static final Role Default = new Role(false, false, false); + } + public record User(int id, String mail, Role role) { public String toString() { return this.id + " " + this.mail + " " + this.role; } } - // role - public record Role(boolean read, boolean write, boolean delete) { - static final Role Default = new Role(false, false, false); - } + // }}} - // csv + // csv {{{ public static String[][] LoadCSV(String filename) throws IOException { var path = Paths.get(filename); return Files.lines(path) @@ -42,7 +45,9 @@ class DbMigration { .toArray(String[][]::new); } - // db + // }}} + + // db {{{ public static void insertUser(PreparedStatement stmt, User user) throws SQLException { stmt.setInt(1, user.id); stmt.setString(2, user.mail); @@ -70,17 +75,20 @@ class DbMigration { return users.toArray(User[]::new); } - // main + // }}} + + // main {{{ public static void main(String[] args) { - // filenames from command line args + // filenames from command line args {{{ if (args.length != 2) { System.out.printf("Usage: java src/DbMigrations.java \n"); return; } String filenameUsers = args[0]; String filenameRoles = args[1]; + // }}} - // read csvs + // read csvs {{{ String[][] csvUsers; String[][] csvRoles; try { @@ -90,8 +98,9 @@ class DbMigration { System.out.printf("Expection occured while trying to load CSV: %s\n", e); return; } + // }}} - // process roles + // process roles {{{ var roles = new HashMap(); for (var row : csvRoles) { // get id and single role @@ -111,13 +120,14 @@ class DbMigration { return role; }); } + // }}} - // OPTIONAL: open db connection for reading + // OPTIONAL: open db connection for reading {{{ if (CONFIG_PRINT_DB) { try (var conn = DriverManager.getConnection(DB_URL)) { var users = getUsers(conn); - System.out.println("DB before update:"); + System.out.println("Before update:"); for (var user : users) { System.out.println(user); } @@ -127,10 +137,12 @@ class DbMigration { return; } } + // }}} - // open db connection for updating + // open db connection for updating {{{ try (var conn = DriverManager.getConnection(DB_URL); var stmt = conn.prepareStatement(SQL_UPSERT_USER); ) { + // batching conn.setAutoCommit(false); @@ -147,11 +159,14 @@ class DbMigration { // commit batch stmt.executeBatch(); conn.commit(); + } catch (SQLException e) { System.out.printf("Expection occured while accessing database: %s\n", e); return; } + // }}} System.out.println("Updated successfully"); } + // }}} }