Giter Club home page Giter Club logo

Comments (9)

dambridge avatar dambridge commented on July 30, 2024 1

This is greasy, but it works in controller:
exec('cd ' . base_path() . ' && php artisan tenants:artisan "migrate --seed" --tenant=' . $tenant->id);

from laravel-multitenancy.

dambridge avatar dambridge commented on July 30, 2024

Hmm, I think I overlooked the simplicity of $tenant->makeCurrent().

That said, still banging on correct syntax for the Artisan call...

Artisan::call('tenants:artisan', [
            'migrate' => true,
            '--seed' => true,
            '--tenant' => [$this->tenant->id],
        ]);
Symfony\Component\Console\Exception\CommandNotFoundException: The command "tenants:artisan migrate --seed" does not exist. in /home/vagrant/code/sites/troovy/vendor/laravel/framework/src/Illuminate/Console/Application.php:181

from laravel-multitenancy.

dambridge avatar dambridge commented on July 30, 2024

Update. This seems to run properly:
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->tenant->id);

However, I now get this:

Next Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000] [1049] Unknown database 'a0eb5d78-9c85-48ae-8d21-4981a4d93643' in /home/vagrant/code/sites/troovy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31

I have a tenant prefix of 'tenant_' on tenant databases. Does the Artisan command just look at $tenant->database perhaps, without adding prefix? Checking...

Update. Tried inserting 'tenant_' . Str::uuid() instead of just UUID into Tenants db. Same error:

PDOException: SQLSTATE[HY000] [1049] Unknown database 'tenant_2e116d05-e941-4721-9212-4749743d4329' in /home/vagrant/code/sites/troovy/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27

Update 2. Confirmed, also fails in console:

SQLSTATE[HY000] [1049] Unknown database '8b3c0438-7dc3-4f4d-a652-93aea2e60ac2' (SQL: select * from information_schema.tables where table_schema = 8b3c0438-7dc3-4f4d-a652-93aea2e60ac2 and table_name = migrations and table_type = 'BASE TABLE')

from laravel-multitenancy.

dambridge avatar dambridge commented on July 30, 2024

Ultimately, I think I tracked it down to a missing config setting. Didn't have the default connection set to 'tenant'.

from laravel-multitenancy.

martincarvalho avatar martincarvalho commented on July 30, 2024

Hey @dambridge , how did you fix the command does not exist issue?

My artisan call is pretty much exactly like yours:
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->id);

But I'm still getting:
Symfony\Component\Console\Exception\CommandNotFoundException The command "tenants:artisan" does not exist.

This is how my createDatabase() method looks like:

    public function createDatabase()
    {
        DB::statement('CREATE DATABASE `'.$this->database.'`');
        Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->id);
    }

from laravel-multitenancy.

dambridge avatar dambridge commented on July 30, 2024

Hi @martincarvalho

Still working through this. This call works perfectly when I have it in a job (I have made all jobs tenant aware by default in the config).
Artisan::call('tenants:artisan "migrate --seed" --tenant=' . $this->tenant->id);

However... it fails when I run it in a controller directly with the same error! Very strange, still looking at it.

from laravel-multitenancy.

dambridge avatar dambridge commented on July 30, 2024

If it works in my job, then something about environment/context must be at play is my thinking. Not sure if this is relevant, but found this in the MultiTenancy service provider:

if ($this->app->runningInConsole()) {
            $this
                ->registerPublishables()
                ->bootCommands();
        }
protected function bootCommands(): self
    {
        $this->commands([
            TenantsArtisanCommand::class,
        ]);

        return $this;
    }

Maybe this means the command is only available from within console? And because jobs run 'out of process' it works?

from laravel-multitenancy.

freekmurze avatar freekmurze commented on July 30, 2024

In the vendor of your app, move that bootCommands outside of runningInConsole and see if just using Artisan::call. work for the command. If it does, I'd accept a PR that makes this change in the package 👍

from laravel-multitenancy.

martincarvalho avatar martincarvalho commented on July 30, 2024

After moving the bootCommands outside of runningInConsole I got the Artisan ::call to work IF in a controller.
It does not work if called inside the Tenant model.
If possible I'd like to create the database AND run the migration inside the createDatabase() method on my Tenant model, as suggested in the documentation.
Is this possible?

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.