refact(cmd): Encapsulate some functionality on Command and Server

This commit extracts 3 separate pieces of functionality into their own
function/method:

- Extracts the database opening with the intention to have a function
  that performs all that functionality but returns the created database
  object rather than setting it to the Server structure.
- Extracts database validation logic, to provide a common code path for
  upcoming functionality.
- Expose schema manager on the Command struct as well as providing
  common functions to acquire shared lock/close.
This commit is contained in:
Hugo Vieira committed 2022-09-12 17:55:32 +01:00
1 parent 621f8a9ff2
commit 8ab9ffbcac
6 files changed
+117 -51

No files matched your search

+1 -1
View File
@@ -226,7 +226,7 @@ func TestVerifyOplogIsEmpty(t *testing.T) {
cmd := InitCommand{Server: base.NewServer(base.NewCommand(cli.NewMockUi()))}
cmd.DatabaseUrl = u
require.NoError(t, cmd.ConnectToDatabase(ctx, dialect))
require.NoError(t, cmd.OpenAndSetServerDatabase(ctx, dialect))
assert.NoError(t, cmd.verifyOplogIsEmpty(ctx))
}
+1 -1
View File
@@ -287,7 +287,7 @@ func (c *InitCommand) Run(args []string) (retCode int) {
return base.CommandUserError
}
// Everything after is done with normal database URL and is affecting actual data
if err := c.ConnectToDatabase(c.Context, dialect); err != nil {
if err := c.OpenAndSetServerDatabase(c.Context, dialect); err != nil {
c.UI.Error(fmt.Errorf("Error connecting to database after migrations: %w", err).Error())
return base.CommandCliError
}