Skip to content

Commit

Permalink
Added SELECTNS admin command to switch to databases in other namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ederuiter committed Nov 6, 2021
1 parent ace526f commit d0e5165
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,12 @@ void selectCommand(client *c) {
}
}

void selectNsCommand(client *c) {
//TODO: validate namespace name
c->ns = getNamespace(szFromObj(c->argv[2]));
selectCommand(c);
}

void allocateCommand(client *c) {
int global_db, ns_db;

Expand Down
4 changes: 4 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ struct redisCommand redisCommandTable[] = {
{"allocate",allocateCommand,4,
"ok-loading fast ok-stale @keyspace",
0,NULL,0,0,0,0,0,0},

{"selectns",selectNsCommand,3,
"admin ok-loading fast ok-stale @keyspace",
0,NULL,0,0,0,0,0,0},
};

/*============================ Utility functions ============================ */
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,7 @@ void incrbyCommand(client *c);
void decrbyCommand(client *c);
void incrbyfloatCommand(client *c);
void selectCommand(client *c);
void selectNsCommand(client *c);
void allocateCommand(client *c);
void swapdbCommand(client *c);
void randomkeyCommand(client *c);
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/namespace.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ start_server [list overrides [list "dir" $server_path "aclfile" "user-namespaces
assert_equal "bob2" [r GET name]
}


test {admin can select databases in different namespaces} {
r AUTH default ""
r SELECTNS 0 ::ns1
assert_equal "alice0" [r GET name]
r SELECTNS 1 ::ns1
assert_equal "alice1" [r GET name]
r SELECTNS 0 ::ns2
assert_equal "bob0" [r GET name]
r SELECTNS 1 ::ns2
assert_equal "bob1" [r GET name]
}
}

0 comments on commit d0e5165

Please sign in to comment.