GithubHelp home page GithubHelp logo

Comments (18)

masterix21 avatar masterix21 commented on June 22, 2024 3

@LemarinelNet seems to be a parameters problem. Try with:

Artisan::call('tenants:artisan', [
    "artisanCommand" => "migrate --database=tenant",
    "--tenant" => $this->id,
]);

from laravel-multitenancy.

Adesin-fr avatar Adesin-fr commented on June 22, 2024 1

Finaly solved it !
My initial code was in fact working, but the issue was from Tinker, which doesn't allow migrate commands in the shell (see laravel/tinker#37).
Tinker has a white list of allowed commands to be run, and migrate is not among them !
So, it was working from the application (web user registration), but not from tinker !

I removed the "Application::starting" part that you suggested, since it was making my code failing, and just the Artisan::call did the trick !

Thanks for your help anyway ;)

from laravel-multitenancy.

ArtisanTinkerer avatar ArtisanTinkerer commented on June 22, 2024

This is working for me:

Artisan::call("tenants:artisan \"migrate --database=tenant\" --tenant={$tenant->id}");

from laravel-multitenancy.

Adesin-fr avatar Adesin-fr commented on June 22, 2024

Thanks for you reply,

That seems to be the same line than mine, except you used double quotes...
Where are you running this code in ?
I do in my Tenant Model, which is not based on Command class. Perhaps your is located in a command ?

from laravel-multitenancy.

ArtisanTinkerer avatar ArtisanTinkerer commented on June 22, 2024

I think it is the quotes, try it like mine. If I use your line in my code, I get the same error.

from laravel-multitenancy.

Adesin-fr avatar Adesin-fr commented on June 22, 2024

Still the same error :
Symfony/Component/Console/Exception/NamespaceNotFoundException with message 'There are no commands defined in the "migrate" namespace.'

My full class is :

<?php

namespace App;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Spatie\Multitenancy\Models\Tenant;

class UserTenant extends Tenant
{
    protected $table = "tenants";

    public static function booted()
    {
        static::created(function (UserTenant $model) {
            $model->createDatabase();
        });
    }

    public function createDatabase()
    {
        $this->database = "tenantdb_" . uniqid();
        $this->save();

        $db = DB::connection();

        $charset = $db->getConfig('charset');
        $collation = $db->getConfig('collation');

        $db->statement('CREATE DATABASE ' . $this->getDatabaseName() . " CHARACTER SET `$charset` COLLATE `$collation`");

        Artisan::call('tenants:artisan', [
            "artisanCommand" => "migrate --database=tenant",
            "--tenant" => $this->id,
        ]);
    }

}

Could it be that the Artisan is searching a "migrate" command in the current namespace App, but it only exists in the App\Console\Commands namespace ?

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 22, 2024

I don't know why it happens, but can you try to execute the following code before your migrations?

Application::starting(function ($artisan) { 
    $artisan->resolveCommands([ 
        \Illuminate\Database\Console\Migrations\MigrateCommand::class,
    ]);
});

from laravel-multitenancy.

Adesin-fr avatar Adesin-fr commented on June 22, 2024

Hi,
This command doesn't fail, but doesn't resolve to anything and doesn't return anything ?
What should it do ?
Placing it before my migration command still doesn't resolve the error.

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 22, 2024

It says to your application how to resolve the migration command. I had a similar problem fixed using the suggested code.

I think that your problem isn't package-dependent, sorry.

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 22, 2024

Great! Thanks for your share

from laravel-multitenancy.

schiffty avatar schiffty commented on June 22, 2024

@LemarinelNet Thanks for the tip on the whitelist, that solved my problem too.

One question, when you added migrate:install to the whitelist, did you do it through TinkerCommand.php or through the tinker.php config file? I kept getting a resolution error when trying to do it the "right" way by adding the InstallCommand class to tinker.php, so I fell back to adding migrate:install directly to TinkerCommand.php to get it working and just want to confirm I'm not missing something obvious. Thanks.

from laravel-multitenancy.

joselara avatar joselara commented on June 22, 2024

I'm having the same issue using Artisan call method inside the console. It looks like it has something todo with tinker itself.

Symfony/Component/Console/Exception/NamespaceNotFoundException with message 'There are no commands defined in the "migrate" namespace.'

 Artisan::call('tenants:artisan', [
     "artisanCommand" => 'migrate --path=database/migrations/tenant --database=tenant',
     "--tenant" => $this->id
])

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 22, 2024

@joselara are you calling the migration from tinker or with a console command? Please post your code.

Thanks

from laravel-multitenancy.

michaelhume avatar michaelhume commented on June 22, 2024

Hi @schiffty, et. al.. - did you ever resolve your question? I've landed here having the same issue. I've tried to add

'commands' => [
        \Illuminate\Database\Console\Migrations\MigrateCommand::class,
        \Spatie\Multitenancy\Commands\TenantsArtisanCommand::class,
    ],

to tinker.php without any luck. I still get the 'There are no commands defined in the "migrate" namespace.' error when trying to run code that contains Artisan::call from within tinker.

@masterix21 - my code is inside a job that is being triggered within tinker when creating a new tenant.

from laravel-multitenancy.

masterix21 avatar masterix21 commented on June 22, 2024

Hi @michaelhume, if you can, please share your code in a testable GitHub repository: I'll take a look asap.

from laravel-multitenancy.

michaelhume avatar michaelhume commented on June 22, 2024

Thanks @masterix21 - I'm digging into it a bit more and will certainly post either a solution or a testable repo.

from laravel-multitenancy.

michaelhume avatar michaelhume commented on June 22, 2024

I was able to sort it all out. There were a number of small configuration issues but ultimately attempting to use a database queue driver in a multitenant setup and dispatching jobs on new models that don't yet have a database gets a bit hairy.

Thanks!

from laravel-multitenancy.

michaelhume avatar michaelhume commented on June 22, 2024

Hi @masterix21 - Just to add a bit to this as it is coming up again for me. I've posted a question over on laracasts as well, but wanted to mention here as I suspect this may be related to this issue.

TLDR - I believe 'There are no commands defined in the "migrate" namespace.' is a red herring and the issue is actually somehow with the Migrator database connection. I can toggle this error by manipulating app('migrator')->setConnection() and running migrate in tinker. By setting to the tenant connection, I always get the error, and can clear it be setting the landlord connection. This is regardless of having a current tenant, or fixing a tenant DB_DATABASE in config.

I don't think it's really a package issue because this works when not in tinker, but I'm just wondering if anyone else has run into this or has any insight into what might be happening here? I'd like to understand why the different behaviour inside tinker.

[edit] laravel/tinker#136

Thanks!

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.