GithubHelp home page GithubHelp logo

Comments (4)

3assy2018 avatar 3assy2018 commented on June 17, 2024 1

@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.

masterix21 avatar masterix21 commented on June 17, 2024

Hi @3assy2018, check if it is useful and give me a feedback.

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 17, 2024

I think no, if your jobs will implement the TenantAware interface, and the queue is tenant aware.

from laravel-multitenancy.

esalazarv avatar esalazarv commented on June 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.