From 6e5186475c0ca9035e30aba2fddfd01752ce594b Mon Sep 17 00:00:00 2001 From: hasura-bot Date: Mon, 14 Jun 2021 17:40:44 +0530 Subject: [PATCH] cli: add exclude-schema & full flags to migrate create command (#1564) GITHUB_PR_NUMBER: 5713 GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/5713 https://github.com/hasura/graphql-engine-mono/pull/1564 Co-authored-by: Sameer Kolhar <6604943+kolharsam@users.noreply.github.com> Co-authored-by: Kali Vara Purushotham Santhati <72007599+purush7@users.noreply.github.com> GitOrigin-RevId: b4441cbde667c66f3ae203d752d6b33a0833f34e --- CHANGELOG.md | 1 + cli/commands/migrate_create.go | 22 +++++++++++++++++++- cli/migrate/database/driver_test.go | 2 +- cli/migrate/database/hasuradb/schema_dump.go | 14 ++++++++++--- cli/migrate/database/schema_dump.go | 2 +- cli/migrate/migrate.go | 4 ++-- 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a80582fa6c5b3..95452c1b11e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - console: add modify functionality on columns, primary keys & unique keys to MS SQL Server tables - cli: add interactive prompt to get input when `--database-name` flag is missing - cli: fix metadata export to avoid unnecessary empty lines in actions.graphql (#5338) +- cli: add exclude-schema & full flags to migrate create command (closes #4301) (#5713) ## v2.0.0-beta.1 diff --git a/cli/commands/migrate_create.go b/cli/commands/migrate_create.go index 5e6ef24e1f37a..7d416df8c850b 100644 --- a/cli/commands/migrate_create.go +++ b/cli/commands/migrate_create.go @@ -29,6 +29,12 @@ const migrateCreateCmdExamples = ` # Setup migration files for the first time b # 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 + + # Take pg_dump from server with all schemas and extensions + hasura migrate create init --from-server --full + + # Take pg_dump from server by excluding certain schemas + hasura migrate create init --from-server --exclude-schema public,hdb_catalog # 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;" @@ -81,6 +87,8 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command { f.BoolVar(&opts.metaDataServer, "metadata-from-server", false, "take metadata from the server and write it as an up migration file") 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") + f.StringSliceVar(&opts.excludeSchema, "exclude-schema", []string{}, "take pg_dump with the --exclude-schema flag") + f.BoolVar(&opts.full, "full", false, "take pg_dump with all tables across all schemas without the --schema option") migrateCreateCmd.MarkFlagFilename("sql-from-file") migrateCreateCmd.MarkFlagFilename("metadata-from-file") @@ -103,7 +111,11 @@ type migrateCreateOptions struct { schemaNames []string upSQL string downSQL string + excludeSchema []string + full bool + Source cli.Source + } func (o *migrateCreateOptions) run() (version int64, err error) { @@ -117,6 +129,14 @@ func (o *migrateCreateOptions) run() (version int64, err error) { } } + if len(o.excludeSchema) != 0 && !o.fromServer { + return 0, errors.New("exclude-schema flag has to be used along with from-server") + } + + if o.full && !o.fromServer { + return 0, errors.New("full flag has to be used along with from-server") + } + if o.flags.Changed("metadata-from-file") && o.EC.Config.Version != cli.V1 { return 0, errors.New("metadata-from-file flag can be set only with config version 1") } @@ -157,7 +177,7 @@ func (o *migrateCreateOptions) run() (version int64, err error) { } } if o.sqlServer { - data, err := migrateDrv.ExportSchemaDump(o.schemaNames, o.Source.Name, o.Source.Kind) + data, err := migrateDrv.ExportSchemaDump(o.schemaNames, o.Source.Name, o.Source.Kind, o.excludeSchema, o.full) if err != nil { return 0, errors.Wrap(err, "cannot fetch schema dump") } diff --git a/cli/migrate/database/driver_test.go b/cli/migrate/database/driver_test.go index dfe78cd722e77..d5fc7a9a182cb 100644 --- a/cli/migrate/database/driver_test.go +++ b/cli/migrate/database/driver_test.go @@ -98,7 +98,7 @@ func (m *mockDriver) Squash(list *CustomList, ret chan<- interface{}) { func (m *mockDriver) EnableCheckMetadataConsistency(enabled bool) { } -func (m *mockDriver) ExportSchemaDump(schemaName []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) { +func (m *mockDriver) ExportSchemaDump(schemaName []string, sourceName string, sourceKind hasura.SourceKind, excludeSchema []string, full bool) ([]byte, error) { return nil, nil } diff --git a/cli/migrate/database/hasuradb/schema_dump.go b/cli/migrate/database/hasuradb/schema_dump.go index 7488bcac43ccb..512a5a4929ab7 100644 --- a/cli/migrate/database/hasuradb/schema_dump.go +++ b/cli/migrate/database/hasuradb/schema_dump.go @@ -7,12 +7,20 @@ import ( "github.com/hasura/graphql-engine/cli/internal/hasura" ) -func (h *HasuraDB) ExportSchemaDump(schemaNames []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) { +// ExportSchemaDump calls pg_dump to help initialize the first set of migrations +func (h *HasuraDB) ExportSchemaDump(schemaNames []string, sourceName string, sourceKind hasura.SourceKind, excludeSchema []string, full bool) ([]byte, error) { switch sourceKind { case hasura.SourceKindPG: opts := []string{"-O", "-x", "--schema-only"} - for _, s := range schemaNames { - opts = append(opts, "--schema", s) + if len(excludeSchema) != 0 { + for _, s := range excludeSchema { + opts = append(opts, "--exclude-schema", s) + } + } + if !full && len(excludeSchema) != 0 { + for _, s := range schemaNames { + opts = append(opts, "--schema", s) + } } query := SchemaDump{ Opts: opts, diff --git a/cli/migrate/database/schema_dump.go b/cli/migrate/database/schema_dump.go index 047e324f409da..e08539b160843 100644 --- a/cli/migrate/database/schema_dump.go +++ b/cli/migrate/database/schema_dump.go @@ -3,5 +3,5 @@ package database import "github.com/hasura/graphql-engine/cli/internal/hasura" type SchemaDriver interface { - ExportSchemaDump(schemaName []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) + ExportSchemaDump(schemaName []string, sourceName string, sourceKind hasura.SourceKind, excludeSchema []string, full bool) ([]byte, error) } diff --git a/cli/migrate/migrate.go b/cli/migrate/migrate.go index e5a16ecca5be1..168712e54a845 100644 --- a/cli/migrate/migrate.go +++ b/cli/migrate/migrate.go @@ -330,8 +330,8 @@ func (m *Migrate) GetUnappliedMigrations(version uint64) []uint64 { return m.sourceDrv.GetUnappliedMigrations(version) } -func (m *Migrate) ExportSchemaDump(schemName []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) { - return m.databaseDrv.ExportSchemaDump(schemName, sourceName, sourceKind) +func (m *Migrate) ExportSchemaDump(schemName []string, sourceName string, sourceKind hasura.SourceKind, excludeSchema []string, full bool) ([]byte, error) { + return m.databaseDrv.ExportSchemaDump(schemName, sourceName, sourceKind, excludeSchema, full) } func (m *Migrate) RemoveVersions(versions []uint64) error {