neo4j-labs / neo4rs

Neo4j driver for rust

Home Page:https://docs.rs/neo4rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting the database via `config_builder.db("graph")` using the ConfigBuilder seems to be ignored

avrabe opened this issue · comments

I'm working on some code in https://github.com/avrabe/graph-git-rs
For this I've locally created a project with an additional database graph.

Independent if I mention the database name in the url() or the db() of the ConfigBuilder, at the end always the neo4j database is used. Do I miss something in my code?

    let config = ConfigBuilder::new()
        .uri(opts.uri)
        .user(opts.user)
        .password(opts.password)
        .db(opts.db)
        .build()
        .unwrap();
    info!("{:?}", config);
    let graph = Arc::new(Graph::connect(config).await.unwrap());
    //Transactions
    let txn = graph.start_txn().await.unwrap();
    txn.run_queries(collector).await.unwrap();
    txn.commit().await.unwrap();
2023-10-01T12:33:37.796902Z  INFO graph_git_cli: graph-git-cli/src/main.rs: Config { uri: "bolt://127.0.0.1:7687", user: "neo4j", password: "12345678", max_connections: 16, db: "graph", fetch_size: 200 }

Just hit this as well, you can hotfix this by vendoring in the repo and updating messages.rs from ...

pub fn begin() -> BoltRequest {
	BoltRequest::Begin(Begin::new(BoltMap::default()))
}

... to ...

pub fn begin(db: &str) -> BoltRequest {
	let mut extra = BoltMap::default();
	extra.put("db".into(), db.to_owned().into());

	BoltRequest::Begin(Begin::new(extra))
}

... and txn.rs from ...

pub(crate) async fn new(config: Config, mut connection: ManagedConnection) -> Result<Self> {
	let begin = BoltRequest::begin();
	match connection.send_recv(begin).await? {
		BoltResponse::Success(_) => Ok(Txn {
			config,
			connection: Arc::new(Mutex::new(connection)),
		}),
		msg => Err(unexpected(msg, "BEGIN")),
	}
}

... to ...

pub(crate) async fn new(config: Config, mut connection: ManagedConnection) -> Result<Self> {
	let begin = BoltRequest::begin(&config.db);
	match connection.send_recv(begin).await? {
		BoltResponse::Success(_) => Ok(Txn {
			config,
			connection: Arc::new(Mutex::new(connection)),
		}),
		msg => Err(unexpected(msg, "BEGIN")),
	}
}

Thanks for reporting this. #117 fixes this issue, so that start_txn will use the configured database.
We also add the functions start_tx_on, execute_on, and run_on that accept a db: &str parameter which takes precedence over the configured database, so you can run queries on whatever database you need without having to configure and use separate clients (see https://github.com/neo4j-labs/neo4rs/blob/0784bd863abfc03c83b50094704be04f67959653/lib/tests/txn_change_db.rs for an example usage).