Skip to content

Commit

Permalink
Revert "Remove pgroll sql subcommand (#515)"
Browse files Browse the repository at this point in the history
This reverts commit 358c6c6.
  • Loading branch information
andrew-farries committed Dec 10, 2024
1 parent 0b29fcf commit 0329062
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Execute() error {
rootCmd.AddCommand(migrateCmd())
rootCmd.AddCommand(pullCmd())
rootCmd.AddCommand(latestCmd())
rootCmd.AddCommand(sqlCmd())

return rootCmd.Execute()
}
39 changes: 39 additions & 0 deletions cmd/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/xataio/pgroll/pkg/sql2pgroll"
)

func sqlCmd() *cobra.Command {
sqlCmd := &cobra.Command{
Use: "sql <sql statement>",
Short: "Convert SQL statements to pgroll operations",
Args: cobra.ExactArgs(1),
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
sql := args[0]

ops, err := sql2pgroll.Convert(sql)
if err != nil {
return fmt.Errorf("failed to convert SQL statement: %w", err)
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(ops); err != nil {
return fmt.Errorf("failed to encode operations: %w", err)
}

return nil
},
}

return sqlCmd
}

0 comments on commit 0329062

Please sign in to comment.