Skip to content

Commit 46a19ec

Browse files
committed
ADDED DROP/ALTER USER/GROUP
1 parent 1ee536e commit 46a19ec

File tree

5 files changed

+583
-21
lines changed

5 files changed

+583
-21
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package ydb_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-cmp/cmp/cmpopts"
9+
"github.com/sqlc-dev/sqlc/internal/engine/ydb"
10+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
11+
)
12+
13+
func TestAlterGroup(t *testing.T) {
14+
tests := []struct {
15+
stmt string
16+
expected ast.Node
17+
}{
18+
{
19+
stmt: `ALTER GROUP admins RENAME TO superusers`,
20+
expected: &ast.Statement{
21+
Raw: &ast.RawStmt{
22+
Stmt: &ast.AlterRoleStmt{
23+
Role: &ast.RoleSpec{
24+
Rolename: strPtr("admins"),
25+
Roletype: ast.RoleSpecType(1),
26+
},
27+
Action: 1,
28+
Options: &ast.List{
29+
Items: []ast.Node{
30+
&ast.DefElem{
31+
Defname: strPtr("rename"),
32+
Defaction: ast.DefElemAction(1),
33+
Arg: &ast.String{Str: "superusers"},
34+
},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
},
41+
{
42+
stmt: `ALTER GROUP devs ADD USER alice, bob, carol`,
43+
expected: &ast.Statement{
44+
Raw: &ast.RawStmt{
45+
Stmt: &ast.AlterRoleStmt{
46+
Role: &ast.RoleSpec{
47+
Rolename: strPtr("devs"),
48+
Roletype: ast.RoleSpecType(1),
49+
},
50+
Action: 1,
51+
Options: &ast.List{
52+
Items: []ast.Node{
53+
&ast.DefElem{
54+
Defname: strPtr("rolemembers"),
55+
Defaction: ast.DefElemAction(3),
56+
Arg: &ast.List{
57+
Items: []ast.Node{
58+
&ast.RoleSpec{Rolename: strPtr("alice"), Roletype: ast.RoleSpecType(1)},
59+
&ast.RoleSpec{Rolename: strPtr("bob"), Roletype: ast.RoleSpecType(1)},
60+
&ast.RoleSpec{Rolename: strPtr("carol"), Roletype: ast.RoleSpecType(1)},
61+
},
62+
},
63+
},
64+
},
65+
},
66+
},
67+
},
68+
},
69+
},
70+
{
71+
stmt: `ALTER GROUP ops DROP USER dan, erin`,
72+
expected: &ast.Statement{
73+
Raw: &ast.RawStmt{
74+
Stmt: &ast.AlterRoleStmt{
75+
Role: &ast.RoleSpec{
76+
Rolename: strPtr("ops"),
77+
Roletype: ast.RoleSpecType(1),
78+
},
79+
Action: 1,
80+
Options: &ast.List{
81+
Items: []ast.Node{
82+
&ast.DefElem{
83+
Defname: strPtr("rolemembers"),
84+
Defaction: ast.DefElemAction(4),
85+
Arg: &ast.List{
86+
Items: []ast.Node{
87+
&ast.RoleSpec{Rolename: strPtr("dan"), Roletype: ast.RoleSpecType(1)},
88+
&ast.RoleSpec{Rolename: strPtr("erin"), Roletype: ast.RoleSpecType(1)},
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
95+
},
96+
},
97+
},
98+
}
99+
100+
p := ydb.NewParser()
101+
for _, tc := range tests {
102+
t.Run(tc.stmt, func(t *testing.T) {
103+
stmts, err := p.Parse(strings.NewReader(tc.stmt))
104+
if err != nil {
105+
t.Fatalf("Ошибка парсинга запроса %q: %v", tc.stmt, err)
106+
}
107+
if len(stmts) == 0 {
108+
t.Fatalf("Запрос %q не распарсен", tc.stmt)
109+
}
110+
111+
diff := cmp.Diff(tc.expected, &stmts[0],
112+
cmpopts.IgnoreFields(ast.RawStmt{}, "StmtLocation", "StmtLen"),
113+
cmpopts.IgnoreFields(ast.DefElem{}, "Location"),
114+
cmpopts.IgnoreFields(ast.RoleSpec{}, "Location"),
115+
cmpopts.IgnoreFields(ast.A_Const{}, "Location"),
116+
)
117+
if diff != "" {
118+
t.Errorf("Несовпадение AST (-ожидалось +получено):\n%s", diff)
119+
}
120+
})
121+
}
122+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package ydb_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-cmp/cmp/cmpopts"
9+
"github.com/sqlc-dev/sqlc/internal/engine/ydb"
10+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
11+
)
12+
13+
func TestAlterUser(t *testing.T) {
14+
tests := []struct {
15+
stmt string
16+
expected ast.Node
17+
}{
18+
{
19+
stmt: `ALTER USER alice RENAME TO queen`,
20+
expected: &ast.Statement{
21+
Raw: &ast.RawStmt{
22+
Stmt: &ast.AlterRoleStmt{
23+
Role: &ast.RoleSpec{
24+
Rolename: strPtr("alice"),
25+
Roletype: ast.RoleSpecType(1),
26+
},
27+
Action: 1,
28+
Options: &ast.List{
29+
Items: []ast.Node{
30+
&ast.DefElem{
31+
Defname: strPtr("rename"),
32+
Arg: &ast.String{Str: "queen"},
33+
Defaction: ast.DefElemAction(1),
34+
},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
},
41+
{
42+
stmt: `ALTER USER bob LOGIN`,
43+
expected: &ast.Statement{
44+
Raw: &ast.RawStmt{
45+
Stmt: &ast.AlterRoleStmt{
46+
Role: &ast.RoleSpec{
47+
Rolename: strPtr("bob"),
48+
Roletype: ast.RoleSpecType(1),
49+
},
50+
Action: 1,
51+
Options: &ast.List{
52+
Items: []ast.Node{
53+
&ast.DefElem{
54+
Defname: strPtr("login"),
55+
Arg: &ast.Boolean{Boolval: true},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
},
62+
},
63+
{
64+
stmt: `ALTER USER charlie NOLOGIN`,
65+
expected: &ast.Statement{
66+
Raw: &ast.RawStmt{
67+
Stmt: &ast.AlterRoleStmt{
68+
Role: &ast.RoleSpec{
69+
Rolename: strPtr("charlie"),
70+
Roletype: ast.RoleSpecType(1),
71+
},
72+
Action: 1,
73+
Options: &ast.List{
74+
Items: []ast.Node{
75+
&ast.DefElem{
76+
Defname: strPtr("nologin"),
77+
Arg: &ast.Boolean{Boolval: false},
78+
},
79+
},
80+
},
81+
},
82+
},
83+
},
84+
},
85+
{
86+
stmt: `ALTER USER dave PASSWORD 'qwerty'`,
87+
expected: &ast.Statement{
88+
Raw: &ast.RawStmt{
89+
Stmt: &ast.AlterRoleStmt{
90+
Role: &ast.RoleSpec{
91+
Rolename: strPtr("dave"),
92+
Roletype: ast.RoleSpecType(1),
93+
},
94+
Action: 1,
95+
Options: &ast.List{
96+
Items: []ast.Node{
97+
&ast.DefElem{
98+
Defname: strPtr("password"),
99+
Arg: &ast.String{Str: "qwerty"},
100+
},
101+
},
102+
},
103+
},
104+
},
105+
},
106+
},
107+
{
108+
stmt: `ALTER USER elena HASH 'abc123'`,
109+
expected: &ast.Statement{
110+
Raw: &ast.RawStmt{
111+
Stmt: &ast.AlterRoleStmt{
112+
Role: &ast.RoleSpec{
113+
Rolename: strPtr("elena"),
114+
Roletype: ast.RoleSpecType(1),
115+
},
116+
Action: 1,
117+
Options: &ast.List{
118+
Items: []ast.Node{
119+
&ast.DefElem{
120+
Defname: strPtr("hash"),
121+
Arg: &ast.String{Str: "abc123"},
122+
},
123+
},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
}
130+
131+
p := ydb.NewParser()
132+
for _, tc := range tests {
133+
t.Run(tc.stmt, func(t *testing.T) {
134+
stmts, err := p.Parse(strings.NewReader(tc.stmt))
135+
if err != nil {
136+
t.Fatalf("Ошибка парсинга запроса %q: %v", tc.stmt, err)
137+
}
138+
if len(stmts) == 0 {
139+
t.Fatalf("Запрос %q не распарсен", tc.stmt)
140+
}
141+
142+
diff := cmp.Diff(tc.expected, &stmts[0],
143+
cmpopts.IgnoreFields(ast.RawStmt{}, "StmtLocation", "StmtLen"),
144+
cmpopts.IgnoreFields(ast.DefElem{}, "Location"),
145+
cmpopts.IgnoreFields(ast.RoleSpec{}, "Location"),
146+
cmpopts.IgnoreFields(ast.A_Const{}, "Location"),
147+
)
148+
if diff != "" {
149+
t.Errorf("Несовпадение AST (-ожидалось +получено):\n%s", diff)
150+
}
151+
})
152+
}
153+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ydb_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-cmp/cmp/cmpopts"
9+
"github.com/sqlc-dev/sqlc/internal/engine/ydb"
10+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
11+
)
12+
13+
func TestDropRole(t *testing.T) {
14+
tests := []struct {
15+
stmt string
16+
expected ast.Node
17+
}{
18+
{
19+
stmt: `DROP USER user1;`,
20+
expected: &ast.Statement{
21+
Raw: &ast.RawStmt{
22+
Stmt: &ast.DropRoleStmt{
23+
MissingOk: false,
24+
Roles: &ast.List{
25+
Items: []ast.Node{
26+
&ast.RoleSpec{Rolename: strPtr("user1"), Roletype: ast.RoleSpecType(1)},
27+
},
28+
},
29+
},
30+
},
31+
},
32+
},
33+
{
34+
stmt: "DROP USER IF EXISTS admin, user2",
35+
expected: &ast.Statement{
36+
Raw: &ast.RawStmt{
37+
Stmt: &ast.DropRoleStmt{
38+
MissingOk: true,
39+
Roles: &ast.List{
40+
Items: []ast.Node{
41+
&ast.RoleSpec{Rolename: strPtr("admin"), Roletype: ast.RoleSpecType(1)},
42+
&ast.RoleSpec{Rolename: strPtr("user2"), Roletype: ast.RoleSpecType(1)},
43+
},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
{
50+
stmt: "DROP GROUP team1, team2",
51+
expected: &ast.Statement{
52+
Raw: &ast.RawStmt{
53+
Stmt: &ast.DropRoleStmt{
54+
MissingOk: false,
55+
Roles: &ast.List{
56+
Items: []ast.Node{
57+
&ast.RoleSpec{Rolename: strPtr("team1"), Roletype: ast.RoleSpecType(1)},
58+
&ast.RoleSpec{Rolename: strPtr("team2"), Roletype: ast.RoleSpecType(1)},
59+
},
60+
},
61+
},
62+
},
63+
},
64+
},
65+
}
66+
67+
p := ydb.NewParser()
68+
for _, tc := range tests {
69+
t.Run(tc.stmt, func(t *testing.T) {
70+
stmts, err := p.Parse(strings.NewReader(tc.stmt))
71+
if err != nil {
72+
t.Fatalf("Error parsing %q: %v", tc.stmt, err)
73+
}
74+
if len(stmts) == 0 {
75+
t.Fatalf("Statement %q was not parsed", tc.stmt)
76+
}
77+
78+
diff := cmp.Diff(tc.expected, &stmts[0],
79+
cmpopts.IgnoreFields(ast.RawStmt{}, "StmtLocation", "StmtLen"),
80+
cmpopts.IgnoreFields(ast.RoleSpec{}, "Location"),
81+
)
82+
if diff != "" {
83+
t.Errorf("AST mismatch for %q (-expected +got):\n%s", tc.stmt, diff)
84+
}
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)