Skip to content
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

cli: add exclude-schema & full flags to migrate create command #5713

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 21 additions & 1 deletion cli/commands/migrate_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/migrate/database/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion cli/migrate/database/hasuradb/schema_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ 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"}
if len(excludeSchema) != 0 {
for _, s := range excludeSchema {
opts = append(opts, "--exclude-schema", s)
}
}
if !full {
for _, s := range schemaNames {
opts = append(opts, "--schema", s)
}
}
query := SchemaDump{
Opts: opts,
CleanOutput: true,
Expand Down
2 changes: 1 addition & 1 deletion cli/migrate/database/schema_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cli/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down