8000 Use `sql2pgroll.Convert` in `convert` command to translate SQL statements from migration file and stdin by kvch · Pull Request #695 · xataio/pgroll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use sql2pgroll.Convert in convert command to translate SQL statements from migration file and stdin #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2025

Conversation

kvch
Copy link
Contributor
@kvch kvch commented Feb 20, 2025

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 name so users can specify a name for their migration.
If they don't, pgroll sets the name to the current timestamp.

$ 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.

$ 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.

@kvch kvch force-pushed the feature-parse-whole-migration-file branch from 49e93f5 to fd8a6e6 Compare February 20, 2025 12:05
@kvch kvch requested a review from andrew-farries February 20, 2025 12:09
cmd/convert.go Outdated
},
}

convertCmd.Flags().StringVar(&migrationName, "migration-name", "{current_timestamp}", "Name of the migration")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this flag should be called name rather than migration-name. It should also have a short form of -n (use StringVarP).

cmd/convert.go Outdated
@@ -24,11 +32,24 @@ func convertCmd() *cobra.Command {
}
defer reader.Close()

_, err = scanSQLStatements(reader)
return err
if migrationName == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check doesn't work as expected because the flag definition assigns the string "{current_timestamp}" to migrationName if it isn't supplied by the user.

kvch and others added 3 commits February 20, 2025 18:16
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
@kvch kvch requested a review from andrew-farries February 20, 2025 17:23
@kvch kvch merged commit cacc250 into xataio:main Feb 21, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0