From 7610827859de6bea15425561cd6eff04b9e33f8f Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Thu, 30 May 2024 14:34:15 -0300 Subject: [PATCH 1/7] feat: add supervisor database commands --- zk_toolbox/Cargo.lock | 11 ++ zk_toolbox/Cargo.toml | 5 +- zk_toolbox/crates/common/Cargo.toml | 3 +- zk_toolbox/crates/common/src/cmd.rs | 28 +++-- zk_toolbox/crates/common/src/db.rs | 73 +++++++++-- zk_toolbox/crates/common/src/term/logger.rs | 6 +- zk_toolbox/crates/common/src/term/spinner.rs | 9 ++ zk_toolbox/crates/config/src/secrets.rs | 29 +---- zk_toolbox/crates/zk_inception/Cargo.toml | 1 + .../src/commands/chain/args/genesis.rs | 42 ++----- .../src/commands/chain/genesis.rs | 30 ++--- .../src/commands/ecosystem/mod.rs | 1 + .../zk_inception/src/config_manipulations.rs | 10 +- .../crates/zk_inception/src/defaults.rs | 10 +- zk_toolbox/crates/zk_supervisor/Cargo.toml | 9 ++ .../src/commands/database/args/mod.rs | 36 ++++++ .../commands/database/args/new_migration.rs | 46 +++++++ .../src/commands/database/check_sqlx_data.rs | 49 ++++++++ .../src/commands/database/drop.rs | 36 ++++++ .../src/commands/database/migrate.rs | 45 +++++++ .../src/commands/database/mod.rs | 43 +++++++ .../src/commands/database/new_migration.rs | 41 +++++++ .../src/commands/database/prepare.rs | 49 ++++++++ .../src/commands/database/reset.rs | 38 ++++++ .../src/commands/database/setup.rs | 50 ++++++++ .../crates/zk_supervisor/src/commands/mod.rs | 1 + zk_toolbox/crates/zk_supervisor/src/dals.rs | 68 +++++++++++ zk_toolbox/crates/zk_supervisor/src/main.rs | 114 +++++++++++++++++- 28 files changed, 783 insertions(+), 100 deletions(-) create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/mod.rs create mode 100644 zk_toolbox/crates/zk_supervisor/src/dals.rs diff --git a/zk_toolbox/Cargo.lock b/zk_toolbox/Cargo.lock index 1469b183152b..1401ca022904 100644 --- a/zk_toolbox/Cargo.lock +++ b/zk_toolbox/Cargo.lock @@ -531,6 +531,7 @@ dependencies = [ "serde_yaml", "sqlx", "strum_macros 0.26.2", + "tokio", "toml", "url", "xshell", @@ -4533,6 +4534,7 @@ dependencies = [ "console", "ethers", "human-panic", + "lazy_static", "serde", "serde_json", "serde_yaml", @@ -4550,7 +4552,16 @@ dependencies = [ name = "zk_supervisor" version = "0.1.0" dependencies = [ + "anyhow", + "clap", + "common", + "config", "human-panic", + "strum 0.26.2", + "strum_macros 0.26.2", + "tokio", + "url", + "xshell", ] [[package]] diff --git a/zk_toolbox/Cargo.toml b/zk_toolbox/Cargo.toml index 539c656292a4..ae4b40fa435e 100644 --- a/zk_toolbox/Cargo.toml +++ b/zk_toolbox/Cargo.toml @@ -32,7 +32,9 @@ clap = { version = "4.4", features = ["derive", "wrap_help"] } cliclack = "0.2.5" console = "0.15.8" ethers = "2.0" +futures = "0.3.30" human-panic = "2.0" +lazy_static = "1.4.0" once_cell = "1.19.0" rand = "0.8.5" serde = { version = "1.0", features = ["derive"] } @@ -41,9 +43,8 @@ serde_yaml = "0.9" sqlx = { version = "0.7.4", features = ["runtime-tokio", "migrate", "postgres"] } strum = "0.26.2" strum_macros = "0.26.2" +thiserror = "1.0.57" tokio = { version = "1.37", features = ["full"] } toml = "0.8.12" url = { version = "2.5.0", features = ["serde"] } xshell = "0.2.6" -futures = "0.3.30" -thiserror = "1.0.57" diff --git a/zk_toolbox/crates/common/Cargo.toml b/zk_toolbox/crates/common/Cargo.toml index efdde1cdfc18..00c3b7775112 100644 --- a/zk_toolbox/crates/common/Cargo.toml +++ b/zk_toolbox/crates/common/Cargo.toml @@ -16,13 +16,14 @@ clap.workspace = true cliclack.workspace = true console.workspace = true ethers.workspace = true +futures.workspace = true once_cell.workspace = true serde.workspace = true serde_json.workspace = true serde_yaml.workspace = true sqlx.workspace = true strum_macros.workspace = true +tokio.workspace = true toml.workspace = true url.workspace = true xshell.workspace = true -futures.workspace = true diff --git a/zk_toolbox/crates/common/src/cmd.rs b/zk_toolbox/crates/common/src/cmd.rs index 8b18c7733059..e39f1e18972c 100644 --- a/zk_toolbox/crates/common/src/cmd.rs +++ b/zk_toolbox/crates/common/src/cmd.rs @@ -1,3 +1,5 @@ +use std::process::Output; + use anyhow::bail; use console::style; @@ -31,13 +33,6 @@ impl<'a> Cmd<'a> { /// Run the command without capturing its output. pub fn run(&mut self) -> anyhow::Result<()> { - self.run_cmd()?; - Ok(()) - } - - /// Run the command and capture its output, logging the command - /// and its output if verbose selected. - fn run_cmd(&mut self) -> anyhow::Result<()> { if global_config().verbose || self.force_run { logger::debug(format!("Running: {}", self.inner)); logger::new_empty_line(); @@ -60,6 +55,25 @@ impl<'a> Cmd<'a> { Ok(()) } + /// Run the command and return its output. + pub fn run_with_output(&mut self) -> anyhow::Result { + if global_config().verbose || self.force_run { + logger::debug(format!("Running: {}", self.inner)); + logger::new_empty_line(); + } + + self.inner.set_ignore_status(true); + let output = self.inner.output()?; + + if global_config().verbose || self.force_run { + logger::raw(log_output(&output)); + logger::new_empty_line(); + logger::new_line(); + } + + Ok(output) + } + fn check_output_status(&self, output: &std::process::Output) -> anyhow::Result<()> { if !output.status.success() { logger::new_line(); diff --git a/zk_toolbox/crates/common/src/db.rs b/zk_toolbox/crates/common/src/db.rs index 887880b2c55c..c0a681bc74c0 100644 --- a/zk_toolbox/crates/common/src/db.rs +++ b/zk_toolbox/crates/common/src/db.rs @@ -1,5 +1,7 @@ use std::{collections::HashMap, path::PathBuf}; +use anyhow::anyhow; +use serde::{Deserialize, Serialize}; use sqlx::{ migrate::{Migrate, MigrateError, Migrator}, Connection, PgConnection, @@ -9,22 +11,63 @@ use xshell::Shell; use crate::{config::global_config, logger}; -pub async fn init_db(db_url: &Url, name: &str) -> anyhow::Result<()> { +/// Database configuration. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DatabaseConfig { + /// Database URL. + pub url: Url, + /// Database name. + pub name: String, +} + +impl DatabaseConfig { + /// Create a new `Db` instance. + pub fn new(url: Url, name: String) -> Self { + Self { url, name } + } + + /// Create a new `Db` instance from a URL. + pub fn from_url(url: Url) -> anyhow::Result { + let name = url + .path_segments() + .ok_or(anyhow!("Failed to parse database name from URL"))? + .last() + .ok_or(anyhow!("Failed to parse database name from URL"))?; + let url_without_db_name = { + let mut url = url.clone(); + url.set_path(""); + url + }; + Ok(Self { + url: url_without_db_name, + name: name.to_string(), + }) + } + + /// Get the full URL of the database. + pub fn full_url(&self) -> Url { + let mut url = self.url.clone(); + url.set_path(&self.name); + url + } +} + +pub async fn init_db(db: &DatabaseConfig) -> anyhow::Result<()> { // Connect to the database. - let mut connection = PgConnection::connect(db_url.as_ref()).await?; + let mut connection = PgConnection::connect(db.url.as_str()).await?; - let query = format!("CREATE DATABASE {}", name); + let query = format!("CREATE DATABASE {}", db.name); // Create DB. sqlx::query(&query).execute(&mut connection).await?; Ok(()) } -pub async fn drop_db_if_exists(db_url: &Url, name: &str) -> anyhow::Result<()> { +pub async fn drop_db_if_exists(db: &DatabaseConfig) -> anyhow::Result<()> { // Connect to the database. - let mut connection = PgConnection::connect(db_url.as_ref()).await?; + let mut connection = PgConnection::connect(db.url.as_str()).await?; - let query = format!("DROP DATABASE IF EXISTS {}", name); + let query = format!("DROP DATABASE IF EXISTS {}", db.name); // DROP DB. sqlx::query(&query).execute(&mut connection).await?; @@ -34,7 +77,7 @@ pub async fn drop_db_if_exists(db_url: &Url, name: &str) -> anyhow::Result<()> { pub async fn migrate_db( shell: &Shell, migrations_folder: PathBuf, - db_url: &str, + db_url: &Url, ) -> anyhow::Result<()> { // Most of this file is copy-pasted from SQLx CLI: // https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/src/migrate.rs @@ -45,7 +88,7 @@ pub async fn migrate_db( } let migrator = Migrator::new(migrations_folder).await?; - let mut conn = PgConnection::connect(db_url).await?; + let mut conn = PgConnection::connect(db_url.as_str()).await?; conn.ensure_migrations_table().await?; let version = conn.dirty_version().await?; @@ -83,7 +126,7 @@ pub async fn migrate_db( let text = if skip { "Skipped" } else { "Applied" }; if global_config().verbose { - logger::raw(&format!( + logger::step(&format!( " {} {}/{} {} ({elapsed:?})", text, migration.version, @@ -104,3 +147,15 @@ pub async fn migrate_db( Ok(()) } + +pub async fn wait_for_db(db_url: &Url, tries: u32) -> anyhow::Result<()> { + for i in 0..tries { + if PgConnection::connect(db_url.as_str()).await.is_ok() { + return Ok(()); + } + if i < tries - 1 { + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } + } + anyhow::bail!("Unable to connect to Postgres, connection cannot be established"); +} diff --git a/zk_toolbox/crates/common/src/term/logger.rs b/zk_toolbox/crates/common/src/term/logger.rs index 9e13c2958078..b505123114be 100644 --- a/zk_toolbox/crates/common/src/term/logger.rs +++ b/zk_toolbox/crates/common/src/term/logger.rs @@ -43,10 +43,14 @@ pub fn success(msg: impl Display) { log::success(msg).unwrap(); } -pub fn raw(msg: impl Display) { +pub fn step(msg: impl Display) { log::step(msg).unwrap(); } +pub fn raw(msg: impl Display) { + term_write(msg); +} + pub fn note(msg: impl Display, content: impl Display) { cliclack::note(msg, content).unwrap(); } diff --git a/zk_toolbox/crates/common/src/term/spinner.rs b/zk_toolbox/crates/common/src/term/spinner.rs index 3e9322ba636c..dcfaaf44d44d 100644 --- a/zk_toolbox/crates/common/src/term/spinner.rs +++ b/zk_toolbox/crates/common/src/term/spinner.rs @@ -34,4 +34,13 @@ impl Spinner { self.time.elapsed().as_secs_f64() )); } + + /// Interrupt the spinner with a failed message. + pub fn fail(self) { + self.pb.error(format!( + "{} failed in {} secs", + self.msg, + self.time.elapsed().as_secs_f64() + )); + } } diff --git a/zk_toolbox/crates/config/src/secrets.rs b/zk_toolbox/crates/config/src/secrets.rs index 829d903adb66..ebacc5d437cb 100644 --- a/zk_toolbox/crates/config/src/secrets.rs +++ b/zk_toolbox/crates/config/src/secrets.rs @@ -5,8 +5,8 @@ use crate::{consts::SECRETS_FILE, traits::FileConfigWithDefaultName}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DatabaseSecrets { - pub server_url: String, - pub prover_url: String, + pub server_url: Url, + pub prover_url: Url, #[serde(flatten)] pub other: serde_json::Value, } @@ -29,28 +29,3 @@ pub struct SecretsConfig { impl FileConfigWithDefaultName for SecretsConfig { const FILE_NAME: &'static str = SECRETS_FILE; } - -#[derive(Debug, Serialize)] -pub struct DatabaseConfig { - pub base_url: Url, - pub database_name: String, -} - -impl DatabaseConfig { - pub fn new(base_url: Url, database_name: String) -> Self { - Self { - base_url, - database_name, - } - } - - pub fn full_url(&self) -> String { - format!("{}/{}", self.base_url, self.database_name) - } -} - -#[derive(Debug, Serialize)] -pub struct DatabasesConfig { - pub server: DatabaseConfig, - pub prover: DatabaseConfig, -} diff --git a/zk_toolbox/crates/zk_inception/Cargo.toml b/zk_toolbox/crates/zk_inception/Cargo.toml index 8123746f1abf..ff22e982e3cc 100644 --- a/zk_toolbox/crates/zk_inception/Cargo.toml +++ b/zk_toolbox/crates/zk_inception/Cargo.toml @@ -17,6 +17,7 @@ cliclack.workspace = true config.workspace = true console.workspace = true human-panic.workspace = true +lazy_static.workspace = true serde_yaml.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs index c8229066a2eb..c5a761fb74b4 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs @@ -1,6 +1,6 @@ use clap::Parser; -use common::{slugify, Prompt}; -use config::{ChainConfig, DatabaseConfig, DatabasesConfig}; +use common::{db::DatabaseConfig, slugify, Prompt}; +use config::ChainConfig; use serde::{Deserialize, Serialize}; use url::Url; @@ -9,11 +9,11 @@ use crate::defaults::{generate_db_names, DBNames, DATABASE_PROVER_URL, DATABASE_ #[derive(Debug, Clone, Serialize, Deserialize, Parser, Default)] pub struct GenesisArgs { #[clap(long, help = "Server database url without database name")] - pub server_db_url: Option, + pub server_db_url: Option, #[clap(long, help = "Server database name")] pub server_db_name: Option, #[clap(long, help = "Prover database url without database name")] - pub prover_db_url: Option, + pub prover_db_url: Option, #[clap(long, help = "Prover database name")] pub prover_db_name: Option, #[clap(long, short, help = "Use default database urls and names")] @@ -31,10 +31,8 @@ impl GenesisArgs { let chain_name = config.name.clone(); if self.use_default { GenesisArgsFinal { - server_db_url: DATABASE_SERVER_URL.to_string(), - server_db_name: server_name, - prover_db_url: DATABASE_PROVER_URL.to_string(), - prover_db_name: prover_name, + server_db: DatabaseConfig::new(DATABASE_SERVER_URL.clone(), server_name), + prover_db: DatabaseConfig::new(DATABASE_PROVER_URL.clone(), prover_name), dont_drop: self.dont_drop, } } else { @@ -42,7 +40,7 @@ impl GenesisArgs { Prompt::new(&format!( "Please provide server database url for chain {chain_name}" )) - .default(DATABASE_SERVER_URL) + .default(DATABASE_SERVER_URL.as_str()) .ask() }); let server_db_name = slugify(&self.server_db_name.unwrap_or_else(|| { @@ -56,7 +54,7 @@ impl GenesisArgs { Prompt::new(&format!( "Please provide prover database url for chain {chain_name}" )) - .default(DATABASE_PROVER_URL) + .default(DATABASE_PROVER_URL.as_str()) .ask() }); let prover_db_name = slugify(&self.prover_db_name.unwrap_or_else(|| { @@ -67,10 +65,8 @@ impl GenesisArgs { .ask() })); GenesisArgsFinal { - server_db_url, - server_db_name, - prover_db_url, - prover_db_name, + server_db: DatabaseConfig::new(server_db_url, server_db_name), + prover_db: DatabaseConfig::new(prover_db_url, prover_db_name), dont_drop: self.dont_drop, } } @@ -79,21 +75,7 @@ impl GenesisArgs { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct GenesisArgsFinal { - pub server_db_url: String, - pub server_db_name: String, - pub prover_db_url: String, - pub prover_db_name: String, + pub server_db: DatabaseConfig, + pub prover_db: DatabaseConfig, pub dont_drop: bool, } - -impl GenesisArgsFinal { - pub fn databases_config(&self) -> anyhow::Result { - let server_url = Url::parse(&self.server_db_url)?; - let prover_url = Url::parse(&self.prover_db_url)?; - - Ok(DatabasesConfig { - server: DatabaseConfig::new(server_url, self.server_db_name.clone()), - prover: DatabaseConfig::new(prover_url, self.prover_db_name.clone()), - }) - } -} diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs index 1bc9d8dd0c36..0909187abe17 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/genesis.rs @@ -3,11 +3,11 @@ use std::path::PathBuf; use anyhow::Context; use common::{ config::global_config, - db::{drop_db_if_exists, init_db, migrate_db}, + db::{drop_db_if_exists, init_db, migrate_db, DatabaseConfig}, logger, spinner::Spinner, }; -use config::{ChainConfig, DatabasesConfig, EcosystemConfig}; +use config::{ChainConfig, EcosystemConfig}; use xshell::Shell; use super::args::genesis::GenesisArgsFinal; @@ -43,17 +43,15 @@ pub async fn genesis( shell.remove_path(&config.rocks_db_path)?; shell.create_dir(&config.rocks_db_path)?; - let db_config = args - .databases_config() - .context("Database config was not fully generated")?; update_general_config(shell, config)?; - update_database_secrets(shell, config, &db_config)?; + update_database_secrets(shell, config, &args.server_db, &args.prover_db)?; logger::note( "Selected config:", logger::object_to_string(serde_json::json!({ "chain_config": config, - "db_config": db_config, + "server_db_config": args.server_db, + "prover_db_config": args.prover_db, })), ); logger::info("Starting genesis process"); @@ -61,7 +59,8 @@ pub async fn genesis( let spinner = Spinner::new("Initializing databases..."); initialize_databases( shell, - db_config, + &args.server_db, + &args.prover_db, config.link_to_code.clone(), args.dont_drop, ) @@ -79,7 +78,8 @@ pub async fn genesis( async fn initialize_databases( shell: &Shell, - db_config: DatabasesConfig, + server_db_config: &DatabaseConfig, + prover_db_config: &DatabaseConfig, link_to_code: PathBuf, dont_drop: bool, ) -> anyhow::Result<()> { @@ -89,15 +89,15 @@ async fn initialize_databases( logger::debug("Initializing server database") } if !dont_drop { - drop_db_if_exists(&db_config.server.base_url, &db_config.server.database_name) + drop_db_if_exists(server_db_config) .await .context("Failed to drop server database")?; - init_db(&db_config.server.base_url, &db_config.server.database_name).await?; + init_db(server_db_config).await?; } migrate_db( shell, path_to_server_migration, - &db_config.server.full_url(), + &server_db_config.full_url(), ) .await?; @@ -105,16 +105,16 @@ async fn initialize_databases( logger::debug("Initializing prover database") } if !dont_drop { - drop_db_if_exists(&db_config.prover.base_url, &db_config.prover.database_name) + drop_db_if_exists(prover_db_config) .await .context("Failed to drop prover database")?; - init_db(&db_config.prover.base_url, &db_config.prover.database_name).await?; + init_db(prover_db_config).await?; } let path_to_prover_migration = link_to_code.join(PROVER_MIGRATIONS); migrate_db( shell, path_to_prover_migration, - &db_config.prover.full_url(), + &prover_db_config.full_url(), ) .await?; diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/mod.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/mod.rs index 1e232b5cf6c6..e2db65b213f8 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/mod.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/mod.rs @@ -12,6 +12,7 @@ pub mod create_configs; mod init; #[derive(Subcommand, Debug)] +#[allow(clippy::large_enum_variant)] pub enum EcosystemCommands { /// Create a new ecosystem and chain, /// setting necessary configurations for later initialization diff --git a/zk_toolbox/crates/zk_inception/src/config_manipulations.rs b/zk_toolbox/crates/zk_inception/src/config_manipulations.rs index a5edcb7bde4a..3c350fa8d894 100644 --- a/zk_toolbox/crates/zk_inception/src/config_manipulations.rs +++ b/zk_toolbox/crates/zk_inception/src/config_manipulations.rs @@ -1,10 +1,11 @@ +use common::db::DatabaseConfig; use config::{ forge_interface::{ initialize_bridges::output::InitializeBridgeOutput, paymaster::DeployPaymasterOutput, register_chain::output::RegisterChainOutput, }, traits::{ReadConfigWithBasePath, SaveConfigWithBasePath}, - ChainConfig, ContractsConfig, DatabasesConfig, GeneralConfig, GenesisConfig, SecretsConfig, + ChainConfig, ContractsConfig, GeneralConfig, GenesisConfig, SecretsConfig, }; use types::ProverMode; use xshell::Shell; @@ -25,11 +26,12 @@ pub(crate) fn update_genesis(shell: &Shell, config: &ChainConfig) -> anyhow::Res pub(crate) fn update_database_secrets( shell: &Shell, config: &ChainConfig, - db_config: &DatabasesConfig, + server_db_config: &DatabaseConfig, + prover_db_config: &DatabaseConfig, ) -> anyhow::Result<()> { let mut secrets = SecretsConfig::read_with_base_path(shell, &config.configs)?; - secrets.database.server_url = db_config.server.full_url(); - secrets.database.prover_url = db_config.prover.full_url(); + secrets.database.server_url = server_db_config.full_url(); + secrets.database.prover_url = prover_db_config.full_url(); secrets.save_with_base_path(shell, &config.configs)?; Ok(()) } diff --git a/zk_toolbox/crates/zk_inception/src/defaults.rs b/zk_toolbox/crates/zk_inception/src/defaults.rs index 4b768abe907d..04b735e02275 100644 --- a/zk_toolbox/crates/zk_inception/src/defaults.rs +++ b/zk_toolbox/crates/zk_inception/src/defaults.rs @@ -1,7 +1,13 @@ use config::ChainConfig; +use lazy_static::lazy_static; +use url::Url; -pub const DATABASE_SERVER_URL: &str = "postgres://postgres:notsecurepassword@localhost:5432"; -pub const DATABASE_PROVER_URL: &str = "postgres://postgres:notsecurepassword@localhost:5432"; +lazy_static! { + pub static ref DATABASE_SERVER_URL: Url = + Url::parse("postgres://postgres:notsecurepassword@localhost:5432").unwrap(); + pub static ref DATABASE_PROVER_URL: Url = + Url::parse("postgres://postgres:notsecurepassword@localhost:5432").unwrap(); +} pub const ROCKS_DB_STATE_KEEPER: &str = "main/state_keeper"; pub const ROCKS_DB_TREE: &str = "main/tree"; diff --git a/zk_toolbox/crates/zk_supervisor/Cargo.toml b/zk_toolbox/crates/zk_supervisor/Cargo.toml index 74e04fc68aac..79d2bac74905 100644 --- a/zk_toolbox/crates/zk_supervisor/Cargo.toml +++ b/zk_toolbox/crates/zk_supervisor/Cargo.toml @@ -11,4 +11,13 @@ description.workspace = true keywords.workspace = true [dependencies] +anyhow.workspace = true +clap.workspace = true +common.workspace = true +config.workspace = true human-panic.workspace = true +strum.workspace = true +strum_macros.workspace = true +tokio.workspace = true +url.workspace = true +xshell.workspace = true diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs new file mode 100644 index 000000000000..e8171f2e11e8 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs @@ -0,0 +1,36 @@ +use clap::Parser; + +use crate::dals::SelectedDals; + +pub mod new_migration; + +#[derive(Debug, Parser)] +pub struct DatabaseCommonArgs { + /// Prover + #[clap(short, long, default_missing_value = "true", num_args = 0..=1)] + pub prover: Option, + /// Core + #[clap(short, long, default_missing_value = "true", num_args = 0..=1)] + pub core: Option, +} + +impl DatabaseCommonArgs { + pub fn fill_values_with_prompt(self, verb: &str) -> DatabaseCommonArgsFinal { + let prover = self.prover.unwrap_or_else(|| { + common::PromptConfirm::new(format!("Do you want to {verb} the prover database?")).ask() + }); + + let core = self.core.unwrap_or_else(|| { + common::PromptConfirm::new(format!("Do you want to {verb} the core database?")).ask() + }); + + DatabaseCommonArgsFinal { + selected_dals: SelectedDals { prover, core }, + } + } +} + +#[derive(Debug)] +pub struct DatabaseCommonArgsFinal { + pub selected_dals: SelectedDals, +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs new file mode 100644 index 000000000000..2364ed0b8cb7 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs @@ -0,0 +1,46 @@ +use clap::{Parser, ValueEnum}; +use common::{Prompt, PromptSelect}; +use strum::IntoEnumIterator; +use strum_macros::{Display, EnumIter}; + +#[derive(Debug, Parser)] +pub struct DatabaseNewMigrationArgs { + /// Database to create new migration for + #[clap(long)] + pub database: Option, + /// Migration name + #[clap(long)] + pub name: Option, +} + +impl DatabaseNewMigrationArgs { + pub fn fill_values_with_prompt(self) -> DatabaseNewMigrationArgsFinal { + let selected_database = self.database.unwrap_or_else(|| { + PromptSelect::new( + "What database do you want to create a new migration for?", + SelectedDatabase::iter(), + ) + .ask() + }); + let name = self + .name + .unwrap_or_else(|| Prompt::new("How do you want to name the migration?").ask()); + + DatabaseNewMigrationArgsFinal { + selected_database, + name, + } + } +} + +#[derive(Debug)] +pub struct DatabaseNewMigrationArgsFinal { + pub selected_database: SelectedDatabase, + pub name: String, +} + +#[derive(Debug, Clone, ValueEnum, EnumIter, PartialEq, Eq, Display)] +pub enum SelectedDatabase { + Prover, + Core, +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs new file mode 100644 index 000000000000..584098957774 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs @@ -0,0 +1,49 @@ +use std::path::Path; + +use common::{cmd::Cmd, logger, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use super::args::DatabaseCommonArgs; +use crate::dals::{get_dals, Dal}; + +pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("check sqlx data for"); + if args.selected_dals.none() { + logger::outro("No databases selected to check"); + return Ok(()); + } + + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + logger::info("Checking sqlx data"); + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + check_sqlx_data(shell, &ecosystem_config.link_to_code, dal)?; + } + + logger::outro("Databases sqlx data checked successfully"); + + Ok(()) +} + +pub fn check_sqlx_data( + shell: &Shell, + link_to_code: impl AsRef, + dal: Dal, +) -> anyhow::Result<()> { + let dir = link_to_code.as_ref().join(&dal.path); + let _dir_guard = shell.push_dir(dir); + let url = dal.url.as_str(); + + let spinner = Spinner::new(&format!("Checking sqlx data for dal {}...", dal.path)); + Cmd::new(cmd!( + shell, + "cargo sqlx prepare --check --database-url {url} -- --tests" + )) + .run()?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs new file mode 100644 index 000000000000..543a9d04eb0c --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs @@ -0,0 +1,36 @@ +use common::{ + db::{drop_db_if_exists, DatabaseConfig}, + logger, + spinner::Spinner, +}; +use xshell::Shell; + +use super::args::DatabaseCommonArgs; +use crate::dals::{get_dals, Dal}; + +pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("drop"); + if args.selected_dals.none() { + logger::outro("No databases selected to drop"); + return Ok(()); + } + + logger::info("Dropping databases"); + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + drop_database(dal).await?; + } + + logger::outro("Databases dropped successfully"); + + Ok(()) +} + +pub async fn drop_database(dal: Dal) -> anyhow::Result<()> { + let spinner = Spinner::new(&format!("Dropping DB for dal {}...", dal.path)); + let db = DatabaseConfig::from_url(dal.url)?; + drop_db_if_exists(&db).await?; + spinner.finish(); + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs new file mode 100644 index 000000000000..c26f2771eb2b --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs @@ -0,0 +1,45 @@ +use std::path::Path; + +use common::{cmd::Cmd, logger, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use super::args::DatabaseCommonArgs; +use crate::dals::{get_dals, Dal}; + +pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("migrate"); + if args.selected_dals.none() { + logger::outro("No databases selected to migrate"); + return Ok(()); + } + + logger::info("Migrating databases"); + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + migrate_database(shell, &ecosystem_config.link_to_code, dal)?; + } + + logger::outro("Databases migrated successfully"); + + Ok(()) +} + +fn migrate_database(shell: &Shell, link_to_code: impl AsRef, dal: Dal) -> anyhow::Result<()> { + let dir = link_to_code.as_ref().join(&dal.path); + let _dir_guard = shell.push_dir(dir); + let url = dal.url.as_str(); + + let spinner = Spinner::new(&format!("Migrating DB for dal {}...", dal.path)); + Cmd::new(cmd!( + shell, + "cargo sqlx database create --database-url {url}" + )) + .run()?; + Cmd::new(cmd!(shell, "cargo sqlx migrate run --database-url {url}")).run()?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs new file mode 100644 index 000000000000..eb090352c350 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs @@ -0,0 +1,43 @@ +use clap::Subcommand; +use xshell::Shell; + +use self::args::{new_migration::DatabaseNewMigrationArgs, DatabaseCommonArgs}; + +mod args; +mod check_sqlx_data; +mod drop; +mod migrate; +mod new_migration; +mod prepare; +mod reset; +mod setup; + +#[derive(Subcommand, Debug)] +pub enum DatabaseCommands { + /// Check sqlx-data.json is up to date + CheckSqlxData(DatabaseCommonArgs), + /// Drop databases + Drop(DatabaseCommonArgs), + /// Migrate databases + Migrate(DatabaseCommonArgs), + /// Create new migration + NewMigration(DatabaseNewMigrationArgs), + /// Prepare sqlx-data.json + Prepare(DatabaseCommonArgs), + /// Reset databases + Reset(DatabaseCommonArgs), + /// Setup databases + Setup(DatabaseCommonArgs), +} + +pub async fn run(shell: &Shell, args: DatabaseCommands) -> anyhow::Result<()> { + match args { + DatabaseCommands::CheckSqlxData(args) => check_sqlx_data::run(shell, args), + DatabaseCommands::Drop(args) => drop::run(shell, args).await, + DatabaseCommands::Migrate(args) => migrate::run(shell, args), + DatabaseCommands::NewMigration(args) => new_migration::run(shell, args), + DatabaseCommands::Prepare(args) => prepare::run(shell, args), + DatabaseCommands::Reset(args) => reset::run(shell, args).await, + DatabaseCommands::Setup(args) => setup::run(shell, args), + } +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs new file mode 100644 index 000000000000..5a8e42620154 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs @@ -0,0 +1,41 @@ +use std::path::Path; + +use common::{cmd::Cmd, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use super::args::new_migration::{DatabaseNewMigrationArgs, SelectedDatabase}; +use crate::dals::{get_core_dal, get_prover_dal, Dal}; + +pub fn run(shell: &Shell, args: DatabaseNewMigrationArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt(); + + let dal = match args.selected_database { + SelectedDatabase::Core => get_core_dal(shell)?, + SelectedDatabase::Prover => get_prover_dal(shell)?, + }; + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + generate_migration(shell, ecosystem_config.link_to_code, dal, args.name)?; + + Ok(()) +} + +fn generate_migration( + shell: &Shell, + link_to_code: impl AsRef, + dal: Dal, + name: String, +) -> anyhow::Result<()> { + let dir = link_to_code.as_ref().join(&dal.path); + let _dir_guard = shell.push_dir(dir); + + let spinner = Spinner::new(&format!( + "Creating new DB migration for dal {}...", + dal.path + )); + Cmd::new(cmd!(shell, "cargo sqlx migrate add -r {name}")).run()?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs new file mode 100644 index 000000000000..df38812a40af --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs @@ -0,0 +1,49 @@ +use std::path::Path; + +use common::{cmd::Cmd, logger, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use super::args::DatabaseCommonArgs; +use crate::dals::{get_dals, Dal}; + +pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("prepare"); + if args.selected_dals.none() { + logger::outro("No databases selected to prepare"); + return Ok(()); + } + + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + logger::info("Preparing sqlx data"); + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + prepare_sqlx_data(shell, &ecosystem_config.link_to_code, dal)?; + } + + logger::outro("Databases sqlx data prepared successfully"); + + Ok(()) +} + +pub fn prepare_sqlx_data( + shell: &Shell, + link_to_code: impl AsRef, + dal: Dal, +) -> anyhow::Result<()> { + let dir = link_to_code.as_ref().join(&dal.path); + let _dir_guard = shell.push_dir(dir); + let url = dal.url.as_str(); + + let spinner = Spinner::new(&format!("Preparing sqlx data for dal {}...", dal.path)); + Cmd::new(cmd!( + shell, + "cargo sqlx prepare --database-url {url} -- --tests" + )) + .run()?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs new file mode 100644 index 000000000000..00eab1a1b1c3 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs @@ -0,0 +1,38 @@ +use std::path::Path; + +use common::logger; +use config::EcosystemConfig; +use xshell::Shell; + +use super::{args::DatabaseCommonArgs, drop::drop_database, setup::setup_database}; +use crate::dals::{get_dals, Dal}; + +pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("reset"); + if args.selected_dals.none() { + logger::outro("No databases selected"); + return Ok(()); + } + + let ecoseystem_config = EcosystemConfig::from_file(shell)?; + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + logger::info(&format!("Resetting database {}", dal.path)); + reset_database(shell, ecoseystem_config.link_to_code.clone(), dal).await?; + } + + logger::outro("Databases resetted"); + + Ok(()) +} + +async fn reset_database( + shell: &Shell, + link_to_code: impl AsRef, + dal: Dal, +) -> anyhow::Result<()> { + drop_database(dal.clone()).await?; + setup_database(shell, link_to_code, dal)?; + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs new file mode 100644 index 000000000000..d645f0b16568 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs @@ -0,0 +1,50 @@ +use std::path::Path; + +use common::{cmd::Cmd, logger, spinner::Spinner}; +use config::EcosystemConfig; +use xshell::{cmd, Shell}; + +use super::args::DatabaseCommonArgs; +use crate::dals::{get_dals, Dal}; + +pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { + let args = args.fill_values_with_prompt("setup"); + if args.selected_dals.none() { + logger::outro("No databases selected to setup"); + return Ok(()); + } + + let ecosystem_config = EcosystemConfig::from_file(shell)?; + + logger::info("Setting up databases"); + + let dals = get_dals(shell, &args.selected_dals)?; + for dal in dals { + setup_database(shell, &ecosystem_config.link_to_code, dal)?; + } + + logger::outro("Databases set up successfully"); + + Ok(()) +} + +pub fn setup_database( + shell: &Shell, + link_to_code: impl AsRef, + dal: Dal, +) -> anyhow::Result<()> { + let dir = link_to_code.as_ref().join(&dal.path); + let _dir_guard = shell.push_dir(dir); + let url = dal.url.as_str(); + + let spinner = Spinner::new(&format!("Setting up DB for dal {}...", dal.path)); + Cmd::new(cmd!( + shell, + "cargo sqlx database create --database-url {url}" + )) + .run()?; + Cmd::new(cmd!(shell, "cargo sqlx migrate run --database-url {url}")).run()?; + spinner.finish(); + + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs new file mode 100644 index 000000000000..8fd0a6be869b --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs @@ -0,0 +1 @@ +pub mod database; diff --git a/zk_toolbox/crates/zk_supervisor/src/dals.rs b/zk_toolbox/crates/zk_supervisor/src/dals.rs new file mode 100644 index 000000000000..2b5ae7fcc6ff --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/dals.rs @@ -0,0 +1,68 @@ +use anyhow::anyhow; +use common::config::global_config; +use config::{EcosystemConfig, SecretsConfig}; +use url::Url; +use xshell::Shell; + +const CORE_DAL_PATH: &str = "core/lib/dal"; +const PROVER_DAL_PATH: &str = "prover/prover_dal"; + +#[derive(Debug, Clone)] +pub struct SelectedDals { + pub prover: bool, + pub core: bool, +} + +impl SelectedDals { + /// Returns true if no databases are selected + pub fn none(&self) -> bool { + !self.prover && !self.core + } +} + +#[derive(Debug, Clone)] +pub struct Dal { + pub path: String, + pub url: Url, +} + +pub fn get_dals(shell: &Shell, selected_dals: &SelectedDals) -> anyhow::Result> { + let mut dals = vec![]; + + if selected_dals.prover { + dals.push(get_prover_dal(shell)?); + } + if selected_dals.core { + dals.push(get_core_dal(shell)?); + } + + Ok(dals) +} + +pub fn get_prover_dal(shell: &Shell) -> anyhow::Result { + let secrets = get_secrets(shell)?; + + Ok(Dal { + path: PROVER_DAL_PATH.to_string(), + url: secrets.database.prover_url.clone(), + }) +} + +pub fn get_core_dal(shell: &Shell) -> anyhow::Result { + let secrets = get_secrets(shell)?; + + Ok(Dal { + path: CORE_DAL_PATH.to_string(), + url: secrets.database.server_url.clone(), + }) +} + +fn get_secrets(shell: &Shell) -> anyhow::Result { + let ecosystem_config = EcosystemConfig::from_file(shell)?; + let chain_config = ecosystem_config + .load_chain(global_config().chain_name.clone()) + .ok_or(anyhow!("Chain not found"))?; + let secrets = chain_config.get_secrets_config()?; + + Ok(secrets) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index 9936141be106..a46733cab7d3 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -1,4 +1,114 @@ -fn main() { +use clap::{Parser, Subcommand}; +use commands::database::DatabaseCommands; +use common::{ + check_prerequisites, + config::{global_config, init_global_config, GlobalConfig}, + init_prompt_theme, logger, +}; +use config::EcosystemConfig; +use xshell::Shell; + +mod commands; +mod dals; + +#[derive(Parser, Debug)] +#[command(version, about)] +struct Supervisor { + #[command(subcommand)] + command: SupervisorSubcommands, + #[clap(flatten)] + global: SupervisorGlobalArgs, +} + +#[derive(Subcommand, Debug)] +enum SupervisorSubcommands { + /// Database related commands + #[command(subcommand)] + Database(DatabaseCommands), +} + +#[derive(Parser, Debug)] +#[clap(next_help_heading = "Global options")] +struct SupervisorGlobalArgs { + /// Verbose mode + #[clap(short, long, global = true)] + verbose: bool, + /// Chain to use + #[clap(long, global = true)] + chain: Option, + /// Ignores prerequisites checks + #[clap(long, global = true)] + ignore_prerequisites: bool, +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { human_panic::setup_panic!(); - println!("Hello, world!"); + + init_prompt_theme(); + + logger::new_empty_line(); + logger::intro(); + + let shell = Shell::new().unwrap(); + let args = Supervisor::parse(); + + init_global_config_inner(&shell, &args.global)?; + + if !global_config().ignore_prerequisites { + check_prerequisites(&shell); + } + + match run_subcommand(args, &shell).await { + Ok(_) => {} + Err(e) => { + logger::error(e.to_string()); + + if e.chain().count() > 1 { + logger::error_note( + "Caused by:", + &e.chain() + .skip(1) + .enumerate() + .map(|(i, cause)| format!(" {i}: {}", cause)) + .collect::>() + .join("\n"), + ); + } + + logger::outro("Failed"); + std::process::exit(1); + } + } + + Ok(()) +} + +async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { + match args.command { + SupervisorSubcommands::Database(command) => commands::database::run(shell, command).await?, + } + Ok(()) +} + +fn init_global_config_inner(shell: &Shell, args: &SupervisorGlobalArgs) -> anyhow::Result<()> { + if let Some(name) = &args.chain { + if let Ok(config) = EcosystemConfig::from_file(shell) { + let chains = config.list_of_chains(); + if !chains.contains(name) { + anyhow::bail!( + "Chain with name {} doesnt exist, please choose one of {:?}", + name, + &chains + ); + } + } + } + + init_global_config(GlobalConfig { + verbose: args.verbose, + chain_name: args.chain.clone(), + ignore_prerequisites: args.ignore_prerequisites, + }); + Ok(()) } From 399c9875d090896bf25f4bd38e7359583114d724 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 10:18:09 -0300 Subject: [PATCH 2/7] fix: merge changes --- .../crates/zk_inception/src/commands/chain/args/genesis.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs index b3e6358fcb16..d835b1eb36a6 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/args/genesis.rs @@ -45,7 +45,7 @@ impl GenesisArgs { } else { let server_db_url = self.server_db_url.unwrap_or_else(|| { Prompt::new(&msg_server_db_url_prompt(&chain_name)) - .default(DATABASE_SERVER_URL) + .default(DATABASE_SERVER_URL.as_str()) .ask() }); let server_db_name = slugify(&self.server_db_name.unwrap_or_else(|| { @@ -55,7 +55,7 @@ impl GenesisArgs { })); let prover_db_url = self.prover_db_url.unwrap_or_else(|| { Prompt::new(&msg_prover_db_url_prompt(&chain_name)) - .default(DATABASE_PROVER_URL) + .default(DATABASE_PROVER_URL.as_str()) .ask() }); let prover_db_name = slugify(&self.prover_db_name.unwrap_or_else(|| { From c265c5b2ff6dc9bafc2748489be02a40f30f7431 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 10:18:18 -0300 Subject: [PATCH 3/7] fix: remove unused msg --- zk_toolbox/crates/zk_inception/src/messages.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/zk_toolbox/crates/zk_inception/src/messages.rs b/zk_toolbox/crates/zk_inception/src/messages.rs index 5745212a6270..799f1a5e2d7a 100644 --- a/zk_toolbox/crates/zk_inception/src/messages.rs +++ b/zk_toolbox/crates/zk_inception/src/messages.rs @@ -117,7 +117,6 @@ pub(super) const MSG_PROVER_DB_URL_HELP: &str = "Prover database url without dat pub(super) const MSG_PROVER_DB_NAME_HELP: &str = "Prover database name"; pub(super) const MSG_GENESIS_USE_DEFAULT_HELP: &str = "Use default database urls and names"; pub(super) const MSG_GENESIS_COMPLETED: &str = "Genesis completed successfully"; -pub(super) const MSG_GENESIS_DATABASE_CONFIG_ERR: &str = "Database config was not fully generated"; pub(super) const MSG_STARTING_GENESIS: &str = "Starting genesis process"; pub(super) const MSG_INITIALIZING_DATABASES_SPINNER: &str = "Initializing databases..."; pub(super) const MSG_STARTING_GENESIS_SPINNER: &str = From 6d817b9b1137a6c6c0b4b71503e747609d2ed043 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 10:18:33 -0300 Subject: [PATCH 4/7] chore: clippy --- zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs index 951e8d116963..fecda40c7760 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/init.rs @@ -97,7 +97,7 @@ pub async fn run(args: EcosystemInitArgs, shell: &Shell) -> anyhow::Result<()> { }; for chain_name in &list_of_chains { - logger::info(msg_initializing_chain(&chain_name)); + logger::info(msg_initializing_chain(chain_name)); let chain_config = ecosystem_config .load_chain(Some(chain_name.clone())) .context(MSG_CHAIN_NOT_INITIALIZED)?; From e57f0115c7cbdf22da99a5bb7049670a090ae3b1 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 10:45:27 -0300 Subject: [PATCH 5/7] feat: modify default behaviour --- .../src/commands/database/args/mod.rs | 22 +++++++++++-------- .../src/commands/database/check_sqlx_data.rs | 2 +- .../src/commands/database/drop.rs | 2 +- .../src/commands/database/migrate.rs | 2 +- .../src/commands/database/mod.rs | 12 +++++----- .../src/commands/database/prepare.rs | 2 +- .../src/commands/database/reset.rs | 2 +- .../src/commands/database/setup.rs | 2 +- 8 files changed, 25 insertions(+), 21 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs index e8171f2e11e8..419e24204c75 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs @@ -15,17 +15,21 @@ pub struct DatabaseCommonArgs { } impl DatabaseCommonArgs { - pub fn fill_values_with_prompt(self, verb: &str) -> DatabaseCommonArgsFinal { - let prover = self.prover.unwrap_or_else(|| { - common::PromptConfirm::new(format!("Do you want to {verb} the prover database?")).ask() - }); - - let core = self.core.unwrap_or_else(|| { - common::PromptConfirm::new(format!("Do you want to {verb} the core database?")).ask() - }); + pub fn parse(self) -> DatabaseCommonArgsFinal { + if self.prover.is_none() && self.core.is_none() { + return DatabaseCommonArgsFinal { + selected_dals: SelectedDals { + prover: true, + core: true, + }, + }; + } DatabaseCommonArgsFinal { - selected_dals: SelectedDals { prover, core }, + selected_dals: SelectedDals { + prover: self.prover.unwrap_or(false), + core: self.core.unwrap_or(false), + }, } } } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs index 584098957774..f19545d0ccdd 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs @@ -8,7 +8,7 @@ use super::args::DatabaseCommonArgs; use crate::dals::{get_dals, Dal}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("check sqlx data for"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected to check"); return Ok(()); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs index 543a9d04eb0c..19bf18d0b5a6 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs @@ -9,7 +9,7 @@ use super::args::DatabaseCommonArgs; use crate::dals::{get_dals, Dal}; pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("drop"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected to drop"); return Ok(()); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs index c26f2771eb2b..c4165007a492 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs @@ -8,7 +8,7 @@ use super::args::DatabaseCommonArgs; use crate::dals::{get_dals, Dal}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("migrate"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected to migrate"); return Ok(()); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs index eb090352c350..4c232e8c7c21 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs @@ -14,19 +14,19 @@ mod setup; #[derive(Subcommand, Debug)] pub enum DatabaseCommands { - /// Check sqlx-data.json is up to date + /// Check sqlx-data.json is up to date. If no databases are selected, all databases will be checked. CheckSqlxData(DatabaseCommonArgs), - /// Drop databases + /// Drop databases. If no databases are selected, all databases will be dropped. Drop(DatabaseCommonArgs), - /// Migrate databases + /// Migrate databases. If no databases are selected, all databases will be migrated. Migrate(DatabaseCommonArgs), /// Create new migration NewMigration(DatabaseNewMigrationArgs), - /// Prepare sqlx-data.json + /// Prepare sqlx-data.json. If no databases are selected, all databases will be prepared. Prepare(DatabaseCommonArgs), - /// Reset databases + /// Reset databases. If no databases are selected, all databases will be reset. Reset(DatabaseCommonArgs), - /// Setup databases + /// Setup databases. If no databases are selected, all databases will be setup. Setup(DatabaseCommonArgs), } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs index df38812a40af..6ee12e743d6e 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs @@ -8,7 +8,7 @@ use super::args::DatabaseCommonArgs; use crate::dals::{get_dals, Dal}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("prepare"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected to prepare"); return Ok(()); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs index 00eab1a1b1c3..da87fdebae95 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs @@ -8,7 +8,7 @@ use super::{args::DatabaseCommonArgs, drop::drop_database, setup::setup_database use crate::dals::{get_dals, Dal}; pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("reset"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected"); return Ok(()); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs index d645f0b16568..77cb6079e41a 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs @@ -8,7 +8,7 @@ use super::args::DatabaseCommonArgs; use crate::dals::{get_dals, Dal}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt("setup"); + let args = args.parse(); if args.selected_dals.none() { logger::outro("No databases selected to setup"); return Ok(()); From b1b98cf3da69a193ea7b8574dc62b104d48cdd31 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 12:06:55 -0300 Subject: [PATCH 6/7] feat: add messages module --- .../src/commands/database/args/mod.rs | 11 ++-- .../commands/database/args/new_migration.rs | 15 +++-- .../src/commands/database/check_sqlx_data.rs | 20 +++++-- .../src/commands/database/drop.rs | 16 ++++-- .../src/commands/database/migrate.rs | 19 +++++-- .../src/commands/database/mod.rs | 19 ++++--- .../src/commands/database/new_migration.rs | 14 +++-- .../src/commands/database/prepare.rs | 19 +++++-- .../src/commands/database/reset.rs | 16 ++++-- .../src/commands/database/setup.rs | 16 ++++-- zk_toolbox/crates/zk_supervisor/src/dals.rs | 4 +- zk_toolbox/crates/zk_supervisor/src/main.rs | 1 + .../crates/zk_supervisor/src/messages.rs | 56 +++++++++++++++++++ 13 files changed, 172 insertions(+), 54 deletions(-) create mode 100644 zk_toolbox/crates/zk_supervisor/src/messages.rs diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs index 419e24204c75..1541e7f518d8 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/mod.rs @@ -1,16 +1,17 @@ use clap::Parser; -use crate::dals::SelectedDals; +use crate::{ + dals::SelectedDals, + messages::{MSG_DATABASE_COMMON_CORE_HELP, MSG_DATABASE_COMMON_PROVER_HELP}, +}; pub mod new_migration; #[derive(Debug, Parser)] pub struct DatabaseCommonArgs { - /// Prover - #[clap(short, long, default_missing_value = "true", num_args = 0..=1)] + #[clap(short, long, default_missing_value = "true", num_args = 0..=1, help = MSG_DATABASE_COMMON_PROVER_HELP)] pub prover: Option, - /// Core - #[clap(short, long, default_missing_value = "true", num_args = 0..=1)] + #[clap(short, long, default_missing_value = "true", num_args = 0..=1, help = MSG_DATABASE_COMMON_CORE_HELP)] pub core: Option, } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs index 2364ed0b8cb7..ef053ca50c77 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/args/new_migration.rs @@ -3,13 +3,16 @@ use common::{Prompt, PromptSelect}; use strum::IntoEnumIterator; use strum_macros::{Display, EnumIter}; +use crate::messages::{ + MSG_DATABASE_NEW_MIGRATION_DATABASE_HELP, MSG_DATABASE_NEW_MIGRATION_DB_PROMPT, + MSG_DATABASE_NEW_MIGRATION_NAME_HELP, MSG_DATABASE_NEW_MIGRATION_NAME_PROMPT, +}; + #[derive(Debug, Parser)] pub struct DatabaseNewMigrationArgs { - /// Database to create new migration for - #[clap(long)] + #[clap(long, help = MSG_DATABASE_NEW_MIGRATION_DATABASE_HELP)] pub database: Option, - /// Migration name - #[clap(long)] + #[clap(long, help = MSG_DATABASE_NEW_MIGRATION_NAME_HELP)] pub name: Option, } @@ -17,14 +20,14 @@ impl DatabaseNewMigrationArgs { pub fn fill_values_with_prompt(self) -> DatabaseNewMigrationArgsFinal { let selected_database = self.database.unwrap_or_else(|| { PromptSelect::new( - "What database do you want to create a new migration for?", + MSG_DATABASE_NEW_MIGRATION_DB_PROMPT, SelectedDatabase::iter(), ) .ask() }); let name = self .name - .unwrap_or_else(|| Prompt::new("How do you want to name the migration?").ask()); + .unwrap_or_else(|| Prompt::new(MSG_DATABASE_NEW_MIGRATION_NAME_PROMPT).ask()); DatabaseNewMigrationArgsFinal { selected_database, diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs index f19545d0ccdd..6a5bc663dc7f 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/check_sqlx_data.rs @@ -5,25 +5,32 @@ use config::EcosystemConfig; use xshell::{cmd, Shell}; use super::args::DatabaseCommonArgs; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, + MSG_DATABASE_CHECK_SQLX_DATA_GERUND, MSG_DATABASE_CHECK_SQLX_DATA_PAST, + MSG_NO_DATABASES_SELECTED, + }, +}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected to check"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } let ecosystem_config = EcosystemConfig::from_file(shell)?; - logger::info("Checking sqlx data"); + logger::info(msg_database_info(MSG_DATABASE_CHECK_SQLX_DATA_GERUND)); let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { check_sqlx_data(shell, &ecosystem_config.link_to_code, dal)?; } - logger::outro("Databases sqlx data checked successfully"); + logger::outro(msg_database_success(MSG_DATABASE_CHECK_SQLX_DATA_PAST)); Ok(()) } @@ -37,7 +44,10 @@ pub fn check_sqlx_data( let _dir_guard = shell.push_dir(dir); let url = dal.url.as_str(); - let spinner = Spinner::new(&format!("Checking sqlx data for dal {}...", dal.path)); + let spinner = Spinner::new(&msg_database_loading( + MSG_DATABASE_CHECK_SQLX_DATA_GERUND, + &dal.path, + )); Cmd::new(cmd!( shell, "cargo sqlx prepare --check --database-url {url} -- --tests" diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs index 19bf18d0b5a6..fb6996b40ee3 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/drop.rs @@ -6,29 +6,35 @@ use common::{ use xshell::Shell; use super::args::DatabaseCommonArgs; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, MSG_DATABASE_DROP_GERUND, + MSG_DATABASE_DROP_PAST, MSG_NO_DATABASES_SELECTED, + }, +}; pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected to drop"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } - logger::info("Dropping databases"); + logger::info(msg_database_info(MSG_DATABASE_DROP_GERUND)); let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { drop_database(dal).await?; } - logger::outro("Databases dropped successfully"); + logger::outro(msg_database_success(MSG_DATABASE_DROP_PAST)); Ok(()) } pub async fn drop_database(dal: Dal) -> anyhow::Result<()> { - let spinner = Spinner::new(&format!("Dropping DB for dal {}...", dal.path)); + let spinner = Spinner::new(&msg_database_loading(MSG_DATABASE_DROP_GERUND, &dal.path)); let db = DatabaseConfig::from_url(dal.url)?; drop_db_if_exists(&db).await?; spinner.finish(); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs index c4165007a492..72bc7d59148e 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/migrate.rs @@ -5,16 +5,22 @@ use config::EcosystemConfig; use xshell::{cmd, Shell}; use super::args::DatabaseCommonArgs; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, MSG_DATABASE_MIGRATE_GERUND, + MSG_DATABASE_MIGRATE_PAST, MSG_NO_DATABASES_SELECTED, + }, +}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected to migrate"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } - logger::info("Migrating databases"); + logger::info(msg_database_info(MSG_DATABASE_MIGRATE_GERUND)); let ecosystem_config = EcosystemConfig::from_file(shell)?; let dals = get_dals(shell, &args.selected_dals)?; @@ -22,7 +28,7 @@ pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { migrate_database(shell, &ecosystem_config.link_to_code, dal)?; } - logger::outro("Databases migrated successfully"); + logger::outro(msg_database_success(MSG_DATABASE_MIGRATE_PAST)); Ok(()) } @@ -32,7 +38,10 @@ fn migrate_database(shell: &Shell, link_to_code: impl AsRef, dal: Dal) -> let _dir_guard = shell.push_dir(dir); let url = dal.url.as_str(); - let spinner = Spinner::new(&format!("Migrating DB for dal {}...", dal.path)); + let spinner = Spinner::new(&msg_database_loading( + MSG_DATABASE_MIGRATE_GERUND, + &dal.path, + )); Cmd::new(cmd!( shell, "cargo sqlx database create --database-url {url}" diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs index 4c232e8c7c21..74c4063a6974 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/mod.rs @@ -2,6 +2,11 @@ use clap::Subcommand; use xshell::Shell; use self::args::{new_migration::DatabaseNewMigrationArgs, DatabaseCommonArgs}; +use crate::messages::{ + MSG_DATABASE_CHECK_SQLX_DATA_ABOUT, MSG_DATABASE_DROP_ABOUT, MSG_DATABASE_MIGRATE_ABOUT, + MSG_DATABASE_NEW_MIGRATION_ABOUT, MSG_DATABASE_PREPARE_ABOUT, MSG_DATABASE_RESET_ABOUT, + MSG_DATABASE_SETUP_ABOUT, +}; mod args; mod check_sqlx_data; @@ -14,19 +19,19 @@ mod setup; #[derive(Subcommand, Debug)] pub enum DatabaseCommands { - /// Check sqlx-data.json is up to date. If no databases are selected, all databases will be checked. + #[clap(about = MSG_DATABASE_CHECK_SQLX_DATA_ABOUT)] CheckSqlxData(DatabaseCommonArgs), - /// Drop databases. If no databases are selected, all databases will be dropped. + #[clap(about = MSG_DATABASE_DROP_ABOUT)] Drop(DatabaseCommonArgs), - /// Migrate databases. If no databases are selected, all databases will be migrated. + #[clap(about = MSG_DATABASE_MIGRATE_ABOUT)] Migrate(DatabaseCommonArgs), - /// Create new migration + #[clap(about = MSG_DATABASE_NEW_MIGRATION_ABOUT)] NewMigration(DatabaseNewMigrationArgs), - /// Prepare sqlx-data.json. If no databases are selected, all databases will be prepared. + #[clap(about = MSG_DATABASE_PREPARE_ABOUT)] Prepare(DatabaseCommonArgs), - /// Reset databases. If no databases are selected, all databases will be reset. + #[clap(about = MSG_DATABASE_RESET_ABOUT)] Reset(DatabaseCommonArgs), - /// Setup databases. If no databases are selected, all databases will be setup. + #[clap(about = MSG_DATABASE_SETUP_ABOUT)] Setup(DatabaseCommonArgs), } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs index 5a8e42620154..127e01bdc10f 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/new_migration.rs @@ -1,11 +1,14 @@ use std::path::Path; -use common::{cmd::Cmd, spinner::Spinner}; +use common::{cmd::Cmd, logger, spinner::Spinner}; use config::EcosystemConfig; use xshell::{cmd, Shell}; use super::args::new_migration::{DatabaseNewMigrationArgs, SelectedDatabase}; -use crate::dals::{get_core_dal, get_prover_dal, Dal}; +use crate::{ + dals::{get_core_dal, get_prover_dal, Dal}, + messages::{msg_database_new_migration_loading, MSG_DATABASE_NEW_MIGRATION_SUCCESS}, +}; pub fn run(shell: &Shell, args: DatabaseNewMigrationArgs) -> anyhow::Result<()> { let args = args.fill_values_with_prompt(); @@ -18,6 +21,8 @@ pub fn run(shell: &Shell, args: DatabaseNewMigrationArgs) -> anyhow::Result<()> generate_migration(shell, ecosystem_config.link_to_code, dal, args.name)?; + logger::outro(MSG_DATABASE_NEW_MIGRATION_SUCCESS); + Ok(()) } @@ -30,10 +35,7 @@ fn generate_migration( let dir = link_to_code.as_ref().join(&dal.path); let _dir_guard = shell.push_dir(dir); - let spinner = Spinner::new(&format!( - "Creating new DB migration for dal {}...", - dal.path - )); + let spinner = Spinner::new(&msg_database_new_migration_loading(&dal.path)); Cmd::new(cmd!(shell, "cargo sqlx migrate add -r {name}")).run()?; spinner.finish(); diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs index 6ee12e743d6e..48f32319ac55 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/prepare.rs @@ -5,25 +5,31 @@ use config::EcosystemConfig; use xshell::{cmd, Shell}; use super::args::DatabaseCommonArgs; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, MSG_DATABASE_PREPARE_GERUND, + MSG_DATABASE_PREPARE_PAST, MSG_NO_DATABASES_SELECTED, + }, +}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected to prepare"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } let ecosystem_config = EcosystemConfig::from_file(shell)?; - logger::info("Preparing sqlx data"); + logger::info(msg_database_info(MSG_DATABASE_PREPARE_GERUND)); let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { prepare_sqlx_data(shell, &ecosystem_config.link_to_code, dal)?; } - logger::outro("Databases sqlx data prepared successfully"); + logger::outro(msg_database_success(MSG_DATABASE_PREPARE_PAST)); Ok(()) } @@ -37,7 +43,10 @@ pub fn prepare_sqlx_data( let _dir_guard = shell.push_dir(dir); let url = dal.url.as_str(); - let spinner = Spinner::new(&format!("Preparing sqlx data for dal {}...", dal.path)); + let spinner = Spinner::new(&msg_database_loading( + MSG_DATABASE_PREPARE_GERUND, + &dal.path, + )); Cmd::new(cmd!( shell, "cargo sqlx prepare --database-url {url} -- --tests" diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs index da87fdebae95..aa813a155510 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs @@ -5,24 +5,32 @@ use config::EcosystemConfig; use xshell::Shell; use super::{args::DatabaseCommonArgs, drop::drop_database, setup::setup_database}; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, MSG_DATABASE_RESET_GERUND, + MSG_DATABASE_RESET_PAST, MSG_NO_DATABASES_SELECTED, + }, +}; pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } let ecoseystem_config = EcosystemConfig::from_file(shell)?; + logger::info(msg_database_info(MSG_DATABASE_RESET_GERUND)); + let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { - logger::info(&format!("Resetting database {}", dal.path)); + logger::info(&msg_database_loading(MSG_DATABASE_RESET_GERUND, &dal.path)); reset_database(shell, ecoseystem_config.link_to_code.clone(), dal).await?; } - logger::outro("Databases resetted"); + logger::outro(msg_database_success(MSG_DATABASE_RESET_PAST)); Ok(()) } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs index 77cb6079e41a..d9d37041774b 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/setup.rs @@ -5,25 +5,31 @@ use config::EcosystemConfig; use xshell::{cmd, Shell}; use super::args::DatabaseCommonArgs; -use crate::dals::{get_dals, Dal}; +use crate::{ + dals::{get_dals, Dal}, + messages::{ + msg_database_info, msg_database_loading, msg_database_success, MSG_DATABASE_SETUP_GERUND, + MSG_DATABASE_SETUP_PAST, MSG_NO_DATABASES_SELECTED, + }, +}; pub fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> { let args = args.parse(); if args.selected_dals.none() { - logger::outro("No databases selected to setup"); + logger::outro(MSG_NO_DATABASES_SELECTED); return Ok(()); } let ecosystem_config = EcosystemConfig::from_file(shell)?; - logger::info("Setting up databases"); + logger::info(msg_database_info(MSG_DATABASE_SETUP_GERUND)); let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { setup_database(shell, &ecosystem_config.link_to_code, dal)?; } - logger::outro("Databases set up successfully"); + logger::outro(msg_database_success(MSG_DATABASE_SETUP_PAST)); Ok(()) } @@ -37,7 +43,7 @@ pub fn setup_database( let _dir_guard = shell.push_dir(dir); let url = dal.url.as_str(); - let spinner = Spinner::new(&format!("Setting up DB for dal {}...", dal.path)); + let spinner = Spinner::new(&msg_database_loading(MSG_DATABASE_SETUP_GERUND, &dal.path)); Cmd::new(cmd!( shell, "cargo sqlx database create --database-url {url}" diff --git a/zk_toolbox/crates/zk_supervisor/src/dals.rs b/zk_toolbox/crates/zk_supervisor/src/dals.rs index 2b5ae7fcc6ff..f2f6f86cfc61 100644 --- a/zk_toolbox/crates/zk_supervisor/src/dals.rs +++ b/zk_toolbox/crates/zk_supervisor/src/dals.rs @@ -4,6 +4,8 @@ use config::{EcosystemConfig, SecretsConfig}; use url::Url; use xshell::Shell; +use crate::messages::MSG_CHAIN_NOT_FOUND_ERR; + const CORE_DAL_PATH: &str = "core/lib/dal"; const PROVER_DAL_PATH: &str = "prover/prover_dal"; @@ -61,7 +63,7 @@ fn get_secrets(shell: &Shell) -> anyhow::Result { let ecosystem_config = EcosystemConfig::from_file(shell)?; let chain_config = ecosystem_config .load_chain(global_config().chain_name.clone()) - .ok_or(anyhow!("Chain not found"))?; + .ok_or(anyhow!(MSG_CHAIN_NOT_FOUND_ERR))?; let secrets = chain_config.get_secrets_config()?; Ok(secrets) diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index a46733cab7d3..12c76ad5b469 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -10,6 +10,7 @@ use xshell::Shell; mod commands; mod dals; +mod messages; #[derive(Parser, Debug)] #[command(version, about)] diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs new file mode 100644 index 000000000000..c593ec272174 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -0,0 +1,56 @@ +// Ecosystem related messages +pub(super) const MSG_CHAIN_NOT_FOUND_ERR: &str = "Chain not found"; + +// Database related messages +pub(super) const MSG_NO_DATABASES_SELECTED: &str = "No databases selected"; +pub(super) fn msg_database_info(gerund_verb: &str) -> String { + format!("{gerund_verb} databases") +} +pub(super) fn msg_database_success(past_verb: &str) -> String { + format!("Databases {past_verb} successfully") +} +pub(super) fn msg_database_loading(gerund_verb: &str, dal: &str) -> String { + format!("{gerund_verb} database for dal {dal}...") +} + +pub(super) const MSG_DATABASE_CHECK_SQLX_DATA_GERUND: &str = "Checking"; +pub(super) const MSG_DATABASE_CHECK_SQLX_DATA_PAST: &str = "checked"; +pub(super) const MSG_DATABASE_DROP_GERUND: &str = "Dropping"; +pub(super) const MSG_DATABASE_DROP_PAST: &str = "dropped"; +pub(super) const MSG_DATABASE_MIGRATE_GERUND: &str = "Migrating"; +pub(super) const MSG_DATABASE_MIGRATE_PAST: &str = "migrated"; +pub(super) const MSG_DATABASE_PREPARE_GERUND: &str = "Preparing"; +pub(super) const MSG_DATABASE_PREPARE_PAST: &str = "prepared"; +pub(super) const MSG_DATABASE_RESET_GERUND: &str = "Resetting"; +pub(super) const MSG_DATABASE_RESET_PAST: &str = "reset"; +pub(super) const MSG_DATABASE_SETUP_GERUND: &str = "Setting up"; +pub(super) const MSG_DATABASE_SETUP_PAST: &str = "set up"; + +pub(super) const MSG_DATABASE_COMMON_PROVER_HELP: &str = "Prover database"; +pub(super) const MSG_DATABASE_COMMON_CORE_HELP: &str = "Core database"; +pub(super) const MSG_DATABASE_NEW_MIGRATION_DATABASE_HELP: &str = + "Database to create new migration for"; +pub(super) const MSG_DATABASE_NEW_MIGRATION_NAME_HELP: &str = "Migration name"; + +pub(super) const MSG_DATABASE_CHECK_SQLX_DATA_ABOUT: &str = "Check sqlx-data.json is up to date. If no databases are selected, all databases will be checked."; +pub(super) const MSG_DATABASE_DROP_ABOUT: &str = + "Drop databases. If no databases are selected, all databases will be dropped."; +pub(super) const MSG_DATABASE_MIGRATE_ABOUT: &str = + "Migrate databases. If no databases are selected, all databases will be migrated."; +pub(super) const MSG_DATABASE_NEW_MIGRATION_ABOUT: &str = "Create new migration"; +pub(super) const MSG_DATABASE_PREPARE_ABOUT: &str = + "Prepare sqlx-data.json. If no databases are selected, all databases will be prepared."; +pub(super) const MSG_DATABASE_RESET_ABOUT: &str = + "Reset databases. If no databases are selected, all databases will be reset."; +pub(super) const MSG_DATABASE_SETUP_ABOUT: &str = + "Setup databases. If no databases are selected, all databases will be setup."; + +// Database new_migration messages +pub(super) const MSG_DATABASE_NEW_MIGRATION_DB_PROMPT: &str = + "What database do you want to create a new migration for?"; +pub(super) const MSG_DATABASE_NEW_MIGRATION_NAME_PROMPT: &str = + "How do you want to name the migration?"; +pub(super) fn msg_database_new_migration_loading(dal: &str) -> String { + format!("Creating new database migration for dal {}...", dal) +} +pub(super) const MSG_DATABASE_NEW_MIGRATION_SUCCESS: &str = "Migration created successfully"; From 8d73686ea65aeafd681cc125949f2cb587fcac07 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Fri, 31 May 2024 12:27:54 -0300 Subject: [PATCH 7/7] fix: add missing message --- zk_toolbox/crates/zk_supervisor/src/main.rs | 7 ++----- zk_toolbox/crates/zk_supervisor/src/messages.rs | 3 +++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index 12c76ad5b469..24daaba35347 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -6,6 +6,7 @@ use common::{ init_prompt_theme, logger, }; use config::EcosystemConfig; +use messages::msg_global_chain_does_not_exist; use xshell::Shell; mod commands; @@ -97,11 +98,7 @@ fn init_global_config_inner(shell: &Shell, args: &SupervisorGlobalArgs) -> anyho if let Ok(config) = EcosystemConfig::from_file(shell) { let chains = config.list_of_chains(); if !chains.contains(name) { - anyhow::bail!( - "Chain with name {} doesnt exist, please choose one of {:?}", - name, - &chains - ); + anyhow::bail!(msg_global_chain_does_not_exist(name, &chains.join(", "))); } } } diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index c593ec272174..97152396b5e5 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -1,5 +1,8 @@ // Ecosystem related messages pub(super) const MSG_CHAIN_NOT_FOUND_ERR: &str = "Chain not found"; +pub(super) fn msg_global_chain_does_not_exist(chain: &str, available_chains: &str) -> String { + format!("Chain with name {chain} doesnt exist, please choose one of: {available_chains}") +} // Database related messages pub(super) const MSG_NO_DATABASES_SELECTED: &str = "No databases selected";