From 097d705c067518e422bfc2c83414627be2844227 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 11 Dec 2024 21:03:57 +0000 Subject: [PATCH] Revert "Remove `pgroll sql` subcommand (#515)" This reverts commit 358c6c667f89d362503bb25f1f3abdcf2c5cab9f. --- cmd/root.go | 1 + cmd/sql.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 cmd/sql.go diff --git a/cmd/root.go b/cmd/root.go index 7dd51cef..e65ebef0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -79,6 +79,7 @@ func Execute() error { rootCmd.AddCommand(migrateCmd()) rootCmd.AddCommand(pullCmd()) rootCmd.AddCommand(latestCmd()) + rootCmd.AddCommand(sqlCmd()) return rootCmd.Execute() } diff --git a/cmd/sql.go b/cmd/sql.go new file mode 100644 index 00000000..da095328 --- /dev/null +++ b/cmd/sql.go @@ -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 ", + 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 +}