]> gitweb.ps.run Git - l21s-case-study/commitdiff
update db name, update sql
authorpatrick-scho <patrick.schoenberger@posteo.de>
Mon, 24 Nov 2025 15:15:36 +0000 (15:15 +0000)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Mon, 24 Nov 2025 15:15:36 +0000 (15:15 +0000)
src/DbMigration.java

index 9357811ad90a2d791bf77018a9e03491b1005bcc..2675c3fb6c051c7bc43206d7420cf92ad9c01c8c 100644 (file)
@@ -7,33 +7,33 @@ class DbMigration {
     // 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 {{{
@@ -44,6 +44,7 @@ class DbMigration {
                 .map(line -> line.split(CSV_SEPARATOR))
                 .toArray(String[][]::new);
     }
+
     // }}}
 
     // db {{{
@@ -73,6 +74,7 @@ class DbMigration {
 
         return users.toArray(User[]::new);
     }
+
     // }}}
 
     // main {{{
@@ -125,7 +127,7 @@ class DbMigration {
             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);
                 }
@@ -140,6 +142,7 @@ class DbMigration {
         // open db connection for updating {{{
         try (var conn = DriverManager.getConnection(DB_URL);
                 var stmt = conn.prepareStatement(SQL_UPSERT_USER); ) {
+
             // batching
             conn.setAutoCommit(false);
 
@@ -156,6 +159,7 @@ class DbMigration {
             // commit batch
             stmt.executeBatch();
             conn.commit();
+        
         } catch (SQLException e) {
             System.out.printf("Expection occured while accessing database: %s\n", e);
             return;