Comments (4)
@masterix21 Thanks for help, it worked like a charm, but I have a question, will this solution cause problems with tenant specific queue events or jobs as the connection is specified in the config ?
from laravel-multitenancy.
Hi @3assy2018, check if it is useful and give me a feedback.
from laravel-multitenancy.
I think no, if your jobs will implement the TenantAware interface, and the queue is tenant aware.
from laravel-multitenancy.
First of all, thanks for great work in this package and the simplicity of implementation, I hope this package keep developing with same philosophy of simplicity.
Preconditions:
- php version: 7.4.8
- laravel version: 7.0
- os: windows
I am trying to fire queuable event in the tenant model (created) observer method to run the migration of each created tenant in the background and simultaneously, but then I got an error like this when the event is fired:
Illuminate/Database/QueryException with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: insert into
jobs
(queue
,attempts
,reserved_at
,available_at
,created_at
,payload
) values (tenants_db, 0, ?, 1594402919, 1594402919, {"uuid":"77b0d6aa-6313-4276-82fd-0538c5eb7ee7","displayName":"App//Listeners//OnTenantCreated","job":"Illuminate//Queue//CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"delay":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"Illuminate//Events//CallQueuedListener","command":"O:36:/"Illuminate//Events//CallQueuedListener/":8:{s:5:/"class/";s:29:/"App//Listeners//OnTenantCreated/";s:6:/"method/";s:6:/"handle/";s:4:/"data/";a:1:{i:0;O:24:/"App//Events//TenantCreated/":2:{s:6:/"tenant/";O:45:/"Illuminate//Contracts//Database//ModelIdentifier/":4:{s:5:/"class/";s:26:/"App//Models//Landlord//Tenant/";s:2:/"id/";i:55;s:9:/"relations/";a:0:{}s:10:/"connection/";s:8:/"landlord/";}s:6:/"socket/";N;}}s:5:/"tries/";N;s:10:/"retryAfter/";N;s:9:/"timeoutAt/";N;s:7:/"timeout/";N;s:3:/"job/";N;}"}}))'I think this problem happens because the database default connection is set to tenant which has database value set to null by default, so I cannot be able to access landlord database jobs table, and to be able to distribute the migrations queue tasks to each tenant it must migrate the jobs table first, so I feel that I am lost in an egg or chicken dilemma, so anyone can help with solution or suggest another workflow ?
Hi @3assy2018
For my implementation I run migrations for each tenant after been created using Eloquent events:
For this extend the Tenant Model
<?php
namespace App\Models\Tenancy;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use \Spatie\Multitenancy\Models\Tenant as Model;
class Tenant extends Model
{
public static function booted()
{
static::created(function (Tenant $model) {
// TODO: make async
$model->database = $model->database ?? Str::slug($model->name, '_');
$model->createDatabase();
Artisan::call('tenants:artisan', [
'artisanCommand' => 'migrate',
'--tenant' => $model->id
]);
});
}
public function createDatabase()
{
// TODO: make async
$schema = $this->database;
$charset = config("database.connections.tenant.charset",'utf8mb4');
$collation = config("database.connections.tenant.collation",'utf8mb4_unicode_ci');
$query = "CREATE DATABASE IF NOT EXISTS {$schema} CHARACTER SET {$charset} COLLATE {$collation};";
DB::statement($query);
}
}
If dispatch jobs from routes or controllers works fine but if you try dispatch jobs from artisan command then fails as I mention here #76 and the solution for me is #76 (comment)
from laravel-multitenancy.
Related Issues (20)
- SQL error when trying to use landlord database HOT 3
- Support for custom primary key on Tenant model HOT 1
- How can I access all tenant data at once?
- Tenant Not Set (Vapor + Octane) HOT 1
- Current tenant not determined when dispatching queued closures HOT 6
- When dispatching a Tenant Aware Job in sync mode from a test, any prior/current DB transaction is cleared HOT 2
- Model ignore tenant when add Observer HOT 2
- Tenant password reset token is being stored on landlord password_reset_tokens table HOT 1
- Prefixing cache doesn't work fine with file-store HOT 3
- Conflict with Rabbitmq package from vyuldashev HOT 1
- Scheduler : Large (longer) jobs, multiple tenants, stops.
- Tenant-Aware Job Scheduling with Horizon HOT 3
- MakeQueueTenantAwareAction seems to break active requests when Queue Connection is sync HOT 4
- base table or view not found with AWS SQS queue for job TenantAware HOT 12
- randomly receive Invalid catalog name: 1046 No database selected (Connection: mysql, SQL: insert into failed_jobs (uuid, connection, queue, payload, exception, failed_at) on queue sqs HOT 5
- Tenant Aware Queue Fails No Database Selected when running from supervisor in a multi tenant environment using AWS SQS FIFO HOT 2
- Update documentation at https://spatie.be to `v3` HOT 3
- in_array(): Argument #2 ($haystack) must be of type array, null given {"exception":"[object] (TypeError(code: 0): in_array(): Argument #2 ($haystack) must be of type array, null given at /spatie/laravel-multitenancy/src/Actions/MakeQueueTenantAwareAction.php:79) HOT 1
- Cache issue with spatie/laravel-permission in TenantAware Command HOT 5
- Issue with the has function on different connections HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-multitenancy.