From 219ec4df9a28a816cd6c1ccbdce92804ad21b75d Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 10 Mar 2025 21:44:21 -0700 Subject: [PATCH] Admin-cli now creates minimum tables for users --- admin-cli/src/main.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/admin-cli/src/main.rs b/admin-cli/src/main.rs index 5dcce2c..704ede4 100644 --- a/admin-cli/src/main.rs +++ b/admin-cli/src/main.rs @@ -15,6 +15,9 @@ struct Args { /// Setup everything from scratch #[arg(short, long)] setup: bool, + /// Specify the directory where all the sql files are located + #[arg(short, long)] + psql_dir: Option } #[derive(Serialize)] @@ -39,20 +42,24 @@ fn random_string(size: usize) -> String { fn admin(username: &str, password_size: usize) -> Admin { Admin { - username: format!("admin-{}", username), + username: username.to_string(), password: random_string(password_size) } } -fn full_setup() -> Result { +fn full_setup(args: Args) -> Result { // Check to make sure we have the DB url set to connect const KEY: &'static str = "DB_CONNECTION_STRING" ; let connection_string = env::var(KEY).expect( &format!("The env var {} is not set!", KEY) ); - let setup_tables_script = fs::read_to_string("db/setup-tables.sql") + let db_folder = match args.psql_dir { + Some(val) => format!("{}/setup-tables.sql", val), + None => "db/setup-tables.sql".to_string() + }; + let setup_tables_script = fs::read_to_string(db_folder) .expect("Failed to load file: db/setup-tables.sql"); - let postgres_admin = admin("bubble_admin", PASSWORD_LENGTH); + let postgres_admin = admin("bubble_admin-", PASSWORD_LENGTH); let bubble_admin = admin( &format!("admin-{}", random_string(8)), PASSWORD_LENGTH @@ -63,21 +70,19 @@ fn full_setup() -> Result { client.execute("DROP DATABASE IF EXISTS bubble;", &[])?; client.execute("CREATE DATABASE bubble;", &[])?; client.execute("DROP USER IF EXISTS bubble_admin;", &[])?; - client.execute( - &format!("CREATE USER bubble_admin WITH ENCRYPTED PASSWORD '{}';", postgres_admin.password), - &[] - )?; + let stmt = format!("CREATE ROLE bubble_admin WITH ENCRYPTED PASSWORD '{}'", postgres_admin.password); + client.execute(&stmt , &[])?; + // Ensure the admin has ownership of the db we created client.execute("ALTER DATABASE bubble OWNER TO bubble_admin", &[])?; + // Service table creation client.batch_execute(&setup_tables_script)?; + + // Populate the user table with the first user ( owner ) client.execute( - &format!( - "INSERT INTO users (name, password) VALUES '{}', '{}'", - bubble_admin.username, - bubble_admin.password - ), - &[] + "INSERT INTO users (id, username, password) VALUES (gen_random_uuid(), $1, $2)", + &[&bubble_admin.username, &bubble_admin.password] )?; Ok(Config { postgres: postgres_admin, @@ -89,7 +94,7 @@ fn main() { let args = Args::parse(); if args.setup { - match full_setup() { + match full_setup(args) { Ok(config) => println!("{}", serde_json::to_string(&config).unwrap()), Err(e) => eprintln!("{:#?}", e) }