diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac37e98adb..2707b7d3250 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,11 +11,11 @@ (Add entries below in the order of server, console, cli, docs, others) - server: extend support for insert mutations to tables without primary key constraint in a MSSQL backend -- cli: migrate and seed subcommands has an option in prompt to choose and apply operation on all available databases - server: fix parsing FLOAT64s in scientific notation and non-finite ones in BigQuery - server: extend support for the `min`/`max` aggregates to all comparable types in BigQuery - cli: migrate and seed subcommands has an option in prompt to choose and apply operation on all available databases - cli: fix `metadata diff --type json | unified-json` behaving incorrectly and showing diff in YAML format. +- cli: fix regression in `migrate create` command (#7971) ## v2.1.1 diff --git a/cli/commands/migrate_create.go b/cli/commands/migrate_create.go index 2501796ab27..9eb1ec58b83 100644 --- a/cli/commands/migrate_create.go +++ b/cli/commands/migrate_create.go @@ -28,9 +28,6 @@ const migrateCreateCmdExamples = ` # Setup migration files for the first time b # Take pg_dump of schema and hasura metadata from server while specifying the schemas to include hasura migrate create init --from-server --schema myschema1,myschema2 - # Take pg_dump from server and save it as a migration and specify the schemas to include - hasura migrate create init --sql-from-server --schema myschema1,myschema2 - # Create up and down SQL migrations, providing contents as flags hasura migrate create migration-name --up-sql "CREATE TABLE article(id serial NOT NULL, title text NOT NULL, content text NOT NULL);" --down-sql "DROP TABLE article;" ` @@ -43,7 +40,7 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { migrateCreateCmd := &cobra.Command{ Use: "create [migration-name]", Short: "Create files required for a migration", - Long: "Create ``sql`` and ``yaml`` files required for a migration", + Long: "Create ``sql`` files required for a migration", Example: migrateCreateCmdExamples, SilenceUsage: true, Args: cobra.ExactArgs(1), @@ -72,19 +69,12 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { } return err } - if cmd.Flags().Changed("metadata-from-file") && ec.Config.Version != cli.V1 { - return errors.New("metadata-from-file flag can be set only with config version 1") - } - if cmd.Flags().Changed("metadata-from-server") && ec.Config.Version != cli.V1 { - return errors.New("metadata-from-server flag can be set only with config version 1") - } - - if cmd.Flags().Changed("up-sql") && !cmd.Flags().Changed("down-sql") { + if opts.upSQLChanged && !opts.downSQLChanged { ec.Logger.Warn("you are creating an up migration without a down migration") } - if cmd.Flags().Changed("down-sql") && !cmd.Flags().Changed("up-sql") { + if opts.downSQLChanged && !opts.upSQLChanged { ec.Logger.Warn("you are creating a down migration without an up migration") } return nil @@ -107,6 +97,8 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { } f := migrateCreateCmd.Flags() opts.flags = f + f.SortFlags = false + f.BoolVar(&opts.fromServer, "from-server", false, "take pg_dump of schema (default: public) and Hasura metadata from the server") f.StringVar(&opts.sqlFile, "sql-from-file", "", "path to an SQL file which contains the SQL statements") f.BoolVar(&opts.sqlServer, "sql-from-server", false, "take pg_dump from the server (default: public) and save it as a migration") @@ -116,6 +108,9 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { f.StringVar(&opts.upSQL, "up-sql", "", "sql string/query that is to be used to create an up migration") f.StringVar(&opts.downSQL, "down-sql", "", "sql string/query that is to be used to create a down migration") + if err := f.MarkDeprecated("sql-from-server", "use --from-server instead"); err != nil { + ec.Logger.Debugf("marking flag --sql-from-server as depricatef failed: %v", err) + } if err := migrateCreateCmd.MarkFlagFilename("sql-from-file", "sql"); err != nil { ec.Logger.WithError(err).Errorf("error while using a dependency library") } @@ -197,10 +192,14 @@ func (o *migrateCreateOptions) run() (version int64, err error) { } } - if o.sqlFile != "" && !o.sqlServer && o.EC.Config.Version != cli.V1 && o.upSQLChanged && o.downSQLChanged { + if o.sqlFile == "" && !o.sqlServer && o.EC.Config.Version != cli.V1 { // Set empty data for [up|down].sql - createOptions.SQLUp = []byte(``) - createOptions.SQLDown = []byte(``) + if !o.upSQLChanged { + createOptions.SQLUp = []byte(``) + } + if !o.downSQLChanged { + createOptions.SQLDown = []byte(``) + } } defer func() { diff --git a/cli/commands/migrate_create_test.go b/cli/commands/migrate_create_test.go index d955203c3d7..0669b7919c0 100644 --- a/cli/commands/migrate_create_test.go +++ b/cli/commands/migrate_create_test.go @@ -64,6 +64,35 @@ var _ = Describe("hasura migrate create (config v3)", func() { } }) + It("can create empty migrations when no data is provided", func() { + migrationName := "no_data_migration" + sourceName := randomdata.SillyName() + session := testutil.Hasura(testutil.CmdOpts{ + Args: []string{ + "migrate", + "create", + migrationName, + "--database-name", sourceName, + }, + WorkingDirectory: projectDirectory, + }) + wantKeywordList := []string{ + "Migrations files created", + migrationName, + "version", + } + + Eventually(session, timeout).Should(Exit(0)) + for _, keyword := range wantKeywordList { + Expect(session.Err.Contents()).Should(ContainSubstring(keyword)) + } + dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", sourceName)) + Expect(err).To(BeNil()) + for _, d := range dirs { + Expect(d.Name()).Should(ContainSubstring(migrationName)) + } + }) + It("can create migrations for database that is not connected to server", func() { migrationName := "create_schema_testing" sourceName := randomdata.SillyName()