Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Add command for DB migration #34

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ pub enum Command {
target: GenerateCommand,
},

/// Migrate DB.
Migrate {
#[arg(
long,
long_help = "Database URL",
env = "GEVULOT_DB_URL",
default_value = "postgres://gevulot:gevulot@localhost/gevulot"
)]
db_url: String,
},

/// Peer related commands.
Peer {
peer: String,
Expand Down
10 changes: 10 additions & 0 deletions crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use gevulot_node::types;
use libsecp256k1::SecretKey;
use pea2pea::Pea2Pea;
use rand::{rngs::StdRng, SeedableRng};
use sqlx::postgres::PgPoolOptions;
use tokio::sync::{Mutex as TMutex, RwLock};
use tonic::transport::Server;
use tracing_subscriber::{filter::LevelFilter, fmt::format::FmtSpan, EnvFilter};
Expand Down Expand Up @@ -65,6 +66,15 @@ async fn main() -> Result<()> {
Command::Generate { target } => match target {
GenerateCommand::NodeKey { options } => generate_node_key(options),
},
Command::Migrate { db_url } => {
let pool = PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_millis(500))
.connect(&db_url)
.await?;
// This will pick them up from `./migrations`.
sqlx::migrate!().run(&pool).await.map_err(|e| e.into())
}
Command::Peer { peer, op } => match op {
PeerCommand::Whitelist { whitelist } => {
todo!("implement peer whitelisting");
Expand Down