Skip to content

Commit

Permalink
feat: dbid util function and cli command (#905)
Browse files Browse the repository at this point in the history
* feat: dbid util function and cli command

* improved docs

* removed anonymous Alias struct
  • Loading branch information
brennanjl authored Jul 31, 2024
1 parent f684fd2 commit 2214f45
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
72 changes: 72 additions & 0 deletions cmd/kwil-cli/cmds/utils/dbid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package utils

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"

"github.com/kwilteam/kwil-db/cmd/common/display"
"github.com/kwilteam/kwil-db/core/utils"
"github.com/spf13/cobra"
)

var (
dbidLong = `"dbid" generates a dbid for a given schema name and deployer.`

dbidExample = `# Generate a dbid for a schema and deployer
kwil-cli utils dbid --schema=myschema --deployer=0x1234567890abcdef
# Maintain the exact deployer address, and do not trim off the 0x prefix
kwil-cli utils dbid --schema=myschema --deployer=0xnot_an_eth_address --no-trim`
)

func dbidCmd() *cobra.Command {
var schema, deployer string
var noTrim bool

cmd := &cobra.Command{
Use: "generate-dbid",
Short: dbidLong,
Long: dbidLong,
Example: dbidExample,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
deployerBts := []byte(deployer)
if strings.HasPrefix(deployer, "0x") && !noTrim {
deployer = deployer[2:]
var err error
deployerBts, err = hex.DecodeString(deployer)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf(`deployer address "%s" has 0x prefix but is not a valid hex string. try using the --no-trim flag`, deployer))
}
}

dbid := utils.GenerateDBID(schema, deployerBts)
return display.PrintCmd(cmd, &dbidOutput{DBID: dbid})
},
}

cmd.Flags().StringVar(&schema, "schema", "", "Schema name")
cmd.Flags().StringVar(&deployer, "deployer", "", "Deployer address")
cmd.Flags().BoolVar(&noTrim, "no-trim", false, "Do not trim off the 0x prefix of the deployer address")
// mark required flags
cmd.MarkFlagRequired("schema")
cmd.MarkFlagRequired("deployer")

return cmd
}

type dbidOutput struct {
DBID string `json:"dbid"`
}

func (d *dbidOutput) MarshalJSON() ([]byte, error) {
// Alias is used to avoid infinite recursion when calling json.Marshal
type Alias dbidOutput
return json.Marshal((Alias)(*d))
}

func (d *dbidOutput) MarshalText() (text []byte, err error) {
return []byte("dbid: " + d.DBID), nil
}
1 change: 1 addition & 0 deletions cmd/kwil-cli/cmds/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewCmdUtils() *cobra.Command {
kgwAuthnCmd(),
newParseCmd(),
testCmd(),
dbidCmd(),
)

return cmd
Expand Down
4 changes: 4 additions & 0 deletions internal/engine/integration/procedure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func Test_Procedures(t *testing.T) {
if format_unix_timestamp(1609459200.123456, 'YYYY-MM-DD HH24:MI:SS:US') != '2021-01-01 00:00:00:123456' {
error('format_unix_timestamp failed');
}
if generate_dbid('aa', decode('B7E2d6DABaf3B0038cFAaf09688Fa104f4409697', 'hex')) != 'xacfa19c2d4af530c6225ea139d611f91e7a55222a362dfd5eb70a826' {
error('generate_dbid failed');
}
}`,
},
{
Expand Down
29 changes: 29 additions & 0 deletions parse/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ var (
},
PGFormat: defaultFormat("digest"),
},
"generate_dbid": {
ValidateArgs: func(args []*types.DataType) (*types.DataType, error) {
// first should be text, second should be blob
if len(args) != 2 {
return nil, wrapErrArgumentNumber(2, len(args))
}

if !args[0].EqualsStrict(types.TextType) {
return nil, wrapErrArgumentType(types.TextType, args[0])
}

if !args[1].EqualsStrict(types.BlobType) {
return nil, wrapErrArgumentType(types.BlobType, args[1])
}

return types.TextType, nil
},
PGFormat: func(inputs []string, distinct, star bool) (string, error) {
if star {
return "", errStar("generate_dbid")
}

if distinct {
return "", errDistinct("generate_dbid")
}

return fmt.Sprintf(`('x' || encode(sha224(lower(%s)::bytea || %s), 'hex'))`, inputs[0], inputs[1]), nil
},
},
// array functions
"array_append": {
ValidateArgs: func(args []*types.DataType) (*types.DataType, error) {
Expand Down

0 comments on commit 2214f45

Please sign in to comment.