Skip to content

Commit

Permalink
Merge pull request #456 from kaneeldias/default-user
Browse files Browse the repository at this point in the history
Set default username for client connections
  • Loading branch information
kaneeldias authored Jun 6, 2022
2 parents 4d0dcdd + 7eb8260 commit 04b8ba0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ballerina/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public isolated client class Client {
# + connectionPool - The `sql:ConnectionPool` to be used for the connection. If there is no
# `connectionPool` provided, the global connection pool (shared by all clients) will be used
# + return - An `sql:Error` if the client creation fails
public isolated function init(string host = "localhost", string? user = (), string? password = (), string? database = (),
public isolated function init(string host = "localhost", string? user = "root", string? password = (), string? database = (),
int port = 3306, Options? options = (), sql:ConnectionPool? connectionPool = ()) returns sql:Error? {
ClientConfiguration clientConfig = {
host: host,
Expand Down
9 changes: 9 additions & 0 deletions ballerina/tests/connection-init-test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ function testWithoutHost() returns error? {
test:assertExactEquals(exitCode, (), "Initialising connection without host fails.");
}

@test:Config {
groups: ["connection", "connection-init"]
}
function testWithoutUser() returns error? {
Client dbClient = check new(host = host, port = port, password = password, database = connectDB);
sql:Error? closeResult = dbClient.close();
test:assertExactEquals(closeResult, (), "Initialising connection without user fails.");
}

@test:Config {
groups: ["connection", "connection-init"]
}
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- [Change default username for client initialization to `root`](https://github.com/ballerina-platform/ballerina-standard-library/issues/2397)


## [1.4.0] - 2022-05-30

Expand Down
36 changes: 36 additions & 0 deletions docs/proposals/default-username-for-client-connections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Define default username for client connections

_Owners_: @kaneeldias
_Reviewers_: @daneshk @niveathika
_Created_: 2022/04/25
_Updated_: 2022/04/22
_Edition_: Swan Lake
_Issues_: [#2397](https://github.com/ballerina-platform/ballerina-standard-library/issues/2397)

## Summary
Define default username to be used when connecting to a MySQL database on client initialization.

## History
The 1.3.x versions and below of the MySQL package defaulted to connecting to the database without a username
attached (i.e. an empty string).

## Goals
- Define the default username to be used when connecting to a MySQL database on client initialization.

## Motivation
The ability to connect to common databases with default credentials (as opposed to manually defining) would make the
developer experience much more quick, simple and user-friendly, especially in testing scenarios.

## Description
For MySQL databases, the default username is `root`[[1]](https://dev.mysql.com/doc/refman/8.0/en/default-privileges.html)

Modify the [client initialization method](https://github.com/ballerina-platform/module-ballerinax-mysql/blob/c2651da46c098ea6ef4a79079dc26cbd4d7cf54b/ballerina/client.bal#L36-L37)
signature to use `root` as the default value for the username instead of `()`.

```ballerina
public isolated function init(string host = "localhost", string? user = "root", string? password = (), string? database = (),
int port = 1433, string instance = "", Options? options = (), sql:ConnectionPool? connectionPool = ()) returns sql:Error? {
```

## References
[1] https://dev.mysql.com/doc/refman/8.0/en/default-privileges.html
4 changes: 2 additions & 2 deletions docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
_Owners_: @daneshk @niveathika
_Reviewers_: @daneshk
_Created_: 2022/01/14
_Updated_: 2022/03/23
_Updated_: 2022/04/25
_Edition_: Swan Lake
_Issue_: [#2289](https://github.com/ballerina-platform/ballerina-standard-library/issues/2289)

Expand Down Expand Up @@ -60,7 +60,7 @@ lifetime of the client.
# `connectionPool` provided, the global connection pool (shared by all
# clients) will be used
# + return - An `sql:Error` if the client creation fails
public isolated function init(string host = "localhost", string? user = (),
public isolated function init(string host = "localhost", string? user = "root",
string? password = (), string? database = (), int port = 3306, Options? options = (),
sql:ConnectionPool? connectionPool = ()) returns sql:Error?;
```
Expand Down

0 comments on commit 04b8ba0

Please sign in to comment.