-
Notifications
You must be signed in to change notification settings - Fork 59
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
base: master
Are you sure you want to change the base?
Conversation
070c6ca
to
2e271da
Compare
- Implemented the `box.Schema()` method that returns a `Schema` object for schema-related operations
2e271da
to
9f736f4
Compare
b := box.New(nil) | ||
|
There was a problem hiding this comment.
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.
// 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. | ||
} |
There was a problem hiding this comment.
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.
// 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. | ||
} | ||
} |
There was a problem hiding this comment.
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.
// 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. | ||
} | ||
} |
There was a problem hiding this comment.
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.
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) | ||
} |
There was a problem hiding this comment.
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.
// User returns a new SchemaUser instance, allowing schema-related user operations. | ||
func (s *Schema) User() *SchemaUser { | ||
return &SchemaUser{conn: s.conn} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- We could locate all requests/responses into the
request.go
file (I don't like the idea). - We need move types from
request.go
into a proper files in the same manner as for the current ones.
box.Schema()
method that returns aSchema
object for schema-related operationsWhat has been done? Why? What problem is being solved?
I didn't forget about (remove if it is not applicable):
Related issues: