Skip to content

Commit

Permalink
Add optional --defaultdb flag to sql tool (#2950)
Browse files Browse the repository at this point in the history
* Add optional --defaultdb flag to sql tool

* Fix unit tests

Co-authored-by: Alex Shtin <alex@shtin.com>
  • Loading branch information
yiminc and alexshtin authored Jul 13, 2022
1 parent 4f01664 commit 04988f6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,23 @@ install-schema: temporal-cassandra-tool

install-schema-mysql: temporal-sql-tool
@printf $(COLOR) "Install MySQL schema..."
./temporal-sql-tool -u temporal --pw temporal drop --db $(TEMPORAL_DB) -f
./temporal-sql-tool -u temporal --pw temporal create --db $(TEMPORAL_DB)
./temporal-sql-tool -u temporal --pw temporal --db $(TEMPORAL_DB) drop -f
./temporal-sql-tool -u temporal --pw temporal --db $(TEMPORAL_DB) create
./temporal-sql-tool -u temporal --pw temporal --db $(TEMPORAL_DB) setup-schema -v 0.0
./temporal-sql-tool -u temporal --pw temporal --db $(TEMPORAL_DB) update-schema -d ./schema/mysql/v57/temporal/versioned
./temporal-sql-tool -u temporal --pw temporal drop --db $(VISIBILITY_DB) -f
./temporal-sql-tool -u temporal --pw temporal create --db $(VISIBILITY_DB)
./temporal-sql-tool -u temporal --pw temporal --db $(VISIBILITY_DB) drop -f
./temporal-sql-tool -u temporal --pw temporal --db $(VISIBILITY_DB) create
./temporal-sql-tool -u temporal --pw temporal --db $(VISIBILITY_DB) setup-schema -v 0.0
./temporal-sql-tool -u temporal --pw temporal --db $(VISIBILITY_DB) update-schema -d ./schema/mysql/v57/visibility/versioned

install-schema-postgresql: temporal-sql-tool
@printf $(COLOR) "Install Postgres schema..."
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres drop --db $(TEMPORAL_DB) -f
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres create --db $(TEMPORAL_DB)
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(TEMPORAL_DB) drop -f
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(TEMPORAL_DB) create
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(TEMPORAL_DB) setup -v 0.0
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(TEMPORAL_DB) update-schema -d ./schema/postgresql/v96/temporal/versioned
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres drop --db $(VISIBILITY_DB) -f
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres create --db $(VISIBILITY_DB)
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(VISIBILITY_DB) drop -f
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(VISIBILITY_DB) create
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(VISIBILITY_DB) setup-schema -v 0.0
./temporal-sql-tool -u temporal -pw temporal -p 5432 --pl postgres --db $(VISIBILITY_DB) update-schema -d ./schema/postgresql/v96/visibility/versioned

Expand Down
2 changes: 2 additions & 0 deletions tools/common/schema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const (
CLIOptKeyspace = "keyspace"
// CLIOptDatabase is the cli option for database
CLIOptDatabase = "database"
// CLIOptDefaultDb is the cli option used as defaultdb to connect to
CLIOptDefaultDb = "defaultdb"
// CLIOptPluginName is the cli option for plugin name
CLIOptPluginName = "plugin"
// CLIOptConnectAttributes is the cli option for connect attributes (key/values via a url query string)
Expand Down
4 changes: 2 additions & 2 deletions tools/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ SQL_USER=$USERNAME SQL_PASSWORD=$PASSWD make install-schema-mysql
- All command below are taking MySQL as example. For postgres, simply use with "--plugin postgres"

```
temporal-sql-tool --ep $SQL_HOST_ADDR -p $port create --plugin mysql --db temporal
temporal-sql-tool --ep $SQL_HOST_ADDR -p $port create --plugin mysql --db temporal_visibility
temporal-sql-tool --ep $SQL_HOST_ADDR -p $port --db temporal --plugin mysql create
temporal-sql-tool --ep $SQL_HOST_ADDR -p $port --db temporal_visibility --plugin mysql create
```

```
Expand Down
6 changes: 3 additions & 3 deletions tools/sql/clitest/setuptaskTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (s *SetupSchemaTestSuite) TestCreateDatabase() {
"-u", testUser,
"--pw", testPassword,
"--pl", s.pluginName,
"create",
"--db", testDatabase,
"create",
})
s.NoError(err)
err = s.conn.DropDatabase(testDatabase)
Expand All @@ -105,8 +105,8 @@ func (s *SetupSchemaTestSuite) TestCreateDatabaseIdempotent() {
"-u", testUser,
"--pw", testPassword,
"--pl", s.pluginName,
"create",
"--db", testDatabase,
"create",
})
s.NoError(err)

Expand All @@ -117,8 +117,8 @@ func (s *SetupSchemaTestSuite) TestCreateDatabaseIdempotent() {
"-u", testUser,
"--pw", testPassword,
"--pl", s.pluginName,
"create",
"--db", testDatabase,
"create",
})
s.NoError(err)

Expand Down
30 changes: 12 additions & 18 deletions tools/sql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,24 @@ func createDatabase(cli *cli.Context, logger log.Logger) error {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
database := cli.String(schema.CLIOptDatabase)
if database == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptDatabase)+" argument ")))
return err
}
err = DoCreateDatabase(cfg, database)
defaultDb := cli.String(schema.CLIOptDefaultDb)
err = DoCreateDatabase(cfg, defaultDb)
if err != nil {
logger.Error("Unable to create SQL database.", tag.Error(err))
return err
}
return nil
}

func DoCreateDatabase(cfg *config.SQL, name string) error {
cfg.DatabaseName = ""
func DoCreateDatabase(cfg *config.SQL, defaultDb string) error {
dbToCreate := cfg.DatabaseName
cfg.DatabaseName = defaultDb
conn, err := NewConnection(cfg)
if err != nil {
return err
}
defer conn.Close()
return conn.CreateDatabase(name)
return conn.CreateDatabase(dbToCreate)
}

// dropDatabase drops a sql database
Expand All @@ -118,26 +115,23 @@ func dropDatabase(cli *cli.Context, logger log.Logger) error {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
database := cli.String(schema.CLIOptDatabase)
if database == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptDatabase)+" argument ")))
return err
}
err = DoDropDatabase(cfg, database)
defaultDb := cli.String(schema.CLIOptDefaultDb)
err = DoDropDatabase(cfg, defaultDb)
if err != nil {
logger.Error("Unable to drop SQL database.", tag.Error(err))
return err
}
return nil
}

func DoDropDatabase(cfg *config.SQL, name string) error {
cfg.DatabaseName = ""
func DoDropDatabase(cfg *config.SQL, defaultDb string) error {
dbToDrop := cfg.DatabaseName
cfg.DatabaseName = defaultDb
conn, err := NewConnection(cfg)
if err != nil {
return err
}
err = conn.DropDatabase(name)
err = conn.DropDatabase(dbToDrop)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions tools/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func BuildCLIOptions() *cli.App {
Usage: "creates a database",
Flags: []cli.Flag{
cli.StringFlag{
Name: schema.CLIFlagDatabase,
Usage: "name of the database",
Name: schema.CLIOptDefaultDb,
Usage: "optional default db to connect to, this is not the db to be created",
},
},
Action: func(c *cli.Context) {
Expand All @@ -205,8 +205,8 @@ func BuildCLIOptions() *cli.App {
Usage: "drops a database",
Flags: []cli.Flag{
cli.StringFlag{
Name: schema.CLIFlagDatabase,
Usage: "name of the database",
Name: schema.CLIOptDefaultDb,
Usage: "optional default db to connect to, not the db to be deleted",
},
cli.BoolFlag{
Name: schema.CLIFlagForce,
Expand All @@ -216,7 +216,7 @@ func BuildCLIOptions() *cli.App {
Action: func(c *cli.Context) {
drop := c.Bool(schema.CLIOptForce)
if !drop {
database := c.String(schema.CLIOptDatabase)
database := c.GlobalString(schema.CLIOptDatabase)
fmt.Printf("Are you sure you want to drop database %q (y/N)? ", database)
y := ""
_, _ = fmt.Scanln(&y)
Expand Down

0 comments on commit 04988f6

Please sign in to comment.