Comments (18)
@LemarinelNet seems to be a parameters problem. Try with:
Artisan::call('tenants:artisan', [
"artisanCommand" => "migrate --database=tenant",
"--tenant" => $this->id,
]);
from laravel-multitenancy.
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.
This is working for me:
Artisan::call("tenants:artisan \"migrate --database=tenant\" --tenant={$tenant->id}");
from laravel-multitenancy.
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.
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.
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.
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.
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.
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.
Great! Thanks for your share
from laravel-multitenancy.
@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.
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.
@joselara are you calling the migration from tinker or with a console command? Please post your code.
Thanks
from laravel-multitenancy.
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.
Hi @michaelhume, if you can, please share your code in a testable GitHub repository: I'll take a look asap.
from laravel-multitenancy.
Thanks @masterix21 - I'm digging into it a bit more and will certainly post either a solution or a testable repo.
from laravel-multitenancy.
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.
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)
- Queue is not working after upgrading Laravel Framework to 9.34+
- 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
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.