back_end: Update back-end for new "phrase" column

This commit is contained in:
Stephen Seo 2022-04-27 11:15:53 +09:00
parent e060d94186
commit 87d93e5b4f
2 changed files with 32 additions and 0 deletions

View file

@ -275,6 +275,7 @@ impl DBHandler {
CREATE TABLE players (id INTEGER PRIMARY KEY NOT NULL, CREATE TABLE players (id INTEGER PRIMARY KEY NOT NULL,
date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
game_id INTEGER, game_id INTEGER,
phrase TEXT,
FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE); FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE);
", ",
[], [],
@ -285,6 +286,9 @@ impl DBHandler {
} }
} else if first_run == DBFirstRun::FirstRun { } else if first_run == DBFirstRun::FirstRun {
println!("\"players\" table exists"); println!("\"players\" table exists");
if let Err(e) = self.db_check_migration(&conn) {
println!("{}", e);
}
} }
let result = conn.execute( 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> { fn create_new_player(&self, conn: Option<&Connection>) -> Result<u32, String> {
let mut _conn_result = Err(String::new()); let mut _conn_result = Err(String::new());
let conn = if conn.is_none() { let conn = if conn.is_none() {

View file

@ -10,9 +10,14 @@ IDs, and paired state), and games in progress.
PRAGMA foreign_keys = ON; PRAGMA foreign_keys = ON;
// fields should be self explanatory for the players table // 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, CREATE TABLE players (id INTEGER PRIMARY KEY NOT NULL,
date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, date_added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
game_id INTEGER, game_id INTEGER,
phrase TEXT,
FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE); FOREIGN KEY(game_id) REFERENCES games(id) ON DELETE CASCADE);
// "cyan_player" and "magenta_player" should correspond to an existing entry in // "cyan_player" and "magenta_player" should correspond to an existing entry in