Skip to content

Commit cacc250

Browse files
kvchandrew-farries
andauthored
Use sql2pgroll.Convert in convert command to translate SQL statements from migration file and stdin (#695)
This PR adds the implementation to the subcommand `convert`. From now on, `pgroll convert` can translate several SQL migrations into a single migration file. It has one flag called `migration-name` so users can specify a name for their migration. If they don't, `pgroll` sets the name to the current timestamp. ```sh $ pgroll convert --help Convert SQL statements to pgroll migrations. The command can read SQL statements from stdin or a file Usage: pgroll convert <path to file with migrations> [flags] Flags: -h, --help help for convert -n, --name string Name of the migration (default "{current_timestamp}") ``` The command expects input either from an SQL migration file or stdin. ```sh $ echo "CREATE TABLE t1(); alter table t1 add column name text; create type t1 as enum ('1','2','3')" | pgroll convert { "name": "20250220125242", "operations": [ { "create_table": { "columns": null, "name": "t1" } }, { "add_column": { "column": { "name": "name", "nullable": true, "type": "text" }, "table": "t1", "up": "TODO: Implement SQL data migration" } }, { "sql": { "up": "create type t1 as enum ('1','2','3')" } } ] } ``` I am adding more detailed documentation when we expose the command publicly. --------- Co-authored-by: Andrew Farries <[email protected]>
1 parent bc24f9e commit cacc250

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

cli-definition.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@
2121
},
2222
{
2323
"name": "convert",
24-
"short": "Convert SQL statements to pgroll operations from SQL",
24+
"short": "Convert SQL statements to a pgroll migration",
2525
"use": "convert <path to file with migrations>",
2626
"example": "",
27-
"flags": [],
27+
"flags": [
28+
{
29+
"name": "name",
30+
"shorthand": "n",
31+
"description": "Name of the migration",
32+
"default": "{current_timestamp}"
33+
}
34+
],
2835
"subcommands": [],
2936
"args": [
3037
"migration-file"

cmd/convert.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
package cmd
44

55
import (
6+
"bytes"
7+
"encoding/json"
68
"fmt"
79
"io"
810
"os"
11+
"time"
912

1013
"github.com/spf13/cobra"
14+
"github.com/xataio/pgroll/pkg/migrations"
15+
"github.com/xataio/pgroll/pkg/sql2pgroll"
1116
)
1217

1318
func convertCmd() *cobra.Command {
19+
var migrationName string
20+
1421
convertCmd := &cobra.Command{
1522
Use: "convert <path to file with migrations>",
16-
Short: "Convert SQL statements to pgroll operations from SQL",
23+
Short: "Convert SQL statements to a pgroll migration",
24+
Long: "Convert SQL statements to a pgroll migration. The command can read SQL statements from stdin or a file",
1725
Args: cobra.MaximumNArgs(1),
1826
ValidArgs: []string{"migration-file"},
1927
Hidden: true,
@@ -24,11 +32,24 @@ func convertCmd() *cobra.Command {
2432
}
2533
defer reader.Close()
2634

27-
_, err = scanSQLStatements(reader)
28-
return err
35+
if migrationName == "{current_timestamp}" {
36+
migrationName = time.Now().Format("20060102150405")
37+
}
38+
migration, err := sqlStatementsToMigration(reader, migrationName)
39+
if err != nil {
40+
return err
41+
}
42+
enc := json.NewEncoder(os.Stdout)
43+
enc.SetIndent("", " ")
44+
if err := enc.Encode(migration); err != nil {
45+
return fmt.Errorf("encode migration: %w", err)
46+
}
47+
return nil
2948
},
3049
}
3150

51+
convertCmd.Flags().StringVarP(&migrationName, "name", "n", "{current_timestamp}", "Name of the migration")
52+
3253
return convertCmd
3354
}
3455

@@ -39,6 +60,18 @@ func openSQLReader(args []string) (io.ReadCloser, error) {
3960
return os.Open(args[0])
4061
}
4162

42-
func scanSQLStatements(reader io.Reader) ([]string, error) {
43-
panic("not implemented")
63+
func sqlStatementsToMigration(reader io.Reader, name string) (migrations.Migration, error) {
64+
var buf bytes.Buffer
65+
_, err := io.Copy(&buf, reader)
66+
if err != nil {
67+
return migrations.Migration{}, err
68+
}
69+
ops, err := sql2pgroll.Convert(buf.String())
70+
if err != nil {
71+
return migrations.Migration{}, err
72+
}
73+
return migrations.Migration{
74+
Name: name,
75+
Operations: ops,
76+
}, nil
4477
}

0 commit comments

Comments
 (0)