Skip to content

Commit

Permalink
cli: refactor datasource -> database
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 89bb1fb
  • Loading branch information
scriptonist authored and hasura-bot committed Feb 17, 2021
1 parent e64d6d0 commit cb029c3
Show file tree
Hide file tree
Showing 54 changed files with 217 additions and 228 deletions.
12 changes: 6 additions & 6 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (s *ServerConfig) SetHTTPClient() error {
return nil
}

type DatasourceConfig struct {
type DatabaseConfig struct {
Name string `yaml:"name,omitempty"`
Type string `yaml:"type,omitempty"`
MigrationsDirectory string `yaml:"migrations_directory,omitempty"`
Expand All @@ -337,8 +337,8 @@ type Config struct {
// SeedsDirectory defines the directory where seed files will be stored
SeedsDirectory string `yaml:"seeds_directory,omitempty"`
// ActionConfig defines the config required to create or generate codegen for an action.
ActionConfig *types.ActionExecutionConfig `yaml:"actions,omitempty"`
DatasourcesConfig []DatasourceConfig `yaml:"datasources,omitempty"`
ActionConfig *types.ActionExecutionConfig `yaml:"actions,omitempty"`
DatabasesConfig []DatabaseConfig `yaml:"datasources,omitempty"`
}

// ExecutionContext contains various contextual information required by the cli
Expand Down Expand Up @@ -428,8 +428,8 @@ type ExecutionContext struct {
// instance of API client which communicates with Hasura API
APIClient *hasura.Client

// current datasource on which operation is being done
Datasource string
// current database on which operation is being done
Database string
HasMetadataV3 bool
}

Expand Down Expand Up @@ -936,7 +936,7 @@ func GetAdminSecretHeaderName(v *version.Version) string {
}
return XHasuraAdminSecret
}
func GetDatasourceOps(ec *ExecutionContext) hasura.DatasourceOperations {
func GetDatabaseOps(ec *ExecutionContext) hasura.DatabaseOperations {
if !ec.HasMetadataV3 {
return ec.APIClient.V1Query
}
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
return err
}
if ec.Config.Version >= cli.V3 {
if !cmd.Flags().Changed("datasource") {
return errors.New("datasource flag is required")
if !cmd.Flags().Changed("database") {
return errors.New("database flag is required")
}
} else {
if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil {
Expand All @@ -51,7 +51,7 @@ func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
}

f := migrateCmd.PersistentFlags()
f.StringVar(&ec.Datasource, "datasource", "", "datasource name which operation should be applied")
f.StringVar(&ec.Database, "database", "", "database on which operation should be applied")

f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/migrate_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Datasource = ec.Datasource
opts.Database = ec.Database
if opts.dryRun && opts.SkipExecution {
return errors.New("both --skip-execution and --dry-run flags cannot be used together")
}
Expand Down Expand Up @@ -116,7 +116,7 @@ type MigrateApplyOptions struct {
GotoVersion string
SkipExecution bool
dryRun bool
Datasource string
Database string
}

func (o *MigrateApplyOptions) Run() error {
Expand All @@ -125,7 +125,7 @@ func (o *MigrateApplyOptions) Run() error {
return errors.Wrap(err, "error validating flags")
}

migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Datasource)
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Database)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cli/commands/migrate_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0]
opts.EC.Spin("Creating migration files...")
opts.Datasource = ec.Datasource
opts.Database = ec.Database
version, err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
Expand Down Expand Up @@ -97,12 +97,12 @@ type migrateCreateOptions struct {
schemaNames []string
upSQL string
downSQL string
Datasource string
Database string
}

func (o *migrateCreateOptions) run() (version int64, err error) {
timestamp := getTime()
createOptions := mig.New(timestamp, o.name, filepath.Join(o.EC.MigrationDir, o.Datasource))
createOptions := mig.New(timestamp, o.name, filepath.Join(o.EC.MigrationDir, o.Database))

if o.fromServer {
o.sqlServer = true
Expand Down Expand Up @@ -137,7 +137,7 @@ func (o *migrateCreateOptions) run() (version int64, err error) {

var migrateDrv *migrate.Migrate
if o.sqlServer || o.metaDataServer || o.flags.Changed("up-sql") || o.flags.Changed("down-sql") {
migrateDrv, err = migrate.NewMigrate(o.EC, true, o.Datasource)
migrateDrv, err = migrate.NewMigrate(o.EC, true, o.Database)
if err != nil {
return 0, errors.Wrap(err, "cannot create migrate instance")
}
Expand All @@ -151,7 +151,7 @@ func (o *migrateCreateOptions) run() (version int64, err error) {
}
}
if o.sqlServer {
data, err := migrateDrv.ExportSchemaDump(o.schemaNames, o.Datasource)
data, err := migrateDrv.ExportSchemaDump(o.schemaNames, o.Database)
if err != nil {
return 0, errors.Wrap(err, "cannot fetch schema dump")
}
Expand Down
10 changes: 5 additions & 5 deletions cli/commands/migrate_squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newMigrateSquashCmd(ec *cli.ExecutionContext) *cobra.Command {
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
opts.newVersion = getTime()
opts.Datasource = ec.Datasource
opts.Database = ec.Database
if opts.EC.HasMetadataV3 && opts.EC.Config.Version < cli.V2 {
return fmt.Errorf("squashing when using metadata V3 is supported from Config V2 only")
}
Expand All @@ -65,19 +65,19 @@ type migrateSquashOptions struct {
newVersion int64

deleteSource bool
Datasource string
Database string
}

func (o *migrateSquashOptions) run() error {
o.EC.Logger.Warnln("This command is currently experimental and hence in preview, correctness of squashed migration is not guaranteed!")
o.EC.Spin(fmt.Sprintf("Squashing migrations from %d to latest...", o.from))
defer o.EC.Spinner.Stop()
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Datasource)
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Database)
if err != nil {
return errors.Wrap(err, "unable to initialize migrations driver")
}

versions, err := mig.SquashCmd(migrateDrv, o.from, o.newVersion, o.name, filepath.Join(o.EC.MigrationDir, o.Datasource))
versions, err := mig.SquashCmd(migrateDrv, o.from, o.newVersion, o.name, filepath.Join(o.EC.MigrationDir, o.Database))
o.EC.Spinner.Stop()
if err != nil {
return errors.Wrap(err, "unable to squash migrations")
Expand All @@ -98,7 +98,7 @@ func (o *migrateSquashOptions) run() error {
for _, v := range versions {
delOptions := mig.CreateOptions{
Version: strconv.FormatInt(v, 10),
Directory: filepath.Join(o.EC.MigrationDir, o.Datasource),
Directory: filepath.Join(o.EC.MigrationDir, o.Database),
}
err = delOptions.Delete()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Fetching migration status...")
opts.Datasource = ec.Datasource
opts.Database = ec.Database
status, err := opts.Run()
opts.EC.Spinner.Stop()
if err != nil {
Expand All @@ -44,12 +44,12 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
}

type MigrateStatusOptions struct {
EC *cli.ExecutionContext
Datasource string
EC *cli.ExecutionContext
Database string
}

func (o *MigrateStatusOptions) Run() (*migrate.Status, error) {
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Datasource)
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Database)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/scripts_upgrade_multiple_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newUpdateMultipleSources(ec *cli.ExecutionContext) *cobra.Command {
Logger: ec.Logger,
EC: ec,
}
return scripts.UpgradeProjectToMultipleSources(opts)
return scripts.UpdateProjectV3(opts)
},
}
return cmd
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewSeedCmd(ec *cli.ExecutionContext) *cobra.Command {
return err
}
if ec.Config.Version >= cli.V3 {
if !cmd.Flags().Changed("datasource") {
return errors.New("datasource flag is required")
if !cmd.Flags().Changed("database") {
return errors.New("database flag is required")
}
} else {
if err := scripts.CheckIfUpdateToConfigV3IsRequired(ec); err != nil {
Expand All @@ -45,7 +45,7 @@ func NewSeedCmd(ec *cli.ExecutionContext) *cobra.Command {
)

f := seedCmd.PersistentFlags()
f.StringVar(&ec.Datasource, "datasource", "", "datasource name which operation should be applied")
f.StringVar(&ec.Database, "database", "", "database on which operation should be applied")

f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/seed_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type SeedApplyOptions struct {
EC *cli.ExecutionContext

// seed file to apply
FileNames []string
Datasource string
FileNames []string
Database string
}

func newSeedApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
Expand All @@ -35,7 +35,7 @@ func newSeedApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Applying seeds...")
opts.Datasource = ec.Datasource
opts.Database = ec.Database
err := opts.Run()
opts.EC.Spinner.Stop()
if err != nil {
Expand All @@ -55,5 +55,5 @@ func (o *SeedApplyOptions) Run() error {
return err
}
fs := afero.NewOsFs()
return seed.ApplySeedsToDatabase(o.EC, fs, migrateDriver, o.FileNames, o.Datasource)
return seed.ApplySeedsToDatabase(o.EC, fs, migrateDriver, o.FileNames, o.Database)
}
16 changes: 8 additions & 8 deletions cli/commands/seed_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type SeedNewOptions struct {
FromTableNames []string

// seed file that was created
FilePath string
Datasource string
FilePath string
Database string
}

func newSeedCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
Expand All @@ -50,7 +50,7 @@ func newSeedCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.SeedName = args[0]
opts.Datasource = ec.Datasource
opts.Database = ec.Database
err := opts.Run()
if err != nil {
return err
Expand All @@ -66,15 +66,15 @@ func newSeedCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
}

func (o *SeedNewOptions) Run() error {
datasourceDirectory := filepath.Join(o.EC.SeedsDirectory, o.Datasource)
if f, _ := os.Stat(datasourceDirectory); f == nil {
if err := os.MkdirAll(datasourceDirectory, 0755); err != nil {
databaseDirectory := filepath.Join(o.EC.SeedsDirectory, o.Database)
if f, _ := os.Stat(databaseDirectory); f == nil {
if err := os.MkdirAll(databaseDirectory, 0755); err != nil {
return err
}
}
createSeedOpts := seed.CreateSeedOpts{
UserProvidedSeedName: o.SeedName,
DirectoryPath: filepath.Join(o.EC.SeedsDirectory, o.Datasource),
DirectoryPath: filepath.Join(o.EC.SeedsDirectory, o.Database),
}

// If we are initializing from a database table
Expand All @@ -87,7 +87,7 @@ func (o *SeedNewOptions) Run() error {
return errors.Wrap(err, "cannot initialize migrate driver")
}
// Send the query
body, err = migrateDriver.ExportDataDump(o.FromTableNames, o.Datasource)
body, err = migrateDriver.ExportDataDump(o.FromTableNames, o.Database)
if err != nil {
return errors.Wrap(err, "exporting seed data")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/integration_test/v3/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestMetadataCmd(t *testing.T, ec *cli.ExecutionContext) {
&commands.MigrateApplyOptions{
EC: ec,
DownMigration: "all",
Datasource: "default",
Database: "default",
},
nil,
"",
Expand Down
22 changes: 11 additions & 11 deletions cli/integration_test/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
status migrate.Status
}{
{"apply-up-all-migrations", &commands.MigrateApplyOptions{
EC: ec,
Datasource: "default",
EC: ec,
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -51,7 +51,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-down-1-migration", &commands.MigrateApplyOptions{
EC: ec,
DownMigration: "1",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -68,7 +68,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-down-all-migration", &commands.MigrateApplyOptions{
EC: ec,
DownMigration: "all",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -85,7 +85,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-goto-2-migration", &commands.MigrateApplyOptions{
EC: ec,
GotoVersion: "2",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -102,7 +102,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-goto-nil-migration", &commands.MigrateApplyOptions{
EC: ec,
GotoVersion: "-1",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -119,7 +119,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-up-1-migration", &commands.MigrateApplyOptions{
EC: ec,
UpMigration: "1",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -136,7 +136,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
{"apply-version-2-up-migration", &commands.MigrateApplyOptions{
EC: ec,
VersionMigration: "2",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand All @@ -154,7 +154,7 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
EC: ec,
VersionMigration: "2",
MigrationType: "down",
Datasource: "default",
Database: "default",
}, nil, migrate.Status{
Index: []uint64{1, 2},
Migrations: map[uint64]*migrate.MigrationStatus{
Expand Down Expand Up @@ -182,8 +182,8 @@ func TestMigrateCmd(t *testing.T, ec *cli.ExecutionContext) {
t.Fatal(err)
}
statusOpts := &commands.MigrateStatusOptions{
EC: ec,
Datasource: "default",
EC: ec,
Database: "default",
}
actualStatus, err := statusOpts.Run()
if err != nil {
Expand Down
Loading

0 comments on commit cb029c3

Please sign in to comment.