-
Notifications
You must be signed in to change notification settings - Fork 156
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
Incubating Program: Use tidb-lite to integrate TiDB into your Golang application #621
Comments
LGTM |
1 similar comment
LGTM |
what's the repo name you prefer? |
tidb-lite |
Currently there is another trend employing container techs(which is of course still external environments) into tests. A popular framework is testcontainer. It has already been used by many popular projects. It doesn't conflict with tidb-lite I think but provides another choice. |
It seems that it works for Java applications. |
Also has other languages. |
Motivation
Built-in database
When developing applications using Golang, we often need to store data locally. It is a good choice to use a built-in database, so that we can use SQL to manage the data, greatly improving our development efficiency. However, there are not many built-in databases that are compatible with the MySQL protocol in Golang.
Database-related unit tests
If our Golang application involves database-related operations, we need to write unit tests for those functions. There are usually two ways.
tidb-lite
Implementation
Basically just need to modify TiDB's main.go file. The
main
method is not required, instead, modify it as an interface to be provided externally. Users can run a TiDB withmocktikv
mode by using this interface in tidb-lite. In addition, tidb-lite also provides another interface to create a database connection, and users can access the database through this connection.Usage
For example, we have the following codes:
GetRowCount is used to get the number of eligible rows in the table, the unit test code of this function using tidb-lite is as follows:
First, we use
NewTiDBServer
to create a TiDB instance and useCreateConn
to get a link to this database. And then you can use this link to access the database, generate test data, and verify the correctness of the function.Advantage
Simplicity
One of the most important advantages of tidb-lite is simplicity. Run a TiDB directly in the code instead of running a MySQL/TiDB instance before running the unit test. This ensures that the unit test does not depend on the external environment; in addition, we don’t need to write a lot of redundancy and boring test code with
go-sqlmock
but focus on the correctness of the function.Compatible with MySQL protocol
TiDB is highly compatible with MySQL protocol. Using tidb-lite can almost completely simulate the MySQL environment.
Plan
tidb-lite now relies on TiDB, but TiDB contains so many features, such as TiKV mode, binlog, dumping, br. In fact tidb-lite only needs the
mocktikv
mode. Maybe we can do a lite version based on TiDB, which is whatlite
means.Reference
Estimated Time
60 days
Team members
Only @WangXiangUSTC. Expect another team member and a mentor.
The text was updated successfully, but these errors were encountered: