Skip to content

Conversation

matthewmcneely
Copy link
Member

@matthewmcneely matthewmcneely commented Jun 4, 2025

Description

This PR adds string-based namespace support to the Txn class. Managing transactions manually is something common in many repos that use the dgo client. This new functionality makes upgrading transaction-heavy code (that wants to use string-based namespaces) less cumbersome.

Note that the v2 TestTxNamespaces will fail until Dgraph PR hypermodeinc/dgraph#9433 is merged.

Checklist

  • Code compiles correctly and linting passes locally
  • For all code changes, an entry added to the CHANGELOG.md file describing and linking to
    this PR
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

Add tests; update test docker compose file
@Copilot Copilot AI review requested due to automatic review settings June 4, 2025 01:25
@matthewmcneely matthewmcneely requested a review from a team as a code owner June 4, 2025 01:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds string‐based namespace support to transactions in the Txn class to simplify manual transaction management with the dgo client.

  • Introduces NewTxnInNamespace and NewReadOnlyTxnInNamespace methods in txn.go.
  • Adds comprehensive tests in v2_test.go and examples_test.go to verify namespace behavior.
  • Updates docker-compose.yml to include non‐ACL service configurations for testing.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
v2_test.go Adds tests for namespace creation, duplicate namespace error handling, and Txn usage.
txn.go Introduces new namespace field and helper methods for namespace-aware transactions.
t/docker-compose.yml Provides new docker services for a non‑ACL environment to support testing.
examples_test.go Adds functions to obtain dgo clients in non‑ACL mode and tests namespace support.
Comments suppressed due to low confidence (1)

txn.go:67

  • Correct the abbreviation 'Tnx' to 'Txn' for clarity in the comment.
// in the default namespace. Note that a Tnx created with this function will not complete successfully as the

matthewmcneely and others added 2 commits June 4, 2025 13:11
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@matthewmcneely matthewmcneely force-pushed the matthewmcneely/support-string-namespaces-in-txns branch from c7159b5 to 3f812cf Compare June 17, 2025 21:12
@RJKeevil
Copy link

RJKeevil commented Jun 25, 2025

I think this would be a great improvement.

With the V2 apis, namespaces are simple and a separate concept from ACL/auth, which is fantastic and exactly what I need. We have some graphs that update continuously, and others that are a monthly dump to be dropped and recreated in full. Namespaces gives me an easy way to acheive this data separation. I do not need different auth rules and user separation across the namespaces, as they are purely used by a single backend service.

The missing parts are to enable namespace selection everywhere, without requiring different logins. So dgo transactions, graphQL admin requests (upload schema etc), graphQL queries and the Ratel/play UI. Happy to add additional info if it helps!

See https://discuss.hypermode.com/t/v25-multitenency/19915/3

@RJKeevil
Copy link

Now that I understand that apiv2 currently deprecates json-based mutatations this change is very important, as I can fall back to legacy apis for these, provided I have a way to set namespaces, which this PR provides.

txn.go Outdated
// in the default namespace. Note that a Tnx created with this function
// against an ACL-enabled Dgraph cluster will not complete successfully as the
// namespace from the JWT is used to calculate the namespace ID.
func (d *Dgraph) NewTxnInNamespace(namespace string) *Txn {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how we should structure the API now:

  1. NewTxn(): existing function, should create a txn in the root namespace
  2. NewReadOnlyTxn(): existing function, should create a read only txn in root namespace
  3. NewTxnInNamespace(): txn in a given namespace
  4. NewReadOnlyTxnInNamespace(): read only txn in a given namespace

OR

  1. NewTxn(TxnOptions)
  2. NewReadOnlyTxn(): will deprecate this later

here, TxnOptions could be BestEffort, ReadOnly and provide namespace as well. We should update existing v2 APIs to take namespace as a TxnOption as well.

Copy link
Member Author

@matthewmcneely matthewmcneely Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. The second way is cleaner. I added a WithNamespace to TxnOptions. So it looks like:

txn := client.NewTxn(dgo.WithNamespace("foo"), dgo.WithReadOnly())

I wonder now if the namespace arg in the v2 funcs is needed

Currently...

resp, err = client.RunDQL(ctx, dgo.RootNamespace, queryDQL, dgo.WithBestEffort())

Could be changed to:

resp, err = client.RunDQL(ctx, queryDQL, dgo.WithNamespace(dgo.RootNamespace), dgo.WithBestEffort())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aman-bansal I can make the changes to all the v2 APIs in this PR (removing the namespace attribute, replacing with the WithNamespace TxnOption). Just let me know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewmcneely Yes please go ahead with it.

@matthewmcneely matthewmcneely force-pushed the matthewmcneely/support-string-namespaces-in-txns branch from dff703b to 6f13f7f Compare August 6, 2025 20:30
@matthewmcneely matthewmcneely force-pushed the matthewmcneely/support-string-namespaces-in-txns branch 2 times, most recently from 502133e to db0cbbd Compare August 8, 2025 17:39
@matthewmcneely matthewmcneely force-pushed the matthewmcneely/support-string-namespaces-in-txns branch from db0cbbd to 2bc5582 Compare August 12, 2025 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants