]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
back_end: Update back-end for new "phrase" column
authorStephen Seo <seo.disparate@gmail.com>
Wed, 27 Apr 2022 02:15:53 +0000 (11:15 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 27 Apr 2022 02:15:53 +0000 (11:15 +0900)
back_end/src/db_handler.rs
specifications/backend_database_specification.md

index 2e912dfc87a1f781b8a31e4f6a3b16367d06c9c9..dea6fad180cbc5d78ebd0bd3cff92dea7f0ed63d 100644 (file)
@@ -275,6 +275,7 @@ impl DBHandler {
                 CREATE TABLE players (id INTEGER PRIMARY KEY NOT NULL,
                     date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
                     game_id INTEGER,
+                    phrase TEXT,
                     FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE);
             ",
                 [],
@@ -285,6 +286,9 @@ impl DBHandler {
                 }
             } else if first_run == DBFirstRun::FirstRun {
                 println!("\"players\" table exists");
+                if let Err(e) = self.db_check_migration(&conn) {
+                    println!("{}", e);
+                }
             }
 
             let result = conn.execute(
@@ -314,6 +318,29 @@ impl DBHandler {
         }
     }
 
+    fn db_check_migration(&self, conn: &Connection) -> Result<(), String> {
+        let mut table_entries_stmt = conn
+            .prepare("PRAGMA table_info(players);")
+            .map_err(|e| format!("{:?}", e))?;
+        let mut table_entries_rows = table_entries_stmt
+            .query([])
+            .map_err(|e| format!("{:?}", e))?;
+        // check if "phrase" column exists
+        let mut phrase_exists = false;
+        while let Some(row) = table_entries_rows.next().map_err(|e| format!("{:?}", e))? {
+            let column_name: String = row.get(1).map_err(|e| format!("{:?}", e))?;
+            if column_name.contains("phrase") {
+                phrase_exists = true;
+            }
+        }
+        if !phrase_exists {
+            conn.execute("ALTER TABLE players ADD COLUMN phrase TEXT;", [])
+                .map_err(|e| format!("{:?}", e))?;
+            println!("Added \"phrase\" column to \"players\" in db.");
+        }
+        Ok(())
+    }
+
     fn create_new_player(&self, conn: Option<&Connection>) -> Result<u32, String> {
         let mut _conn_result = Err(String::new());
         let conn = if conn.is_none() {
index 41e340f782430d6dbda28fc01e16c99c97064c09..6c8e427ce5259d321aa9be2f0f33b04c03a198cf 100644 (file)
@@ -10,9 +10,14 @@ IDs, and paired state), and games in progress.
 PRAGMA foreign_keys = ON;
 
 // fields should be self explanatory for the players table
+
+// "phrase" is used to connect players with identical "phrase" text to make it
+// easier to connect with the player one wants to play with
+
 CREATE TABLE players (id INTEGER PRIMARY KEY NOT NULL,
                       date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
                       game_id INTEGER,
+                      phrase TEXT,
                       FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE);
 
 // "cyan_player" and "magenta_player" should correspond to an existing entry in