// 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 {{{
+ 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 {{{
.map(line -> line.split(CSV_SEPARATOR))
.toArray(String[][]::new);
}
+
// }}}
// db {{{
return users.toArray(User[]::new);
}
+
// }}}
// main {{{
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);
}
// open db connection for updating {{{
try (var conn = DriverManager.getConnection(DB_URL);
var stmt = conn.prepareStatement(SQL_UPSERT_USER); ) {
+
// batching
conn.setAutoCommit(false);
// commit batch
stmt.executeBatch();
conn.commit();
+
} catch (SQLException e) {
System.out.printf("Expection occured while accessing database: %s\n", e);
return;