-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inspect): add role-configs command to list db roles with custom …
…settings (#2818) * added role-configs command to list users with custom settings * Update internal/inspect/role_configs/role_configs.go --------- Co-authored-by: Han Qiao <[email protected]>
- Loading branch information
1 parent
cf2136a
commit 68f9c56
Showing
5 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package role_configs | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"fmt" | ||
|
||
"github.com/go-errors/errors" | ||
"github.com/jackc/pgconn" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/spf13/afero" | ||
"github.com/supabase/cli/internal/migration/list" | ||
"github.com/supabase/cli/internal/utils" | ||
"github.com/supabase/cli/pkg/pgxv5" | ||
) | ||
|
||
//go:embed role_configs.sql | ||
var RoleConfigsQuery string | ||
|
||
type Result struct { | ||
Role_name string | ||
Custom_config string | ||
} | ||
|
||
func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error { | ||
conn, err := utils.ConnectByConfig(ctx, config, options...) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close(context.Background()) | ||
rows, err := conn.Query(ctx, RoleConfigsQuery) | ||
if err != nil { | ||
return errors.Errorf("failed to query rows: %w", err) | ||
} | ||
result, err := pgxv5.CollectRows[Result](rows) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
table := "|Role name|Custom config|\n|-|-|\n" | ||
for _, r := range result { | ||
table += fmt.Sprintf("|`%s`|`%s`|\n", r.Role_name, r.Custom_config) | ||
} | ||
|
||
return list.RenderTable(table) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
select | ||
rolname as role_name, | ||
array_to_string(rolconfig, ',', '*') as custom_config | ||
from | ||
pg_roles where rolconfig is not null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package role_configs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/jackc/pgconn" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/supabase/cli/pkg/pgtest" | ||
) | ||
|
||
var dbConfig = pgconn.Config{ | ||
Host: "127.0.0.1", | ||
Port: 5432, | ||
User: "admin", | ||
Password: "password", | ||
Database: "postgres", | ||
} | ||
|
||
func TestRoleCommand(t *testing.T) { | ||
t.Run("inspects role connections", func(t *testing.T) { | ||
// Setup in-memory fs | ||
fsys := afero.NewMemMapFs() | ||
// Setup mock postgres | ||
conn := pgtest.NewConn() | ||
defer conn.Close(t) | ||
conn.Query(RoleConfigsQuery). | ||
Reply("SELECT 1", Result{ | ||
Role_name: "postgres", | ||
Custom_config: "statement_timeout=3s", | ||
}) | ||
// Run test | ||
err := Run(context.Background(), dbConfig, fsys, conn.Intercept) | ||
// Check error | ||
assert.NoError(t, err) | ||
}) | ||
} |