Admin-cli now creates minimum tables for users
This commit is contained in:
parent
d83bd0a7bf
commit
219ec4df9a
@ -15,6 +15,9 @@ struct Args {
|
|||||||
/// Setup everything from scratch
|
/// Setup everything from scratch
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
setup: bool,
|
setup: bool,
|
||||||
|
/// Specify the directory where all the sql files are located
|
||||||
|
#[arg(short, long)]
|
||||||
|
psql_dir: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@ -39,20 +42,24 @@ fn random_string(size: usize) -> String {
|
|||||||
|
|
||||||
fn admin(username: &str, password_size: usize) -> Admin {
|
fn admin(username: &str, password_size: usize) -> Admin {
|
||||||
Admin {
|
Admin {
|
||||||
username: format!("admin-{}", username),
|
username: username.to_string(),
|
||||||
password: random_string(password_size)
|
password: random_string(password_size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full_setup() -> Result<Config, postgres::Error> {
|
fn full_setup(args: Args) -> Result<Config, postgres::Error> {
|
||||||
// Check to make sure we have the DB url set to connect
|
// Check to make sure we have the DB url set to connect
|
||||||
const KEY: &'static str = "DB_CONNECTION_STRING" ;
|
const KEY: &'static str = "DB_CONNECTION_STRING" ;
|
||||||
let connection_string = env::var(KEY).expect(
|
let connection_string = env::var(KEY).expect(
|
||||||
&format!("The env var {} is not set!", KEY)
|
&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");
|
.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(
|
let bubble_admin = admin(
|
||||||
&format!("admin-{}", random_string(8)),
|
&format!("admin-{}", random_string(8)),
|
||||||
PASSWORD_LENGTH
|
PASSWORD_LENGTH
|
||||||
@ -63,21 +70,19 @@ fn full_setup() -> Result<Config, postgres::Error> {
|
|||||||
client.execute("DROP DATABASE IF EXISTS bubble;", &[])?;
|
client.execute("DROP DATABASE IF EXISTS bubble;", &[])?;
|
||||||
client.execute("CREATE DATABASE bubble;", &[])?;
|
client.execute("CREATE DATABASE bubble;", &[])?;
|
||||||
client.execute("DROP USER IF EXISTS bubble_admin;", &[])?;
|
client.execute("DROP USER IF EXISTS bubble_admin;", &[])?;
|
||||||
client.execute(
|
let stmt = format!("CREATE ROLE bubble_admin WITH ENCRYPTED PASSWORD '{}'", postgres_admin.password);
|
||||||
&format!("CREATE USER bubble_admin WITH ENCRYPTED PASSWORD '{}';", postgres_admin.password),
|
client.execute(&stmt , &[])?;
|
||||||
&[]
|
|
||||||
)?;
|
|
||||||
// Ensure the admin has ownership of the db we created
|
// Ensure the admin has ownership of the db we created
|
||||||
client.execute("ALTER DATABASE bubble OWNER TO bubble_admin", &[])?;
|
client.execute("ALTER DATABASE bubble OWNER TO bubble_admin", &[])?;
|
||||||
|
|
||||||
// Service table creation
|
// Service table creation
|
||||||
client.batch_execute(&setup_tables_script)?;
|
client.batch_execute(&setup_tables_script)?;
|
||||||
|
|
||||||
|
// Populate the user table with the first user ( owner )
|
||||||
client.execute(
|
client.execute(
|
||||||
&format!(
|
"INSERT INTO users (id, username, password) VALUES (gen_random_uuid(), $1, $2)",
|
||||||
"INSERT INTO users (name, password) VALUES '{}', '{}'",
|
&[&bubble_admin.username, &bubble_admin.password]
|
||||||
bubble_admin.username,
|
|
||||||
bubble_admin.password
|
|
||||||
),
|
|
||||||
&[]
|
|
||||||
)?;
|
)?;
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
postgres: postgres_admin,
|
postgres: postgres_admin,
|
||||||
@ -89,7 +94,7 @@ fn main() {
|
|||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
if args.setup {
|
if args.setup {
|
||||||
match full_setup() {
|
match full_setup(args) {
|
||||||
Ok(config) => println!("{}", serde_json::to_string(&config).unwrap()),
|
Ok(config) => println!("{}", serde_json::to_string(&config).unwrap()),
|
||||||
Err(e) => eprintln!("{:#?}", e)
|
Err(e) => eprintln!("{:#?}", e)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user