So I am working on a multi tenant multi database architecture application using Laravel 5. So basically every tenant has his own database. The authentication is using the JWTs.
The way I resolve the database connection issues is that I take the username
of the tenant, and the credentials of the user. Then I first change the DB to the tenant DB using the username
I got from the request and then authenticate the user against the user
table in that DB. I also put a username field in the JWT for further reference when the token is used to call other routes.
So now every time the tenant user uses this token to make a request, the DB is changed by a middleware according to the username
payload in the JWT and then the request is passed forward. If there is no username
payload in the token, it means it is the superadmin of the system and I simply connect it to the main DB which has all the tenants information stored. The code looks something like this:
$token = JWTAuth::parseToken();
$username = $token->getPayload()->get('username');
if(! $username){
\Config::set('database.connections.tenant.database', 'archive');
}else{
\Config::set('database.connections.tenant.database', $username);
\Config::set('database.default', 'tenant');
\DB::reconnect();
}
return $next($request);
Now the requests work just fine for me but when I try to run codeception tests, it gives me this exception:
Can't swap PDO instance while within transaction.
I am guessing that the Codeception Laravel5 module is trying to run database transactions and I am not allowed to change DBs while the transaction is running. Is there a way around this?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire