dolthub / dolt

Dolt – Git for Data
Apache License 2.0
17.82k stars 504 forks source link

Checked out a branch but data is still obtained from the main branch #8054

Closed HelloRayDeng closed 3 months ago

HelloRayDeng commented 3 months ago

Using scenario: we are developing an application using dolt-sql-server to have our database version controlled. There are several projects in our database, and each project data is held by a separate project branch. There is also an overview page which will gathers some basic information of each project. But referenced project list is held by the main branch.

So the procedure is like this:

  1. Get all available project id's from the project list table in main branch;
  2. Iterate each project id and enter into its corresponding project branch to get each project data. Now the problem is, I can not get the correct project data from each project branch. By debugging, the data always comes from the main branch, although each time before hitting the database, I have executed the switching branch operation using "CALL DOLT_CHECKOUT('" + projectId + "');" and by checking the active branch using "select active_branch()", it also gives me the right one. Only the queried data is not correct, which is from main branch instead of each project branch.

I have no idea, why does this kind of behavior happen at runtime. I also tried to make my current thread to sleep for 10 to 100 millseconds after having project branch switched, which however still makes no difference.

Development environment:

Can someone please tell me what I did wrong? Or how can I solve this kind of problem to have my application run in an expected behavior by getting data from the already switched branch. Thanks a lot.

timsehn commented 3 months ago

Now the problem is, I can not get the correct project data from each project branch. By debugging, the data always comes from the main branch, although each time before hitting the database, I have executed the switching branch operation using "CALL DOLT_CHECKOUT('" + projectId + "');" and by checking the active branch using "select active_branch()", it also gives me the right one. Only the queried data is not correct, which is from main branch instead of each project branch.

This is often caused by a connection pool or your framework using a different connection to run subsequent queries. dolt_checkout() changes the branch for the connection, not the default branch for the database. For this reason, we recommend "connecting to a branch" using your DSN a la database=mydb/branch.

As I said, this is a common pattern and usually handled in the middleware of web frameworks, here is how I handled it in various frameworks:

Laravel: https://github.com/dolthub/chirper/blob/main/app/Http/Middleware/setActiveBranch.php Django: https://github.com/dolthub/dolt_django/blob/main/polls/middleware/dolt_branch_middleware.py Rails: https://github.com/dolthub/dolt_rails/blob/main/app/controllers/branch_controller.rb

Here's a relevant blog section that explains this: https://www.dolthub.com/blog/2024-01-08-dolt-laravel/#changing-branches

I'm fairly certain this is the issue but it could be something else. Please let us know if this helps..

bpf120 commented 3 months ago

@HelloRayDeng , thanks for trying out Dolt. We'd love to learn more about your use case and how we can help. Feel free to email me or swing by our Discord if you'd like to share.

timsehn commented 3 months ago

I'm going to resolve this. Please re-open or create a new issue if you have further questions.