From 87d93e5b4f480d1d357f43d64d0689541c3d2859 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 27 Apr 2022 11:15:53 +0900 Subject: [PATCH] back_end: Update back-end for new "phrase" column --- back_end/src/db_handler.rs | 27 +++++++++++++++++++ .../backend_database_specification.md | 5 ++++ 2 files changed, 32 insertions(+) diff --git a/back_end/src/db_handler.rs b/back_end/src/db_handler.rs index 2e912df..dea6fad 100644 --- a/back_end/src/db_handler.rs +++ b/back_end/src/db_handler.rs @@ -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 { let mut _conn_result = Err(String::new()); let conn = if conn.is_none() { diff --git a/specifications/backend_database_specification.md b/specifications/backend_database_specification.md index 41e340f..6c8e427 100644 --- a/specifications/backend_database_specification.md +++ b/specifications/backend_database_specification.md @@ -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