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

Added logic for working with Tarantool schema via Box #426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KaymeKaydex
Copy link

@KaymeKaydex KaymeKaydex commented Dec 27, 2024

  • Implemented the box.Schema() method that returns a Schema object for schema-related operations

What has been done? Why? What problem is being solved?

I didn't forget about (remove if it is not applicable):

Related issues:

- Implemented the `box.Schema()` method that returns a `Schema` object for schema-related operations
Comment on lines 12 to -13
b := box.New(nil)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, revert the change if it is no necessary.

Comment on lines +5 to +9
// Schema represents the schema-related operations in Tarantool.
// It holds a connection to interact with the Tarantool instance.
type Schema struct {
conn tarantool.Doer // Connection interface for interacting with Tarantool.
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok in the separate file.

Comment on lines +11 to +17
// Schema returns a new Schema instance, providing access to schema-related operations.
// It uses the connection from the Box instance to communicate with Tarantool.
func (b *Box) Schema() *Schema {
return &Schema{
conn: b.conn, // Pass the Box connection to the Schema.
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is related to the Box type, please, move it into box.go.

Comment on lines +11 to +17
// Schema returns a new Schema instance, providing access to schema-related operations.
// It uses the connection from the Box instance to communicate with Tarantool.
func (b *Box) Schema() *Schema {
return &Schema{
conn: b.conn, // Pass the Box connection to the Schema.
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is a helper. It's ok. But please, add a NewSchema(conn tarantool.Doer) *Schema function as the type constructor to avoid are too close relationships between types.

Comment on lines +66 to +143
func TestBox_Sugar_Schema(t *testing.T) {
const (
username = "opensource"
password = "enterprise"
)

ctx := context.TODO()

conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
require.NoError(t, err)

b := box.New(conn)

// Create new user
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.NoError(t, err)

// Get error that user already exists
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{Password: password})
require.Error(t, err)

// Require that error code is ER_USER_EXISTS
var boxErr tarantool.Error
errors.As(err, &boxErr)
require.Equal(t, iproto.ER_USER_EXISTS, boxErr.Code)

// Check that already exists by exists call procedure
exists, err := b.Schema().User().Exists(ctx, username)
require.True(t, exists)
require.NoError(t, err)

// There is no error if IfNotExists option is true
err = b.Schema().User().Create(ctx, username, box.UserCreateOptions{
Password: password,
IfNotExists: true,
})

require.NoError(t, err)

// Require password hash
hash, err := b.Schema().User().Password(ctx, username)
require.NoError(t, err)
require.NotEmpty(t, hash)

// Check that password is valid and we can connect to tarantool with such credentials
var newUserDialer = tarantool.NetDialer{
Address: server,
User: username,
Password: password,
}

// We can connect with our new credentials
newUserConn, err := tarantool.Connect(ctx, newUserDialer, tarantool.Opts{})
require.NoError(t, err)
require.NotNil(t, newUserConn)
require.NoError(t, newUserConn.Close())

// Try to drop user
err = b.Schema().User().Drop(ctx, username, box.UserDropOptions{})
require.NoError(t, err)

// Require error cause user already deleted
err = b.Schema().User().Drop(ctx, username, box.UserDropOptions{})
require.Error(t, err)

// Require that error code is ER_NO_SUCH_USER
errors.As(err, &boxErr)
require.Equal(t, iproto.ER_NO_SUCH_USER, boxErr.Code)

// No error with option IfExists: true
err = b.Schema().User().Drop(ctx, username, box.UserDropOptions{IfExists: true})
require.NoError(t, err)

// Check that user not exists after drop
exists, err = b.Schema().User().Exists(ctx, username)
require.False(t, exists)
require.NoError(t, err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test could be splitted into a several separate tests. Please, do it.

Comment on lines +16 to +19
// User returns a new SchemaUser instance, allowing schema-related user operations.
func (s *Schema) User() *SchemaUser {
return &SchemaUser{conn: s.conn}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same about a constructor as for the Schema type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add unit-test for requests and responses types in the file.

We probably also need to think about naming and location of requests types in this package. As example, at now some requests are at request.go file. It could be confusing.

There are two ways I see:

  1. We could locate all requests/responses into the request.go file (I don't like the idea).
  2. We need move types from request.go into a proper files in the same manner as for the current ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants