From b10676deb412aaaf7616184d93ea7606440b05f3 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Mon, 7 Nov 2022 09:00:51 +0100 Subject: [PATCH 1/5] Implement new flag for `Remove` method for graphs. --- context.go | 18 ++++++++++++++++++ graph_impl.go | 1 + 2 files changed, 19 insertions(+) diff --git a/context.go b/context.go index 8789828e..02518349 100644 --- a/context.go +++ b/context.go @@ -66,6 +66,7 @@ const ( keyOverwrite ContextKey = "arangodb-overwrite" keyUseQueueTimeout ContextKey = "arangodb-use-queue-timeout" keyMaxQueueTime ContextKey = "arangodb-max-queue-time-seconds" + keyDropCollections ContextKey = "arangodb-drop-collections" ) type OverwriteMode string @@ -274,6 +275,16 @@ func WithOverwrite(parent context.Context) context.Context { return context.WithValue(contextOrBackground(parent), keyOverwrite, true) } +// WithDropCollections is used to configure a context to make graph removal functions to also drop the collections of the graph instead only the graph definition. +// You can pass a single (optional) boolean. If that is set to true, you explicitly ask to also drop the collections of the graph. +func WithDropCollections(parent context.Context, value ...bool) context.Context { + v := true + if len(value) == 1 { + v = value[0] + } + return context.WithValue(contextOrBackground(parent), keyDropCollections, v) +} + type contextSettings struct { Silent bool WaitForSync bool @@ -297,6 +308,7 @@ type contextSettings struct { Overwrite bool QueueTimeout bool MaxQueueTime time.Duration + DropCollections *bool } // loadContextResponseValue loads generic values from the response and puts it into variables specified @@ -489,6 +501,12 @@ func applyContextSettings(ctx context.Context, req Request) contextSettings { result.OverwriteMode = mode } } + // DropCollections + if v := ctx.Value(keyDropCollections); v != nil { + if dropCollections, ok := v.(bool); ok { + result.DropCollections = &dropCollections + } + } if v := ctx.Value(keyOverwrite); v != nil { if overwrite, ok := v.(bool); ok && overwrite { diff --git a/graph_impl.go b/graph_impl.go index 2f1ec815..ad8176e2 100644 --- a/graph_impl.go +++ b/graph_impl.go @@ -128,6 +128,7 @@ func (g *graph) Remove(ctx context.Context) error { if err != nil { return WithStack(err) } + applyContextSettings(ctx, req) resp, err := g.conn.Do(ctx, req) if err != nil { return WithStack(err) From 61319e56d21962e9c96a7b4545056a4a0bfe3d05 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Mon, 7 Nov 2022 09:38:12 +0100 Subject: [PATCH 2/5] Actually send query parameter. --- context.go | 1 + 1 file changed, 1 insertion(+) diff --git a/context.go b/context.go index 02518349..3eb2e2da 100644 --- a/context.go +++ b/context.go @@ -504,6 +504,7 @@ func applyContextSettings(ctx context.Context, req Request) contextSettings { // DropCollections if v := ctx.Value(keyDropCollections); v != nil { if dropCollections, ok := v.(bool); ok { + req.SetQuery("dropCollections", string(dropCollections)) result.DropCollections = &dropCollections } } From 1880acfed49d90e23d74fef509772b72741e623a Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Mon, 7 Nov 2022 09:40:35 +0100 Subject: [PATCH 3/5] Get bool conversion right. --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 3eb2e2da..ba516f27 100644 --- a/context.go +++ b/context.go @@ -504,7 +504,7 @@ func applyContextSettings(ctx context.Context, req Request) contextSettings { // DropCollections if v := ctx.Value(keyDropCollections); v != nil { if dropCollections, ok := v.(bool); ok { - req.SetQuery("dropCollections", string(dropCollections)) + req.SetQuery("dropCollections", strconv.FormatBool(dropCollections)) result.DropCollections = &dropCollections } } From 45cb59f9d63e1010c918556933943f472923bec0 Mon Sep 17 00:00:00 2001 From: Lars Maier Date: Fri, 28 Apr 2023 14:36:04 +0200 Subject: [PATCH 4/5] Add replication version option for database creation. --- client_databases.go | 9 +++++++++ database.go | 2 ++ test/database_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/client_databases.go b/client_databases.go index 715d5ddf..40d23b91 100644 --- a/client_databases.go +++ b/client_databases.go @@ -62,6 +62,13 @@ type CreateDatabaseOptions struct { Options CreateDatabaseDefaultOptions `json:"options,omitempty"` } +type DatabaseReplicationVersion string + +const ( + DatabaseReplicationVersionOne DatabaseReplicationVersion = "1" + DatabaseReplicationVersionTwo DatabaseReplicationVersion = "2" +) + // CreateDatabaseDefaultOptions contains options that change defaults for collections type CreateDatabaseDefaultOptions struct { // Default replication factor for collections in database @@ -70,6 +77,8 @@ type CreateDatabaseDefaultOptions struct { WriteConcern int `json:"writeConcern,omitempty"` // Default sharding for collections in database Sharding DatabaseSharding `json:"sharding,omitempty"` + // Replication version to use for this database + ReplicationVersion DatabaseReplicationVersion `json:"replicationVersion,omitempty"` } // CreateDatabaseUserOptions contains options for creating a single user for a database. diff --git a/database.go b/database.go index 024c7d5d..ab4ff0f1 100644 --- a/database.go +++ b/database.go @@ -94,6 +94,8 @@ type DatabaseInfo struct { WriteConcern int `json:"writeConcern,omitempty"` // Default sharding for collections in database Sharding DatabaseSharding `json:"sharding,omitempty"` + // Replication version used for this database + ReplicationVersion DatabaseReplicationVersion `json:"replicationVersion,omitempty"` } // EngineType indicates type of database engine being used. diff --git a/test/database_test.go b/test/database_test.go index ad3a109f..e2a8cfb0 100644 --- a/test/database_test.go +++ b/test/database_test.go @@ -204,6 +204,44 @@ func TestDatabaseNameUnicode(t *testing.T) { require.NoErrorf(t, db.Remove(ctx), "failed to remove testing database") } +// TestCreateDatabaseReplication2 creates a database with replication version two. +func TestCreateDatabaseReplication2(t *testing.T) { + ctx := context.Background() + c := createClientFromEnv(t, true) + version, _ := c.Version(ctx) + if version.Version.CompareTo("3.11.0") < 0 { + t.Skipf("Version of the ArangoDB should be at least 3.11.0") + } + + name := "create_test1" + opts := driver.CreateDatabaseOptions{Options: driver.CreateDatabaseDefaultOptions{ + ReplicationVersion: driver.DatabaseReplicationVersionTwo, + }} + if _, err := c.CreateDatabase(nil, name, &opts); err != nil { + t.Fatalf("Failed to create database '%s': %s", name, describe(err)) + } + // Database must exist now + if found, err := c.DatabaseExists(nil, name); err != nil { + t.Errorf("DatabaseExists('%s') failed: %s", name, describe(err)) + } else if !found { + t.Errorf("DatabaseExists('%s') return false, expected true", name) + } + + // Read database properties + db, err := c.Database(nil, name) + if err != nil { + t.Fatal("Failed to get database ") + } + info, err := db.Info(nil) + if err != nil { + t.Fatal("Failed to get database name") + } + + if info.ReplicationVersion != driver.DatabaseReplicationVersionTwo { + t.Errorf("Wrong replication version, expected %s, found %s", driver.DatabaseReplicationVersionTwo, info.ReplicationVersion) + } +} + // databaseExtendedNamesRequired skips test if the version is < 3.9.0 or the ArangoDB has not been launched // with the option --database.extended-names-databases=true. func databaseExtendedNamesRequired(t *testing.T, c driver.Client) { From 0e3554a452da56113c0b425115ef7c3152e4c6d4 Mon Sep 17 00:00:00 2001 From: Lars Maier Date: Fri, 28 Apr 2023 15:13:51 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Nikita Vaniasin --- client_databases.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client_databases.go b/client_databases.go index 40d23b91..22e429c7 100644 --- a/client_databases.go +++ b/client_databases.go @@ -62,6 +62,9 @@ type CreateDatabaseOptions struct { Options CreateDatabaseDefaultOptions `json:"options,omitempty"` } +// DatabaseReplicationVersion defines replication protocol version to use for this database +// Available since ArangoDB version 3.11 +// Note: this feature is still considered experimental and should not be used in production type DatabaseReplicationVersion string const ( @@ -78,6 +81,7 @@ type CreateDatabaseDefaultOptions struct { // Default sharding for collections in database Sharding DatabaseSharding `json:"sharding,omitempty"` // Replication version to use for this database + // Available since ArangoDB version 3.11 ReplicationVersion DatabaseReplicationVersion `json:"replicationVersion,omitempty"` }